PresentationModelCollectors.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.ui.spi;

  27. import javax.annotation.Nonnull;
  28. import java.util.ArrayList;
  29. import java.util.Collection;
  30. import java.util.Collections;
  31. import java.util.List;
  32. import java.util.function.Function;
  33. import java.util.stream.Stream;
  34. import java.util.stream.StreamSupport;
  35. import it.tidalwave.util.As;
  36. import it.tidalwave.util.spi.ArrayListCollectorSupport;
  37. import it.tidalwave.role.Composite;
  38. import it.tidalwave.role.SimpleComposite;
  39. import it.tidalwave.role.ui.PresentationModel;
  40. import static it.tidalwave.util.Parameters.r;
  41. import static it.tidalwave.role.ui.Presentable._Presentable_;

  42. /***************************************************************************************************************************************************************
  43.  *
  44.  * A {@link java.util.stream.Collector} which collects a {@link Stream} of {@link PresentationModel}s into a single
  45.  * {@code PresentationModel} with a {@code Composite<PresentationModel>} role containing them.
  46.  *
  47.  * @author  Fabrizio Giudici
  48.  *
  49.  **************************************************************************************************************************************************************/
  50. public class PresentationModelCollectors extends ArrayListCollectorSupport<PresentationModel, PresentationModel>
  51.   {
  52.     @Nonnull
  53.     private final Collection<Object> roles = new ArrayList<>();
  54.    
  55.     /***********************************************************************************************************************************************************
  56.      * A {@link java.util.stream.Collector} which collects a {@link Stream} of {@link PresentationModel}s into a single
  57.      * {@code PresentationModel} with a {@link Composite} role containing them. In other words:
  58.      *
  59.      * <pre>
  60.      * List&lt;PresentationModel&gt; pms = ...
  61.      * PresentationModel compositePm = pms.stream().collect(toCompositePresentationModel());
  62.      * // same contents as childrenPms
  63.      * List&lt;PresentationModel&gt; childrenPms = compositePm.as(_Composite_).findChildren().results();
  64.      * </pre>
  65.      *
  66.      * @param   roles   some extra roles included in the resulting {@code PresentationModel}
  67.      * @return          a {@code PresentationModel}
  68.      * @since   3.2-ALPHA-3 (refactored)
  69.      **********************************************************************************************************************************************************/
  70.     @Nonnull
  71.     public static PresentationModelCollectors toCompositePresentationModel (@Nonnull final Collection<Object> roles)
  72.       {
  73.         return new PresentationModelCollectors(roles);
  74.       }

  75.     @Nonnull
  76.     public static PresentationModelCollectors toCompositePresentationModel()
  77.       {
  78.         return toCompositePresentationModel((Collection<Object>)Collections.emptyList());
  79.       }

  80.     /***********************************************************************************************************************************************************
  81.      * A facility method that creates a composite {@link PresentationModel} out of an iterable (which means an array,
  82.      * a collection or a stream) of objects implementing {@link As}. For each  object in the iterable, its {@code
  83.      * PresentationModel} is created by means of invoking its {@link it.tidalwave.role.ui.Presentable} role. Then all
  84.      * the {@code PresentationModel}s are aggregated into the composite.
  85.      *
  86.      * A function which creates specific roles for each {@code PresentationModel} can be specified. The function can
  87.      * return a single role or multiple roles in form of an array {@code Object[]}.
  88.      *
  89.      * @param   <T>             the type of the objects
  90.      * @param   i               the {@code Iterable}
  91.      * @param   roleCreator     the function to create roles
  92.      * @return                  the composite {@code PresentationModel}
  93.      *
  94.      ******************************************************************************************************************/
  95.     @Nonnull
  96.     public static <T extends As> PresentationModel toCompositePresentationModel (
  97.             @Nonnull final Iterable<? extends T> i,
  98.             @Nonnull final Function<? super T, Object> roleCreator)
  99.       {
  100.         return StreamSupport.stream(i.spliterator(), false)
  101.                             .map(o -> o.as(_Presentable_).createPresentationModel(r(roleCreator.apply(o))))
  102.                             .collect(toCompositePresentationModel());
  103.       }

  104.     /***********************************************************************************************************************************************************
  105.      * A facility simplified version of
  106.      * {@link #toCompositePresentationModel(java.lang.Iterable, java.util.function.Function)}.
  107.      *
  108.      * @param   <T>             the type of the objects
  109.      * @param   i               the {@code Iterable}
  110.      * @return                  the composite {@code PresentationModel}
  111.      *
  112.      ******************************************************************************************************************/
  113.     @Nonnull
  114.     public static <T extends As> PresentationModel toCompositePresentationModel (@Nonnull final Iterable<T> i)
  115.       {
  116.         return toCompositePresentationModel(i, o -> new Object[0]);
  117.       }

  118.     /***********************************************************************************************************************************************************
  119.      * {@inheritDoc}
  120.      *
  121.      ******************************************************************************************************************/
  122.     @Override @Nonnull
  123.     public Function<List<PresentationModel>, PresentationModel> finisher()
  124.       {
  125.         return childrenPms -> PresentationModel.of("", r(roles, SimpleComposite.ofCloned(childrenPms)));
  126.       }
  127.    
  128.     /***********************************************************************************************************************************************************
  129.      *
  130.      ******************************************************************************************************************/
  131.     private PresentationModelCollectors (@Nonnull final Collection<Object> roles)
  132.       {
  133.         this.roles.addAll(roles);
  134.       }
  135.   }