PresentationModelAggregate.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2023 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
  12.  * the License. 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
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  23.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.role.ui;

  28. import javax.annotation.Nonnull;
  29. import java.util.Collection;
  30. import java.util.Optional;
  31. import it.tidalwave.role.Aggregate;
  32. import lombok.AccessLevel;
  33. import lombok.RequiredArgsConstructor;
  34. import lombok.ToString;

  35. /***********************************************************************************************************************
  36.  *
  37.  * A specialisation of {@link Aggregate}{@code <PresentationModel>} which offers a convenience method for aggregating
  38.  * its contained objects.
  39.  *
  40.  * @author  Fabrizio Giudici
  41.  * @since 3.2-ALPHA-3
  42.  *
  43.  **********************************************************************************************************************/
  44. @RequiredArgsConstructor(access = AccessLevel.PRIVATE) @ToString
  45. public class PresentationModelAggregate implements Aggregate<PresentationModel>
  46.   {
  47.     @Nonnull
  48.     private final Aggregate<PresentationModel> delegate;

  49.     /*******************************************************************************************************************
  50.      *
  51.      * Creates a new, empty instance.
  52.      *
  53.      * @return          the new instance
  54.      *
  55.      ******************************************************************************************************************/
  56.     @Nonnull
  57.     public static PresentationModelAggregate newInstance()
  58.       {
  59.         return new PresentationModelAggregate(Aggregate.newInstance());
  60.       }

  61.     /*******************************************************************************************************************
  62.      *
  63.      * Adds another {@link PresentationModel} with the given roles, associated to the given name. With a plain
  64.      * {@link Aggregate}{@code <PresentationModel>} the code would be:
  65.      *
  66.      * <pre>
  67.      *   Aggregate&lt;PresentationModel&gt; aggregate = Aggregate.newInstance()
  68.      *            .with("name", PresentationModel.of("", r(role1, role2, role3));
  69.      * </pre>
  70.      *
  71.      * The simplified code is:
  72.      *
  73.      * <pre>
  74.      *   Aggregate&lt;PresentationModel&gt; aggregate = PresentationModelAggregate.newInstance()
  75.      *            .withPmOf("name", r(role1, role2, role3));
  76.      * </pre>
  77.      *
  78.      * @param   name    the name of the {@code PresentationModel}
  79.      * @param   roles   the roles
  80.      * @return          the new {@code PresentationModel}
  81.      *
  82.      ******************************************************************************************************************/
  83.     @Nonnull
  84.     public PresentationModelAggregate withPmOf (@Nonnull final String name, @Nonnull final Collection<Object> roles)
  85.       {
  86.         return new PresentationModelAggregate(delegate.with(name, PresentationModel.of("", roles)));
  87.       }

  88.     /*******************************************************************************************************************
  89.      *
  90.      * {@inheritDoc}
  91.      *
  92.      ******************************************************************************************************************/
  93.     @Override @Nonnull
  94.     public Optional<PresentationModel> getByName (@Nonnull final String name)
  95.       {
  96.         return delegate.getByName(name);
  97.       }

  98.     /*******************************************************************************************************************
  99.      *
  100.      * {@inheritDoc}
  101.      *
  102.      ******************************************************************************************************************/
  103.     @Override @Nonnull
  104.     public Collection<String> getNames()
  105.       {
  106.         return delegate.getNames();
  107.       }
  108.   }