PresentationModelAggregate.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;

  27. import javax.annotation.Nonnull;
  28. import java.util.Collection;
  29. import java.util.Optional;
  30. import java.util.Set;
  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.      * Creates a new, empty instance.
  51.      *
  52.      * @return          the new instance
  53.      **********************************************************************************************************************************************************/
  54.     @Nonnull
  55.     public static PresentationModelAggregate newInstance()
  56.       {
  57.         return new PresentationModelAggregate(Aggregate.newInstance());
  58.       }

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

  84.     /***********************************************************************************************************************************************************
  85.      * {@inheritDoc}
  86.      **********************************************************************************************************************************************************/
  87.     @Override @Nonnull
  88.     public Optional<PresentationModel> getByName (@Nonnull final String name)
  89.       {
  90.         return delegate.getByName(name);
  91.       }

  92.     /***********************************************************************************************************************************************************
  93.      * {@inheritDoc}
  94.      **********************************************************************************************************************************************************/
  95.     @Override @Nonnull
  96.     public Set<String> getNames()
  97.       {
  98.         return delegate.getNames();
  99.       }
  100.   }