Package: TypeSafeHashMap
TypeSafeHashMap
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TypeSafeHashMap(Map) |
|
|
|
|
|
||||||||||||||||||||
TypeSafeHashMap(Map, boolean) |
|
|
|
|
|
||||||||||||||||||||
asMap() |
|
|
|
|
|
||||||||||||||||||||
canEqual(Object) |
|
|
|
|
|
||||||||||||||||||||
containsKey(Key) |
|
|
|
|
|
||||||||||||||||||||
entrySet() |
|
|
|
|
|
||||||||||||||||||||
equals(Object) |
|
|
|
|
|
||||||||||||||||||||
forEach(BiConsumer) |
|
|
|
|
|
||||||||||||||||||||
get(Key) |
|
|
|
|
|
||||||||||||||||||||
hashCode() |
|
|
|
|
|
||||||||||||||||||||
iterator() |
|
|
|
|
|
||||||||||||||||||||
keySet() |
|
|
|
|
|
||||||||||||||||||||
size() |
|
|
|
|
|
||||||||||||||||||||
toString() |
|
|
|
|
|
||||||||||||||||||||
values() |
|
|
|
|
|
||||||||||||||||||||
with(Key, Object) |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 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.util.impl;
28:
29: import javax.annotation.Nonnegative;
30: import javax.annotation.Nonnull;
31: import javax.annotation.concurrent.Immutable;
32: import java.util.Collection;
33: import java.util.HashMap;
34: import java.util.Iterator;
35: import java.util.Map;
36: import java.util.Set;
37: import java.util.concurrent.CopyOnWriteArraySet;
38: import java.util.function.BiConsumer;
39: import java.io.Serializable;
40: import it.tidalwave.util.Key;
41: import it.tidalwave.util.NotFoundException;
42: import it.tidalwave.util.TypeSafeMap;
43: import lombok.EqualsAndHashCode;
44:
45: /***********************************************************************************************************************
46: *
47: * An implementation of {@link TypeSafeMap}. This class is not part of the public API.
48: *
49: * @author Fabrizio Giudici
50: *
51: **********************************************************************************************************************/
52:•@Immutable @EqualsAndHashCode
53: public class TypeSafeHashMap implements TypeSafeMap, Serializable
54: {
55: private static final long serialVersionUID = 564564576856746L;
56:
57: @Nonnull
58: private final Map<Key<?>, Object> map;
59:
60: /*******************************************************************************************************************
61: *
62: *
63: ******************************************************************************************************************/
64: public TypeSafeHashMap (@Nonnull final Map<Key<?>, Object> map)
65: {
66: this(new HashMap<>(), false);
67: this.map.putAll(map);
68: }
69:
70: /*******************************************************************************************************************
71: *
72: *
73: *
74: ******************************************************************************************************************/
75: /* package */ TypeSafeHashMap (@Nonnull final Map<Key<?>, Object> map, final boolean dummy)
76: {
77: this.map = map;
78: }
79:
80: /*******************************************************************************************************************
81: *
82: * {@inheritDoc}
83: *
84: ******************************************************************************************************************/
85: @Override @Nonnull
86: public <T> T get (@Nonnull final Key<T> key)
87: throws NotFoundException
88: {
89: return NotFoundException.throwWhenNull(key.getType().cast(map.get(key)), "not found: %s", key);
90: }
91:
92: /*******************************************************************************************************************
93: *
94: * {@inheritDoc}
95: *
96: ******************************************************************************************************************/
97: @Override
98: public boolean containsKey (@Nonnull final Key<?> key)
99: {
100: return map.containsKey(key);
101: }
102:
103: /*******************************************************************************************************************
104: *
105: * {@inheritDoc}
106: *
107: ******************************************************************************************************************/
108: @Override @Nonnull
109: public <T> TypeSafeMap with (@Nonnull final Key<T> key, @Nonnull final T value)
110: {
111: final Map<Key<?>, Object> map = asMap();
112: map.put(key, value);
113: return new TypeSafeHashMap(map, true);
114: }
115:
116: /*******************************************************************************************************************
117: *
118: * {@inheritDoc}
119: *
120: ******************************************************************************************************************/
121: @Override @Nonnull
122: public Set<Key<?>> keySet()
123: {
124: return new CopyOnWriteArraySet<>(map.keySet());
125: }
126:
127: /*******************************************************************************************************************
128: *
129: * {@inheritDoc}
130: *
131: ******************************************************************************************************************/
132: @Override @Nonnull
133: public Collection<Object> values()
134: {
135: return map.values();
136: }
137:
138: /*******************************************************************************************************************
139: *
140: * {@inheritDoc}
141: *
142: ******************************************************************************************************************/
143: @Override @Nonnull
144: public Set<Map.Entry<Key<?>, Object>> entrySet()
145: {
146: return map.entrySet();
147: }
148:
149: /*******************************************************************************************************************
150: *
151: * {@inheritDoc}
152: *
153: ******************************************************************************************************************/
154: @Override @Nonnegative
155: public int size()
156: {
157: return map.size();
158: }
159:
160: /*******************************************************************************************************************
161: *
162: * {@inheritDoc}
163: *
164: ******************************************************************************************************************/
165: @Override @Nonnull
166: public Iterator<Map.Entry<Key<?>, Object>> iterator()
167: {
168: return map.entrySet().iterator();
169: }
170:
171: /*******************************************************************************************************************
172: *
173: * {@inheritDoc}
174: *
175: ******************************************************************************************************************/
176: @Override @Nonnull
177: public Map<Key<?>, Object> asMap()
178: {
179: return new HashMap<>(map);
180: }
181:
182: /*******************************************************************************************************************
183: *
184: * {@inheritDoc}
185: *
186: ******************************************************************************************************************/
187: @Override
188: public void forEach (@Nonnull final BiConsumer<? super Key<?>, ? super Object> action)
189: {
190: map.forEach(action);
191: }
192:
193: /*******************************************************************************************************************
194: *
195: * {@inheritDoc}
196: *
197: ******************************************************************************************************************/
198: @Override @Nonnull
199: public String toString()
200: {
201: return map.toString();
202: }
203: }