/
SARLMapExtensions.java
315 lines (296 loc) · 11.8 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
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2018 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.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
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.Maps;
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>.
*
* @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.
*
* <p>FIXME: Xtext upgrade, No proposal within Xtext. https://github.com/eclipse/xtext-lib/pull/63.
*
* @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.
*
* <p>FIXME: Xtext upgrade, No proposal within Xtext. https://github.com/eclipse/xtext-lib/pull/63.
*
* @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.
*
* <p>FIXME: Xtext upgrade, No proposal within Xtext. https://github.com/eclipse/xtext-lib/pull/63.
*
* @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 = "$3.union($1, $4.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.
*
* <p>FIXME: Xtext upgrade, No proposal within Xtext. https://github.com/eclipse/xtext-lib/pull/63.
*
* @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 = "$3.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.
*
* <p>FIXME: Xtext upgrade, No proposal within Xtext. https://github.com/eclipse/xtext-lib/pull/63.
*
* @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.
*
* <p>FIXME: Xtext upgrade, No proposal within Xtext. https://github.com/eclipse/xtext-lib/pull/63.
*
* @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.
*
* <p>FIXME: Xtext upgrade, No proposal within Xtext. https://github.com/eclipse/xtext-lib/pull/63.
*
* @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>If the given key is inside the map, but is not mapped to the given value, the
* map will not be changed.
*
* <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>FIXME: Xtext upgrade, No proposal within Xtext. https://github.com/eclipse/xtext-lib/pull/63.
*
* @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
public static <K, V> Map<K, V> operator_minus(Map<K, V> left, final Pair<? extends K, ? extends V> right) {
return Maps.filterEntries(left, new Predicate<Entry<K, V>>() {
@Override
public boolean apply(Entry<K, V> input) {
return !Objects.equal(input.getKey(), right.getKey()) || !Objects.equal(input.getValue(), right.getValue());
}
});
}
/** 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.
*
* <p>FIXME: Xtext upgrade, No proposal within Xtext. https://github.com/eclipse/xtext-lib/pull/63.
*
* @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.
* If the pair's values differ from
* the value within the map, the map entry is not removed.
*
* <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}.
*
* <p>FIXME: Xtext upgrade, No proposal within Xtext. https://github.com/eclipse/xtext-lib/pull/63.
*
* @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
public static <K, V> Map<K, V> operator_minus(Map<K, V> left, Map<? extends K, ? extends V> right) {
return Maps.filterEntries(left, new Predicate<Entry<K, V>>() {
@Override
public boolean apply(Entry<K, V> input) {
final V value = right.get(input.getKey());
if (value == null) {
return true;
}
return !Objects.equal(input.getValue(), value);
}
});
}
/** 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.
*
* <p>FIXME: Xtext upgrade, No proposal within Xtext. https://github.com/eclipse/xtext-lib/pull/63.
*
* @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);
}
});
}
/** Merge the given maps.
*
* <p>The replied map is a view on the given two maps.
* If a key exists in the two maps, the replied value is the value of the right operand.
*
* <p>The replied map is unmodifiable.
*
* <p>FIXME: Xtext upgrade, No proposal within Xtext. https://github.com/eclipse/xtext-lib/pull/63.
*
* @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.
*/
@Pure
@Inline(value = "(new $3<$5, $6>($1, $2))", imported = UnmodifiableMergingMapView.class, constantExpression = true)
public static <K, V> Map<K, V> union(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
return new UnmodifiableMergingMapView<>(left, right);
}
}