/
MapIterable.java
287 lines (255 loc) · 11 KB
/
MapIterable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Copyright (c) 2016 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.api.map;
import java.util.Map;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.multimap.Multimap;
import org.eclipse.collections.api.tuple.Pair;
/**
* A Read-only Map API, with the minor exception inherited from java.lang.Iterable. The method map.iterator().remove()
* will throw an UnsupportedOperationException.
*/
public interface MapIterable<K, V> extends RichIterable<V>
{
/**
* @see Map#get(Object)
*/
V get(Object key);
/**
* @see Map#containsKey(Object)
*/
boolean containsKey(Object key);
/**
* @see Map#containsValue(Object)
*/
boolean containsValue(Object value);
/**
* Calls the procedure with each <em>value</em> of the map.
* <pre>
* Set<String> result = UnifiedSet.newSet();
* MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three", 4, "Four");
* map.<b>forEachValue</b>(new CollectionAddProcedure<String>(result));
* Verify.assertSetsEqual(UnifiedSet.newSetWith("One", "Two", "Three", "Four"), result);
* </pre>
*/
void forEachValue(Procedure<? super V> procedure);
/**
* Executes the Procedure for each value of the map and returns {@code this}.
* <p>
* <pre>
* return peopleByCity.<b>tap</b>(person -> LOGGER.info(person.getName()));
* </pre>
*
* @see #forEach(Procedure)
* @since 6.0
*/
@Override
MapIterable<K, V> tap(Procedure<? super V> procedure);
/**
* Calls the {@code procedure} with each <em>key</em> of the map.
* <pre>
* final Collection<Integer> result = new ArrayList<Integer>();
* MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3");
* map.<b>forEachKey</b>(new CollectionAddProcedure<Integer>(result));
* Verify.assertContainsAll(result, 1, 2, 3);
* </pre>
*/
void forEachKey(Procedure<? super K> procedure);
/**
* Calls the {@code procedure} with each <em>key-value</em> pair of the map.
* <pre>
* final Collection<String> collection = new ArrayList<String>();
* MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three");
* map.<b>forEachKeyValue</b>((Integer key, String value) -> collection.add(String.valueOf(key) + value));
* Verify.assertContainsAll(collection, "1One", "2Two", "3Three");
* </pre>
*/
void forEachKeyValue(Procedure2<? super K, ? super V> procedure);
/**
* Return the MapIterable that is obtained by flipping the direction of this map and making the associations
* from value to key.
* <pre>
* MapIterable<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3");
* MapIterable<String, Integer> result = map.flipUniqueValues();
* Assert.assertTrue(result.equals(UnifiedMap.newWithKeysValues("1", 1, "2", 2, "3", 3)));
* </pre>
*
* @throws IllegalStateException if the MapIterable contains duplicate values.
* @since 5.0
*/
MapIterable<V, K> flipUniqueValues();
/**
* Return the value in the Map that corresponds to the specified key, or if there is no value at the key, return the
* result of evaluating the specified Function0.
*/
V getIfAbsent(K key, Function0<? extends V> function);
/**
* Return the value in the Map that corresponds to the specified key, or if there is no value at the key, return {@code value}.
*/
V getIfAbsentValue(K key, V value);
/**
* Return the value in the Map that corresponds to the specified key, or if there is no value at the key, return the
* result of evaluating the specified function and parameter.
*/
<P> V getIfAbsentWith(K key, Function<? super P, ? extends V> function, P parameter);
/**
* If there is a value in the Map that corresponds to the specified key return the result of applying the specified
* Function on the value, otherwise return null.
*/
<A> A ifPresentApply(K key, Function<? super V, ? extends A> function);
/**
* Returns an unmodifiable lazy iterable wrapped around the keySet for the map.
*/
RichIterable<K> keysView();
/**
* Returns an unmodifiable lazy iterable wrapped around the values for the map.
*/
RichIterable<V> valuesView();
/**
* Returns an unmodifiable lazy iterable of key/value pairs wrapped around the entrySet for the map.
*/
RichIterable<Pair<K, V>> keyValuesView();
/**
* Given a map from Domain -> Range return a multimap from Range -> Domain. We chose the name 'flip'
* rather than 'invert' or 'transpose' since this method does not have the property of applying twice
* returns the original.
* <p>
* Since the keys in the input are unique, the values in the output are unique, so the return type should
* be a SetMultimap. However since SetMultimap and SortedSetMultimap don't inherit from one another, SetMultimap
* here does not allow SortedMapIterable to have a SortedSetMultimap return. Thus we compromise and call this
* Multimap, even though all implementations will be a SetMultimap or SortedSetMultimap.
*
* @since 5.0
*/
Multimap<V, K> flip();
/**
* For each key and value of the map the predicate is evaluated, if the result of the evaluation is true,
* that key and value are returned in a new map.
* <p>
* <pre>
* MapIterable<City, Person> selected =
* peopleByCity.select((city, person) -> city.getName().equals("Anytown") && person.getLastName().equals("Smith"));
* </pre>
*/
MapIterable<K, V> select(Predicate2<? super K, ? super V> predicate);
/**
* For each key and value of the map the predicate is evaluated, if the result of the evaluation is false,
* that key and value are returned in a new map.
* <p>
* <pre>
* MapIterable<City, Person> rejected =
* peopleByCity.reject((city, person) -> city.getName().equals("Anytown") && person.getLastName().equals("Smith"));
* </pre>
*/
MapIterable<K, V> reject(Predicate2<? super K, ? super V> predicate);
/**
* For each key and value of the map the function is evaluated. The results of these evaluations are returned in
* a new map. The map returned will use the values projected from the function rather than the original values.
* <p>
* <pre>
* MapIterable<String, String> collected =
* peopleByCity.collect((City city, Person person) -> Pair.of(city.getCountry(), person.getAddress().getCity()));
* </pre>
*/
<K2, V2> MapIterable<K2, V2> collect(Function2<? super K, ? super V, Pair<K2, V2>> function);
/**
* For each key and value of the map the function is evaluated. The results of these evaluations are returned in
* a new map. The map returned will use the values projected from the function rather than the original values.
* <p>
* <pre>
* MapIterable<City, String> collected =
* peopleByCity.collectValues((City city, Person person) -> person.getFirstName() + " " + person.getLastName());
* </pre>
*/
<R> MapIterable<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);
/**
* Return the first key and value of the map for which the predicate evaluates to true when they are given
* as arguments. The predicate will only be evaluated until such pair is found or until all of the keys and
* values of the map have been used as arguments. That is, there may be keys and values of the map that are
* never used as arguments to the predicate. The result is null if predicate does not evaluate to true for
* any key/value combination.
* <p>
* <pre>
* Pair<City, Person> detected =
* peopleByCity.detect((City city, Person person) -> city.getName().equals("Anytown") && person.getLastName().equals("Smith"));
* </pre>
*/
Pair<K, V> detect(Predicate2<? super K, ? super V> predicate);
/**
* Return the first key and value of the map as an Optional for which the predicate evaluates to true when
* they are given as arguments. The predicate will only be evaluated until such pair is found or until all
* of the keys and values of the map have been used as arguments. That is, there may be keys and values of
* the map that are never used as arguments to the predicate.
* <p>
* <pre>
* Optional<Pair<City, Person>> detected =
* peopleByCity.detectOptional((city, person)
* -> city.getName().equals("Anytown") && person.getLastName().equals("Smith"));
* </pre>
*/
Optional<Pair<K, V>> detectOptional(Predicate2<? super K, ? super V> predicate);
/**
* Follows the same general contract as {@link Map#equals(Object)}.
*/
@Override
boolean equals(Object o);
/**
* Follows the same general contract as {@link Map#hashCode()}.
*/
@Override
int hashCode();
/**
* Returns a string with the keys and values of this map separated by commas with spaces and
* enclosed in curly braces. Each key and value is separated by an equals sign.
* <p>
* <pre>
* Assert.assertEquals("{1=1, 2=2, 3=3}", Maps.mutable.with(1, 1, 2, 2, 3, 3).toString());
* </pre>
* @see java.util.AbstractMap#toString()
*
* @return a string representation of this MapIterable
*/
@Override
String toString();
ImmutableMapIterable<K, V> toImmutable();
/**
* @since 9.0
*/
default Stream<V> stream()
{
return StreamSupport.stream(this.spliterator(), false);
}
/**
* @since 9.0
*/
default Stream<V> parallelStream()
{
return StreamSupport.stream(this.spliterator(), true);
}
/**
* @since 9.0
*/
@Override
default Spliterator<V> spliterator()
{
return Spliterators.spliterator(this.iterator(), (long) this.size(), 0);
}
}