Skip to contentPackage: TypeSafeMap
TypeSafeMap
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.util;
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.Collections;
34: import java.util.Map;
35: import java.util.Optional;
36: import java.util.Set;
37: import java.util.function.BiConsumer;
38: import it.tidalwave.util.impl.TypeSafeHashMap;
39:
40: /***********************************************************************************************************************
41: *
42: * A map that is type safe, i.e. the pairs (key, value) are type-checked. It's immutable.
43: *
44: * @author Fabrizio Giudici
45: *
46: **********************************************************************************************************************/
47: @Immutable
48: public interface TypeSafeMap extends Iterable<Map.Entry<Key<?>, Object>>
49: {
50: /*******************************************************************************************************************
51: *
52: * Returns a value given its key.
53: *
54: * @param <T> the type
55: * @param key the key
56: * @return the value
57: * @throws NotFoundException if the key is not found
58: * @deprecated Use {@link #getOptional(Key)} instead
59: *
60: ******************************************************************************************************************/
61: @Nonnull @Deprecated
62: public <T> T get (@Nonnull Key<T> key)
63: throws NotFoundException;
64:
65: /*******************************************************************************************************************
66: *
67: * Returns an optional value given its key.
68: *
69: * @param <T> the type
70: * @param key the key
71: * @return the value
72: *
73: * @since 3.2-ALPHA-1
74: *
75: ******************************************************************************************************************/
76: @Nonnull
77: public default <T> Optional<T> getOptional (@Nonnull final Key<? extends T> key)
78: {
79: try
80: {
81: return Optional.of(get(key));
82: }
83: catch (NotFoundException e)
84: {
85: return Optional.empty();
86: }
87: }
88:
89: /*******************************************************************************************************************
90: *
91: * Checks whether a pair has been stored.
92: *
93: * @param key the key
94: * @return {@code true} if the pair is present
95: *
96: ******************************************************************************************************************/
97: public boolean containsKey (@Nonnull Key<?> key);
98:
99: /*******************************************************************************************************************
100: *
101: * Returns a set of all the contained keys.
102: *
103: * @return the keys as a mutable set
104: *
105: ******************************************************************************************************************/
106: @Nonnull
107: public Set<Key<?>> keySet();
108:
109: /*******************************************************************************************************************
110: *
111: * Returns a set of all the contained values.
112: *
113: * @return the values as a mutable collection
114: * @since 3.2-ALPHA-6
115: *
116: ******************************************************************************************************************/
117: @Nonnull
118: public Collection<Object> values();
119:
120: /*******************************************************************************************************************
121: *
122: * Returns a set of all the contained (key, value) pairs.
123: *
124: * @return the pairs as a mutable collection
125: * @since 3.2-ALPHA-6
126: *
127: ******************************************************************************************************************/
128: @Nonnull
129: public Set<Map.Entry<Key<?>, Object>> entrySet();
130:
131: /*******************************************************************************************************************
132: *
133: * Returns the size of this map.
134: *
135: * @return the size
136: *
137: ******************************************************************************************************************/
138: @Nonnegative
139: public int size();
140:
141: /*******************************************************************************************************************
142: *
143: * Returns the contents as a plain {@link Map}.
144: *
145: * @return the contents as a mutable map
146: *
147: ******************************************************************************************************************/
148: @Nonnull
149: public Map<Key<?>, Object> asMap();
150:
151: /*******************************************************************************************************************
152: *
153: * Performs the given action on all the pairs (key, value) contained in this map.
154: *
155: * @param action the action
156: * @since 3.2-ALPHA-10
157: *
158: ******************************************************************************************************************/
159: public void forEach (@Nonnull BiConsumer<? super Key<?>, ? super Object> action);
160:
161: /*******************************************************************************************************************
162: *
163: * Create a new instance with an additional pair (key, value=
164: *
165: * @param <T> the type
166: * @param key the key
167: * @param value the value
168: * @return the new instance
169: * @since 3.2-ALPHA-2
170: *
171: ******************************************************************************************************************/
172: @Nonnull
173: public <T> TypeSafeMap with (@Nonnull final Key<T> key, @Nonnull final T value);
174:
175: /*******************************************************************************************************************
176: *
177: * Creates a new empty instance.
178: *
179: * @return the new instance
180: * @since 3.2-ALPHA-2
181: *
182: ******************************************************************************************************************/
183: @Nonnull
184: public static TypeSafeMap newInstance()
185: {
186: return new TypeSafeHashMap(Collections.emptyMap());
187: }
188:
189: /*******************************************************************************************************************
190: *
191: * Creates an instance cloning the given map.
192: *
193: * @param map the map to clone
194: * @return the new instance
195: * @since 3.2-ALPHA-2
196: *
197: ******************************************************************************************************************/
198: @Nonnull
199: public static TypeSafeMap ofCloned (@Nonnull final Map<? extends Key<?>, Object> map)
200: {
201: return new TypeSafeHashMap(map);
202: }
203: }