Composite.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *************************************************************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  12.  * You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.role;

  27. import jakarta.annotation.Nonnull;
  28. import java.util.Optional;
  29. import java.util.function.Consumer;
  30. import java.util.stream.Stream;
  31. import it.tidalwave.util.Finder;

  32. /***************************************************************************************************************************************************************
  33.  *
  34.  * The role of a composite object, that is an object which contains children. They are exposed by means of a
  35.  * {@link Finder}.
  36.  *
  37.  * @stereotype Role
  38.  *
  39.  * @author  Fabrizio Giudici
  40.  * @it.tidalwave.javadoc.stable
  41.  *
  42.  **************************************************************************************************************************************************************/
  43. @FunctionalInterface
  44. public interface Composite<T, F extends Finder<? extends T>>
  45.   {
  46.     /** Shortcut for {@link it.tidalwave.util.As}. */
  47.     public static final Class<Composite> _Composite_ = Composite.class;

  48.     /***********************************************************************************************************************************************************
  49.      * A default <code>Composite</code> with no children.
  50.      **********************************************************************************************************************************************************/
  51.     public static final Composite<Object, Finder<Object>> DEFAULT = new Composite<>()
  52.       {
  53.         @Override @Nonnull
  54.         public Finder<Object> findChildren()
  55.           {
  56.             return Finder.empty();
  57.           }
  58.       };

  59.     /***********************************************************************************************************************************************************
  60.      * {@return the children of this object}.
  61.      **********************************************************************************************************************************************************/
  62.     @Nonnull
  63.     public F findChildren();

  64.     /***********************************************************************************************************************************************************
  65.      * {@return  a stream of children}.
  66.      * @since 3.2-ALPHA-23
  67.      **********************************************************************************************************************************************************/
  68.     @Nonnull
  69.     public default Stream<? extends T> stream()
  70.       {
  71.         return findChildren().stream();
  72.       }

  73.     /***********************************************************************************************************************************************************
  74.      * Iterates through children.
  75.      * @param   consumer  the consumer
  76.      * @since 3.2-ALPHA-23
  77.      **********************************************************************************************************************************************************/
  78.     public default void forEach (@Nonnull final Consumer<? super T> consumer)
  79.       {
  80.         stream().forEach(consumer);
  81.       }

  82.     /***********************************************************************************************************************************************************
  83.      * A visitor that can travel through the {@code Composite} items.
  84.      * @param   <T>   the type of the composite
  85.      * @param   <R>   the type of the result
  86.      **********************************************************************************************************************************************************/
  87.     @SuppressWarnings("EmptyMethod")
  88.     public static interface Visitor<T, R>
  89.       {
  90.         /*******************************************************************************************************************************************************
  91.          * Visits an object. This method is called before visiting children (pre-order).
  92.          * @param  object  the visited object
  93.          ******************************************************************************************************************************************************/
  94.         public default void preVisit (@Nonnull final T object)
  95.           {
  96.           }

  97.         /*******************************************************************************************************************************************************
  98.          * Visits an object. This method is actually called just after {@link #preVisit(Object)}, it makes sense to implement it when you don't need to
  99.          * distinguish between pre-order and post-order traversal.
  100.          * @param  object  the visited object
  101.          ******************************************************************************************************************************************************/
  102.         public default void visit (@Nonnull final T object)
  103.           {
  104.           }

  105.         /*******************************************************************************************************************************************************
  106.          * Visits an object. This method is called after visiting children (post-order).
  107.          * @param  object  the visited object
  108.          ******************************************************************************************************************************************************/
  109.         public default void postVisit (@Nonnull final T object)
  110.           {
  111.           }

  112.         /*******************************************************************************************************************************************************
  113.          * {@return the value of this visitor}.
  114.          ******************************************************************************************************************************************************/
  115.         @Nonnull
  116.         public default Optional<R> getValue()
  117.           {
  118.             return Optional.empty();
  119.           }
  120.       }
  121.   }