/
SARLMapExtensions.java
506 lines (451 loc) · 15.5 KB
/
SARLMapExtensions.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2017 the original authors or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sarl.lang.scoping.batch;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.eclipse.xtext.xbase.lib.Inline;
import org.eclipse.xtext.xbase.lib.Pair;
import org.eclipse.xtext.xbase.lib.Pure;
/** This is an extension library for {@link Map maps} in the SARL language.
*
* <p>This extension is provided for the elements that are not (yet) provided
* by the Xbase API.
* The enhancement of Xbase is asked in <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=454319">this issue</a>.
*
* <p>FIXME: Remove if Xbase or Xtend are providing these functions.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
@GwtCompatible
public final class SARLMapExtensions {
private SARLMapExtensions() {
//
}
/** Add the given pair into the map.
*
* <p>If the pair key already exists in the map, its value is replaced
* by the value in the pair, and the old value in the map is returned.
*
* @param <K> - type of the map keys.
* @param <V> - type of the map values.
* @param map - the map to update.
* @param entry - the entry (key, value) to add into the map.
* @return the value previously associated to the key, or <code>null</code>
* if the key was not present in the map before the addition.
*/
@Inline(value = "$1.put($2.getKey(), $2.getValue())", statementExpression = true)
public static <K, V> V operator_add(Map<K, V> map, Pair<? extends K, ? extends V> entry) {
return map.put(entry.getKey(), entry.getValue());
}
/** Add the given entries of the input map into the output map.
*
* <p>If a key in the inputMap already exists in the outputMap, its value is
* replaced in the outputMap by the value from the inputMap.
*
* @param <K> - type of the map keys.
* @param <V> - type of the map values.
* @param outputMap - the map to update.
* @param inputMap - the entries to add.
*/
@Inline(value = "$1.putAll($2)", statementExpression = true)
public static <K, V> void operator_add(Map<K, V> outputMap, Map<? extends K, ? extends V> inputMap) {
outputMap.putAll(inputMap);
}
/** Add the given pair to a given map for obtaining a new map.
*
* <p>The replied map is a view on the given map. It means that any change
* in the original map is reflected to the result of this operation.
*
* <p>Even if the key of the right operand exists in the left operand, the value in the right operand is preferred.
*
* @param <K> - type of the map keys.
* @param <V> - type of the map values.
* @param left - the map to consider.
* @param right - the entry (key, value) to add into the map.
* @return an immutable map with the content of the map and with the given entry.
* @throws IllegalArgumentException - when the right operand key exists in the left operand.
*/
@Pure
@Inline(value = "SARLMapExtensions.union($1, Collections.singletonMap($2.getKey(), $2.getValue()))",
imported = { SARLMapExtensions.class, Collections.class })
public static <K, V> Map<K, V> operator_plus(Map<K, V> left, final Pair<? extends K, ? extends V> right) {
return union(left, Collections.singletonMap(right.getKey(), right.getValue()));
}
/** Merge the two maps.
*
* <p>The replied map is a view on the given map. It means that any change
* in the original map is reflected to the result of this operation.
*
* <p>If a key exists in the left and right operands, the value in the right operand is preferred.
*
* @param <K> - type of the map keys.
* @param <V> - type of the map values.
* @param left - the left map.
* @param right - the right map.
* @return a map with the merged contents from the two maps.
* @throws IllegalArgumentException - when a right operand key exists in the left operand.
*/
@Pure
@Inline(value = "SARLMapExtensions.union($1, $2)", imported = SARLMapExtensions.class)
public static <K, V> Map<K, V> operator_plus(Map<K, V> left, Map<? extends K, ? extends V> right) {
return union(left, right);
}
/** Remove a key from the given map.
*
* @param <K> - type of the map keys.
* @param <V> - type of the map values.
* @param map - the map to update.
* @param key - the key to remove.
* @return the removed value, or <code>null</code> if the key was not
* present in the map.
* @since 2.8
*/
@Inline(value = "$1.remove($2)", statementExpression = true)
public static <K, V> V operator_remove(Map<K, V> map, K key) {
return map.remove(key);
}
/** Remove the given pair into the map.
*
* <p>If the given key is inside the map, but is not mapped to the given value, the
* map will not be changed.
*
* @param <K> - type of the map keys.
* @param <V> - type of the map values.
* @param map - the map to update.
* @param entry - the entry (key, value) to remove from the map.
* @return {@code true} if the pair was removed.
*/
@Inline(value = "$1.remove($2.getKey(), $2.getValue())", statementExpression = true)
public static <K, V> boolean operator_remove(Map<K, V> map, Pair<? extends K, ? extends V> entry) {
return map.remove(entry.getKey(), entry.getValue());
}
/** Remove pairs with the given keys from the map.
*
*
* @param <K> - type of the map keys.
* @param <V> - type of the map values.
* @param map - the map to update.
* @param keysToRemove - the keys of the pairs to remove.
*/
public static <K, V> void operator_remove(Map<K, V> map, Iterable<? super K> keysToRemove) {
for (final Object key : keysToRemove) {
map.remove(key);
}
}
/** Remove the given pair from a given map for obtaining a new map.
*
* <p>The replied map is a view on the given map. It means that any change
* in the original map is reflected to the result of this operation.
*
* @param <K> - type of the map keys.
* @param <V> - type of the map values.
* @param left - the map to consider.
* @param right - the entry (key, value) to remove from the map.
* @return an immutable map with the content of the map and with the given entry.
* @throws IllegalArgumentException - when the right operand key exists in the left operand.
*/
@Pure
@Inline(value = "SARLMapExtensions.operator_minus($1, $2.getKey())",
imported = { SARLMapExtensions.class, Collections.class })
public static <K, V> Map<K, V> operator_minus(Map<K, V> left, final Pair<? extends K, ? extends V> right) {
return operator_minus(left, right.getKey());
}
/** Replies the elements of the given map except the pair with the given key.
*
* <p>The replied map is a view on the given map. It means that any change
* in the original map is reflected to the result of this operation.
*
* @param <K> - type of the map keys.
* @param <V> - type of the map values.
* @param map - the map to update.
* @param key - the key to remove.
* @return the map with the content of the map except the key.
*/
@Pure
public static <K, V> Map<K, V> operator_minus(Map<K, V> map, final K key) {
return Maps.filterKeys(map, new Predicate<K>() {
@Override
public boolean apply(K input) {
return !Objects.equal(input, key);
}
});
}
/** Replies the elements of the left map without the pairs in the right map.
*
* <p>The difference is an immutable
* snapshot of the state of the maps at the time this method is called. It
* will never change, even if the maps change at a later time.
*
* <p>Since this method uses {@code HashMap} instances internally, the keys of
* the supplied maps must be well-behaved with respect to
* {@link Object#equals} and {@link Object#hashCode}.
*
* @param <K> - type of the map keys.
* @param <V> - type of the map values.
* @param left - the map to update.
* @param right - the pairs to remove.
* @return the map with the content of the left map except the pairs of the right map.
*/
@Pure
@Inline(value = "SARLMapExtensions.operator_minus($0, $1.keySet())", imported = SARLMapExtensions.class)
public static <K, V> Map<K, V> operator_minus(Map<K, V> left, Map<? extends K, ? extends V> right) {
return operator_minus(left, right.keySet());
}
/** Replies the elements of the given map except the pairs with the given keys.
*
* <p>The replied map is a view on the given map. It means that any change
* in the original map is reflected to the result of this operation.
*
* @param <K> - type of the map keys.
* @param <V> - type of the map values.
* @param map - the map to update.
* @param keys - the keys of the pairs to remove.
* @return the map with the content of the map except the pairs.
*/
@Pure
public static <K, V> Map<K, V> operator_minus(Map<K, V> map, final Iterable<?> keys) {
return Maps.filterKeys(map, new Predicate<K>() {
@Override
public boolean apply(K input) {
return !Iterables.contains(keys, input);
}
});
}
/** Replies the value associated to the given key in the map.
*
* @param <K> - type of the map keys.
* @param <V> - type of the map values.
* @param map - the map to consider.
* @param key - the key of the value.
* @return the value associated to the key, or <code>null</code> if
* the key was not found.
*/
@Pure
@Inline(value = "$1.get($2)")
public static <K, V> V operator_mappedTo(Map<K, V> map, K key) {
return map.get(key);
}
/** Merge the given maps.
*
* <p>The replied map is a view on the given two maps.
* If a key exsits in the two maps, the replied value is the value of the right operand.
*
* <p>The replied map is unmodifiable.
*
* @param <K> - type of the map keys.
* @param <V> - type of the map values.
* @param left - the left map.
* @param right - the right map.
* @return a map with the merged contents from the two maps.
*/
public static <K, V> Map<K, V> union(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
return new MergingMap<>(left, right);
}
/** Map implementation that is merging two maps.
*
* @param <K> the type of the keys.
* @param <V> the type of the values in the maps.
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
private static class MergingMap<K, V> implements Map<K, V> {
private final Map<? extends K, ? extends V> left;
private final Map<? extends K, ? extends V> filteredLeft;
private final Map<? extends K, ? extends V> right;
/** Construct the wrapping map.
*
* @param left - the left operand to merge.
* @param right - the right operand to merge.
*/
MergingMap(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
this.left = left;
this.right = right;
this.filteredLeft = Maps.filterKeys(this.left, new Predicate<K>() {
@SuppressWarnings("synthetic-access")
@Override
public boolean apply(K input) {
return !MergingMap.this.right.containsKey(input);
}
});
}
@Override
public int size() {
return this.right.size() + this.filteredLeft.size();
}
@Override
public boolean isEmpty() {
return this.right.isEmpty() && this.left.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return this.right.containsKey(key) || this.left.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
if (this.right.containsValue(value)) {
return true;
}
return this.filteredLeft.containsValue(value);
}
@Override
public V get(Object key) {
if (this.right.containsKey(key)) {
return this.right.get(key);
}
return this.left.get(key);
}
@Override
public V put(K key, V value) {
throw new UnsupportedOperationException();
}
@Override
public V remove(Object key) {
throw new UnsupportedOperationException();
}
@Override
public void putAll(Map<? extends K, ? extends V> map) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public Set<K> keySet() {
return Sets.union(this.right.keySet(), this.filteredLeft.keySet());
}
@Override
public Collection<V> values() {
return new MergingCollection();
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Set<Entry<K, V>> entrySet() {
final Set ks1 = this.right.entrySet();
final Set ks2 = this.filteredLeft.entrySet();
return Sets.union(ks1, ks2);
}
/** Implementation of the collection of values in a map
* that is representing the merging of two maps.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
@SuppressWarnings("synthetic-access")
private class MergingCollection implements Collection<V> {
MergingCollection() {
//
}
@Override
public int size() {
return MergingMap.this.size();
}
@Override
public boolean isEmpty() {
return MergingMap.this.isEmpty();
}
@Override
public boolean contains(Object object) {
return MergingMap.this.containsValue(object);
}
@Override
public Iterator<V> iterator() {
return Iterators.concat(
MergingMap.this.right.values().iterator(),
MergingMap.this.filteredLeft.values().iterator());
}
@Override
public Object[] toArray() {
final Object[] tab = new Object[MergingMap.this.size()];
final Iterator<?> iterator = iterator();
for (int i = 0; i < tab.length; ++i) {
tab[i] = iterator.next();
}
return tab;
}
@SuppressWarnings("unchecked")
@Override
public <T> T[] toArray(T[] output) {
T[] tab = output;
final int size = MergingMap.this.size();
if (tab == null || tab.length < size) {
tab = (T[]) Array.newInstance(Object.class, size);
}
final Iterator<?> iterator = iterator();
for (int i = 0; i < tab.length; ++i) {
tab[i] = (T) iterator.next();
}
return tab;
}
@Override
public boolean containsAll(Collection<?> collection) {
for (final Object o : collection) {
if (!MergingMap.this.containsValue(o)) {
return false;
}
}
return true;
}
@Override
public boolean add(V element) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object object) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends V> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
}
}
}