Composite.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2024 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 javax.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.     public static final Class<Composite> _Composite_ = Composite.class;

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

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

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

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

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

  96.         /*******************************************************************************************************************************************************
  97.          * 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
  98.          * distinguish between pre-order and post-order traversal.
  99.          * @param  object  the visited object
  100.          ******************************************************************************************************************************************************/
  101.         public default void visit (@Nonnull final T object)
  102.           {
  103.           }

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

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