Skip to contentMethod: getByName(String)
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.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: **********************************************************************************************************************************************************/
57: public MapAggregate()
58: {
59: this.mapByName = Collections.emptyMap();
60: }
61:
62: /***********************************************************************************************************************************************************
63: **********************************************************************************************************************************************************/
64: public MapAggregate (@Nonnull final Map<String, T> mapByName)
65: {
66: this.mapByName = Map.copyOf(mapByName);
67: }
68:
69: /***********************************************************************************************************************************************************
70: * {@inheritDoc}
71: **********************************************************************************************************************************************************/
72: @Override @Nonnull
73: public Optional<T> getByName (@Nonnull final String name)
74: {
75: return Optional.ofNullable(mapByName.get(name));
76: }
77:
78: /***********************************************************************************************************************************************************
79: * {@inheritDoc}
80: **********************************************************************************************************************************************************/
81: @Override @Nonnull
82: public Set<String> getNames()
83: {
84: return new CopyOnWriteArraySet<>(mapByName.keySet());
85: }
86:
87: /***********************************************************************************************************************************************************
88: * {@inheritDoc}
89: **********************************************************************************************************************************************************/
90: @Override @Nonnull
91: public Aggregate<T> with (@Nonnull final String name, @Nonnull final T object)
92: {
93: final Map<String, T> clone = new HashMap<>(mapByName);
94: clone.put(name, object);
95: return new MapAggregate<>(clone);
96: }
97: }