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.impl;
27
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 *
41 * A map-based implementation of {@link Aggregate}.
42 * This is no more a public class; use {@link Aggregate#of(String, Object)} instead.
43 * @stereotype Role
44 *
45 * @param <T> the type of the aggregate
46 *
47 * @author Fabrizio Giudici
48 *
49 **************************************************************************************************************************************************************/
50 @Immutable @ToString
51 public class MapAggregate<T> implements Aggregate<T>
52 {
53 private final Map<String, T> mapByName;
54
55 /***********************************************************************************************************************************************************
56 * Creates a new instance.
57 **********************************************************************************************************************************************************/
58 public MapAggregate()
59 {
60 this.mapByName = Collections.emptyMap();
61 }
62
63 /***********************************************************************************************************************************************************
64 * Creates a new instance copying fron a given map.
65 * @param mapByName the map
66 **********************************************************************************************************************************************************/
67 public MapAggregate (@Nonnull final Map<String, T> mapByName)
68 {
69 this.mapByName = Map.copyOf(mapByName);
70 }
71
72 /***********************************************************************************************************************************************************
73 * {@inheritDoc}
74 **********************************************************************************************************************************************************/
75 @Override @Nonnull
76 public Optional<T> getByName (@Nonnull final String name)
77 {
78 return Optional.ofNullable(mapByName.get(name));
79 }
80
81 /***********************************************************************************************************************************************************
82 * {@inheritDoc}
83 **********************************************************************************************************************************************************/
84 @Override @Nonnull
85 public Set<String> getNames()
86 {
87 return new CopyOnWriteArraySet<>(mapByName.keySet());
88 }
89
90 /***********************************************************************************************************************************************************
91 * {@inheritDoc}
92 **********************************************************************************************************************************************************/
93 @Override @Nonnull
94 public Aggregate<T> with (@Nonnull final String name, @Nonnull final T object)
95 {
96 final Map<String, T> clone = new HashMap<>(mapByName);
97 clone.put(name, object);
98 return new MapAggregate<>(clone);
99 }
100 }