MapAggregate.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
  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.impl;

  28. import javax.annotation.Nonnull;
  29. import javax.annotation.concurrent.Immutable;
  30. import java.util.Collections;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. import java.util.Optional;
  34. import java.util.Set;
  35. import java.util.concurrent.CopyOnWriteArraySet;
  36. import it.tidalwave.role.Aggregate;
  37. import lombok.ToString;

  38. /***********************************************************************************************************************
  39.  *
  40.  * A map-based implementation of {@link Aggregate}.
  41.  * This is no more a public class; use {@link Aggregate#of(String, Object)} instead.
  42.  * @stereotype Role
  43.  *
  44.  * @param <T>    the type of the aggregate
  45.  *
  46.  * @author  Fabrizio Giudici
  47.  *
  48.  **********************************************************************************************************************/
  49. @Immutable @ToString
  50. public class MapAggregate<T> implements Aggregate<T>
  51.   {
  52.     private final Map<String, T> mapByName;

  53.     /*******************************************************************************************************************
  54.      *
  55.      *
  56.      ******************************************************************************************************************/
  57.     public MapAggregate()
  58.       {
  59.         this.mapByName = Collections.emptyMap();
  60.       }

  61.     /*******************************************************************************************************************
  62.      *
  63.      *
  64.      ******************************************************************************************************************/
  65.     public MapAggregate (@Nonnull final Map<String, T> mapByName)
  66.       {
  67.         this.mapByName = Map.copyOf(mapByName);
  68.       }

  69.     /*******************************************************************************************************************
  70.      *
  71.      * {@inheritDoc}
  72.      *
  73.      ******************************************************************************************************************/
  74.     @Override @Nonnull
  75.     public Optional<T> getByName (@Nonnull final String name)
  76.       {
  77.         return Optional.ofNullable(mapByName.get(name));
  78.       }

  79.     /*******************************************************************************************************************
  80.      *
  81.      * {@inheritDoc}
  82.      *
  83.      ******************************************************************************************************************/
  84.     @Override @Nonnull
  85.     public Set<String> getNames()
  86.       {
  87.         return new CopyOnWriteArraySet<>(mapByName.keySet());
  88.       }

  89.     /*******************************************************************************************************************
  90.      *
  91.      * {@inheritDoc}
  92.      *
  93.      ******************************************************************************************************************/
  94.     @Override @Nonnull
  95.     public Aggregate<T> with (@Nonnull final String name, @Nonnull final T object)
  96.       {
  97.         final Map<String, T> clone = new HashMap<>(mapByName);
  98.         clone.put(name, object);
  99.         return new MapAggregate<>(clone);
  100.       }
  101.   }