Skip to content

Package: MapAggregate

MapAggregate

nameinstructionbranchcomplexitylinemethod
MapAggregate()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
MapAggregate(Map)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getByName(String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getNames()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
with(String, Object)
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

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:
29: import javax.annotation.Nonnull;
30: import javax.annotation.concurrent.Immutable;
31: import java.util.Collections;
32: import java.util.HashMap;
33: import java.util.Map;
34: import java.util.Optional;
35: import java.util.Set;
36: import java.util.concurrent.CopyOnWriteArraySet;
37: import it.tidalwave.role.Aggregate;
38: import lombok.ToString;
39:
40: /***********************************************************************************************************************
41: *
42: * A map-based implementation of {@link Aggregate}.
43: * This is no more a public class; use {@link Aggregate#of(String, Object)} instead.
44: * @stereotype Role
45: *
46: * @param <T> the type of the aggregate
47: *
48: * @author Fabrizio Giudici
49: *
50: **********************************************************************************************************************/
51: @Immutable @ToString
52: public class MapAggregate<T> implements Aggregate<T>
53: {
54: private final Map<String, T> mapByName;
55:
56: /*******************************************************************************************************************
57: *
58: *
59: ******************************************************************************************************************/
60: public MapAggregate()
61: {
62: this.mapByName = Collections.emptyMap();
63: }
64:
65: /*******************************************************************************************************************
66: *
67: *
68: ******************************************************************************************************************/
69: public MapAggregate (@Nonnull final Map<String, T> mapByName)
70: {
71: this.mapByName = Map.copyOf(mapByName);
72: }
73:
74: /*******************************************************************************************************************
75: *
76: * {@inheritDoc}
77: *
78: ******************************************************************************************************************/
79: @Override @Nonnull
80: public Optional<T> getByName (@Nonnull final String name)
81: {
82: return Optional.ofNullable(mapByName.get(name));
83: }
84:
85: /*******************************************************************************************************************
86: *
87: * {@inheritDoc}
88: *
89: ******************************************************************************************************************/
90: @Override @Nonnull
91: public Set<String> getNames()
92: {
93: return new CopyOnWriteArraySet<>(mapByName.keySet());
94: }
95:
96: /*******************************************************************************************************************
97: *
98: * {@inheritDoc}
99: *
100: ******************************************************************************************************************/
101: @Override @Nonnull
102: public Aggregate<T> with (@Nonnull final String name, @Nonnull final T object)
103: {
104: final Map<String, T> clone = new HashMap<>(mapByName);
105: clone.put(name, object);
106: return new MapAggregate<>(clone);
107: }
108: }