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

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

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

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

  53.     /***********************************************************************************************************************************************************
  54.      * Returns the names of contained objects.
  55.      * @return  the names of the objects
  56.      * @since   3.1-ALPHA-8
  57.      **********************************************************************************************************************************************************/
  58.     @Nonnull
  59.     public default Set<String> getNames()
  60.       {
  61.         return Collections.emptySet();
  62.       }

  63.     /***********************************************************************************************************************************************************
  64.      * Returns a new instance with the specified (name, value) pairs.
  65.      * @param   <T>         the static type of the value
  66.      * @param   mapByName   the map containing the pairs
  67.      * @return              the new instance
  68.      * @since               3.2-ALPHA-1
  69.      **********************************************************************************************************************************************************/
  70.     @Nonnull
  71.     public static <T> Aggregate<T> of (@Nonnull final Map<String, T> mapByName)
  72.       {
  73.         return new MapAggregate<>(mapByName);
  74.       }

  75.     /***********************************************************************************************************************************************************
  76.      * Returns a new empty instance that will be populated by means of {@link #with(String, Object)}.
  77.      * @param   <T>         the static type of the aggregate
  78.      * @return              the new instance
  79.      * @since               3.2-ALPHA-2
  80.      **********************************************************************************************************************************************************/
  81.     @Nonnull
  82.     public static <T> Aggregate<T> newInstance()
  83.       {
  84.         return new MapAggregate<>();
  85.       }

  86.     /***********************************************************************************************************************************************************
  87.      * Returns a new instance with the specified (name, value) pair.
  88.      * @param   <T>         the static type of the aggregate
  89.      * @param   name        the name in the pair
  90.      * @param   value       the value in the pair
  91.      * @return              the new instance
  92.      * @since               3.2-ALPHA-1
  93.      **********************************************************************************************************************************************************/
  94.     @Nonnull
  95.     public static <T> Aggregate<T> of (@Nonnull final String name, @Nonnull final T value)
  96.       {
  97.         return new MapAggregate<T>().with(name, value);
  98.       }

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