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