Aggregate.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 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/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.role;

  27. import jakarta.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.     /** Shortcut for {@link it.tidalwave.util.As}. */
  46.     public static final Class<Aggregate> _Aggregate_ = Aggregate.class;

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

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

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

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

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

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