12
12
import java .util .LinkedHashMap ;
13
13
import java .util .LinkedHashSet ;
14
14
import java .util .Map ;
15
+ import java .util .Map .Entry ;
15
16
import java .util .Set ;
16
17
import java .util .function .BiConsumer ;
17
18
import java .util .stream .Stream ;
20
21
import static java .util .Collections .unmodifiableCollection ;
21
22
import static java .util .Collections .unmodifiableSet ;
22
23
23
- public class AttributeMap <E > implements Map < Attribute , E > {
24
+ public class AttributeMap <E > {
24
25
25
26
static class AttributeWrapper {
26
27
@@ -144,14 +145,13 @@ public String toString() {
144
145
private static final AttributeMap EMPTY = new AttributeMap <>();
145
146
146
147
@ SuppressWarnings ("unchecked" )
147
- public static final <E > AttributeMap <E > emptyAttributeMap () {
148
+ public static <E > AttributeMap <E > emptyAttributeMap () {
148
149
return EMPTY ;
149
150
}
150
151
151
152
private final Map <AttributeWrapper , E > delegate ;
152
153
private Set <Attribute > keySet = null ;
153
154
private Collection <E > values = null ;
154
- private Set <Entry <Attribute , E >> entrySet = null ;
155
155
156
156
public AttributeMap () {
157
157
delegate = new LinkedHashMap <>();
@@ -161,24 +161,24 @@ public AttributeMap(Attribute key, E value) {
161
161
delegate = singletonMap (new AttributeWrapper (key ), value );
162
162
}
163
163
164
- void add (Attribute key , E value ) {
164
+ protected void add (Attribute key , E value ) {
165
165
delegate .put (new AttributeWrapper (key ), value );
166
166
}
167
167
168
168
// a set from a collection of sets without (too much) copying
169
- void addAll (AttributeMap <E > other ) {
169
+ protected void addAll (AttributeMap <E > other ) {
170
170
delegate .putAll (other .delegate );
171
171
}
172
-
173
- public AttributeMap <E > combine (AttributeMap <E > other ) {
172
+
173
+ AttributeMap <E > combine (AttributeMap <E > other ) {
174
174
AttributeMap <E > combine = new AttributeMap <>();
175
175
combine .addAll (this );
176
176
combine .addAll (other );
177
177
178
178
return combine ;
179
179
}
180
180
181
- public AttributeMap <E > subtract (AttributeMap <E > other ) {
181
+ AttributeMap <E > subtract (AttributeMap <E > other ) {
182
182
AttributeMap <E > diff = new AttributeMap <>();
183
183
for (Entry <AttributeWrapper , E > entry : this .delegate .entrySet ()) {
184
184
if (other .delegate .containsKey (entry .getKey ()) == false ) {
@@ -189,7 +189,7 @@ public AttributeMap<E> subtract(AttributeMap<E> other) {
189
189
return diff ;
190
190
}
191
191
192
- public AttributeMap <E > intersect (AttributeMap <E > other ) {
192
+ AttributeMap <E > intersect (AttributeMap <E > other ) {
193
193
AttributeMap <E > smaller = (other .size () > size () ? this : other );
194
194
AttributeMap <E > larger = (smaller == this ? other : this );
195
195
@@ -203,7 +203,7 @@ public AttributeMap<E> intersect(AttributeMap<E> other) {
203
203
return intersect ;
204
204
}
205
205
206
- public boolean subsetOf (AttributeMap <E > other ) {
206
+ boolean subsetOf (AttributeMap <E > other ) {
207
207
if (this .size () > other .size ()) {
208
208
return false ;
209
209
}
@@ -216,7 +216,7 @@ public boolean subsetOf(AttributeMap<E> other) {
216
216
return true ;
217
217
}
218
218
219
- public Set <String > attributeNames () {
219
+ Set <String > attributeNames () {
220
220
Set <String > s = new LinkedHashSet <>(size ());
221
221
222
222
for (AttributeWrapper aw : delegate .keySet ()) {
@@ -225,67 +225,22 @@ public Set<String> attributeNames() {
225
225
return s ;
226
226
}
227
227
228
- @ Override
229
228
public int size () {
230
229
return delegate .size ();
231
230
}
232
231
233
- @ Override
234
232
public boolean isEmpty () {
235
233
return delegate .isEmpty ();
236
234
}
237
-
238
- @ Override
239
- public boolean containsKey (Object key ) {
240
- if (key instanceof NamedExpression ) {
241
- return delegate .keySet ().contains (new AttributeWrapper (((NamedExpression ) key ).toAttribute ()));
242
- }
243
- return false ;
244
- }
245
-
246
- @ Override
247
- public boolean containsValue (Object value ) {
248
- return delegate .values ().contains (value );
249
- }
250
-
251
- @ Override
252
- public E get (Object key ) {
235
+
236
+ public E getOrDefault (Object key , E defaultValue ) {
253
237
if (key instanceof NamedExpression ) {
254
- return delegate .get (new AttributeWrapper (((NamedExpression ) key ).toAttribute ()));
238
+ return delegate .getOrDefault (new AttributeWrapper (((NamedExpression ) key ).toAttribute ()), defaultValue );
255
239
}
256
- return null ;
257
- }
258
-
259
- @ Override
260
- public E getOrDefault (Object key , E defaultValue ) {
261
- E e ;
262
- return (((e = get (key )) != null ) || containsKey (key ))
263
- ? e
264
- : defaultValue ;
265
- }
266
-
267
- @ Override
268
- public E put (Attribute key , E value ) {
269
- throw new UnsupportedOperationException ();
240
+ return defaultValue ;
270
241
}
271
242
272
- @ Override
273
- public E remove (Object key ) {
274
- throw new UnsupportedOperationException ();
275
- }
276
-
277
- @ Override
278
- public void putAll (Map <? extends Attribute , ? extends E > m ) {
279
- throw new UnsupportedOperationException ();
280
- }
281
-
282
- @ Override
283
- public void clear () {
284
- throw new UnsupportedOperationException ();
285
- }
286
-
287
- @ Override
288
- public Set <Attribute > keySet () {
243
+ protected Set <Attribute > keySet () {
289
244
if (keySet == null ) {
290
245
keySet = new UnwrappingSet <>(delegate .keySet ()) {
291
246
@ Override
@@ -296,44 +251,14 @@ protected Attribute unwrap(AttributeWrapper next) {
296
251
}
297
252
return keySet ;
298
253
}
299
-
300
- @ Override
301
- public Collection <E > values () {
254
+
255
+ protected Collection <E > values () {
302
256
if (values == null ) {
303
257
values = unmodifiableCollection (delegate .values ());
304
258
}
305
259
return values ;
306
260
}
307
261
308
- @ Override
309
- public Set <Entry <Attribute , E >> entrySet () {
310
- if (entrySet == null ) {
311
- entrySet = new UnwrappingSet <>(delegate .entrySet ()) {
312
- @ Override
313
- protected Entry <Attribute , E > unwrap (final Entry <AttributeWrapper , E > next ) {
314
- return new Entry <>() {
315
- @ Override
316
- public Attribute getKey () {
317
- return next .getKey ().attr ;
318
- }
319
-
320
- @ Override
321
- public E getValue () {
322
- return next .getValue ();
323
- }
324
-
325
- @ Override
326
- public E setValue (E value ) {
327
- throw new UnsupportedOperationException ();
328
- }
329
- };
330
- }
331
- };
332
- }
333
- return entrySet ;
334
- }
335
-
336
- @ Override
337
262
public void forEach (BiConsumer <? super Attribute , ? super E > action ) {
338
263
delegate .forEach ((k , v ) -> action .accept (k .attr , v ));
339
264
}
0 commit comments