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

  28. import javax.annotation.Nonnull;
  29. import java.util.Collection;
  30. import java.util.Collections;
  31. import java.util.Map;
  32. import java.util.Optional;
  33. import it.tidalwave.role.impl.MapAggregate;

  34. /***********************************************************************************************************************
  35.  *
  36.  * The role of an aggregate object, that is an object which contains other named objects.
  37.  *
  38.  * @stereotype Role
  39.  *
  40.  * @author  Fabrizio Giudici
  41.  *
  42.  **********************************************************************************************************************/
  43. @FunctionalInterface
  44. public interface Aggregate<T>
  45.   {
  46.     public static final Class<Aggregate> _Aggregate_ = Aggregate.class;

  47.     /*******************************************************************************************************************
  48.      *
  49.      * Returns an object given its name.
  50.      *
  51.      * @param   name      the name
  52.      * @return  the object
  53.      *
  54.      ******************************************************************************************************************/
  55.     @Nonnull
  56.     public Optional<T> getByName (@Nonnull String name);

  57.     /*******************************************************************************************************************
  58.      *
  59.      * Returns the names of contained objects.
  60.      *
  61.      * @return  the names of the objects
  62.      * @since 3.1-ALPHA-8
  63.      *
  64.      ******************************************************************************************************************/
  65.     @Nonnull
  66.     public default Collection<String> getNames()
  67.       {
  68.         return Collections.emptyList();
  69.       }

  70.     /*******************************************************************************************************************
  71.      *
  72.      * Returns a new instance with the specified (name, value) pairs.
  73.      *
  74.      * @param   <T>         the static type of the value
  75.      * @param   mapByName   the map containing the pairs
  76.      * @return              the new instance
  77.      * @since               3.2-ALPHA-1
  78.      *
  79.      ******************************************************************************************************************/
  80.     @Nonnull
  81.     public static <T> Aggregate<T> of (@Nonnull final Map<String, T> mapByName)
  82.       {
  83.         return new MapAggregate<>(mapByName);
  84.       }

  85.     /*******************************************************************************************************************
  86.      *
  87.      * Returns a new empty instance that will be populated by means of {@link #with(String, Object)}.
  88.      *
  89.      * @param   <T>         the static type of the aggregate
  90.      * @return              the new instance
  91.      * @since               3.2-ALPHA-2
  92.      *
  93.      ******************************************************************************************************************/
  94.     @Nonnull
  95.     public static <T> Aggregate<T> newInstance()
  96.       {
  97.         return new MapAggregate<>();
  98.       }

  99.     /*******************************************************************************************************************
  100.      *
  101.      * Returns a new instance with the specified (name, value) pair.
  102.      *
  103.      * @param   <T>         the static type of the aggregate
  104.      * @param   name        the name in the pair
  105.      * @param   value       the value in the pair
  106.      * @return              the new instance
  107.      * @since               3.2-ALPHA-1
  108.      *
  109.      ******************************************************************************************************************/
  110.     @Nonnull
  111.     public static <T> Aggregate<T> of (@Nonnull final String name, @Nonnull final T value)
  112.       {
  113.         return new MapAggregate<T>().with(name, value);
  114.       }

  115.     /*******************************************************************************************************************
  116.      *
  117.      * Returns a new instance with the specified (name, value) pair.
  118.      *
  119.      * @param   name        the name in the pair
  120.      * @param   value       the value in the pair
  121.      * @return              the new instance
  122.      * @since               3.2-ALPHA-1
  123.      *
  124.      ******************************************************************************************************************/
  125.     @Nonnull
  126.     public default Aggregate<T> with (@Nonnull final String name, @Nonnull final T value)
  127.       {
  128.         return new MapAggregate<T>().with(name, value);
  129.       }
  130.   }