PresentationModelCollectors.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * SteelBlue: DCI User Interfaces
  5.  * http://tidalwave.it/projects/steelblue
  6.  *
  7.  * Copyright (C) 2015 - 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/steelblue-src
  22.  * git clone https://github.com/tidalwave-it/steelblue-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.ui.core.role.spi;

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

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

  81.     /***********************************************************************************************************************************************************
  82.      * A facility method that creates a composite {@link PresentationModel} out of an iterable (which means an array,
  83.      * a collection or a stream) of objects implementing {@link As}. For each  object in the iterable, its {@code
  84.      * PresentationModel} is created by means of invoking its {@link it.tidalwave.ui.core.role.Presentable} role. Then all
  85.      * the {@code PresentationModel}s are aggregated into the composite.
  86.      *
  87.      * A function which creates specific roles for each {@code PresentationModel} can be specified. The function can
  88.      * return a single role or multiple roles in form of an array {@code Object[]}.
  89.      *
  90.      * @param   <T>             the type of the objects
  91.      * @param   i               the {@code Iterable}
  92.      * @param   roleCreator     the function to create roles
  93.      * @return                  the composite {@code PresentationModel}
  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.     @Nonnull
  113.     public static <T extends As> PresentationModel toCompositePresentationModel (@Nonnull final Iterable<T> i)
  114.       {
  115.         return toCompositePresentationModel(i, o -> new Object[0]);
  116.       }

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