Skip to content

Commit fe814ec

Browse files
author
Andras Palinkas
committed
Removes the Map and Set interface implementations from the Attribute{Map,Set}
1 parent 2e8e058 commit fe814ec

File tree

11 files changed

+102
-251
lines changed

11 files changed

+102
-251
lines changed

x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/AttributeMap.java

Lines changed: 18 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.LinkedHashMap;
1313
import java.util.LinkedHashSet;
1414
import java.util.Map;
15+
import java.util.Map.Entry;
1516
import java.util.Set;
1617
import java.util.function.BiConsumer;
1718
import java.util.stream.Stream;
@@ -20,7 +21,7 @@
2021
import static java.util.Collections.unmodifiableCollection;
2122
import static java.util.Collections.unmodifiableSet;
2223

23-
public class AttributeMap<E> implements Map<Attribute, E> {
24+
public class AttributeMap<E> {
2425

2526
static class AttributeWrapper {
2627

@@ -144,14 +145,13 @@ public String toString() {
144145
private static final AttributeMap EMPTY = new AttributeMap<>();
145146

146147
@SuppressWarnings("unchecked")
147-
public static final <E> AttributeMap<E> emptyAttributeMap() {
148+
public static <E> AttributeMap<E> emptyAttributeMap() {
148149
return EMPTY;
149150
}
150151

151152
private final Map<AttributeWrapper, E> delegate;
152153
private Set<Attribute> keySet = null;
153154
private Collection<E> values = null;
154-
private Set<Entry<Attribute, E>> entrySet = null;
155155

156156
public AttributeMap() {
157157
delegate = new LinkedHashMap<>();
@@ -161,24 +161,24 @@ public AttributeMap(Attribute key, E value) {
161161
delegate = singletonMap(new AttributeWrapper(key), value);
162162
}
163163

164-
void add(Attribute key, E value) {
164+
protected void add(Attribute key, E value) {
165165
delegate.put(new AttributeWrapper(key), value);
166166
}
167167

168168
// a set from a collection of sets without (too much) copying
169-
void addAll(AttributeMap<E> other) {
169+
protected void addAll(AttributeMap<E> other) {
170170
delegate.putAll(other.delegate);
171171
}
172-
173-
public AttributeMap<E> combine(AttributeMap<E> other) {
172+
173+
AttributeMap<E> combine(AttributeMap<E> other) {
174174
AttributeMap<E> combine = new AttributeMap<>();
175175
combine.addAll(this);
176176
combine.addAll(other);
177177

178178
return combine;
179179
}
180180

181-
public AttributeMap<E> subtract(AttributeMap<E> other) {
181+
AttributeMap<E> subtract(AttributeMap<E> other) {
182182
AttributeMap<E> diff = new AttributeMap<>();
183183
for (Entry<AttributeWrapper, E> entry : this.delegate.entrySet()) {
184184
if (other.delegate.containsKey(entry.getKey()) == false) {
@@ -189,7 +189,7 @@ public AttributeMap<E> subtract(AttributeMap<E> other) {
189189
return diff;
190190
}
191191

192-
public AttributeMap<E> intersect(AttributeMap<E> other) {
192+
AttributeMap<E> intersect(AttributeMap<E> other) {
193193
AttributeMap<E> smaller = (other.size() > size() ? this : other);
194194
AttributeMap<E> larger = (smaller == this ? other : this);
195195

@@ -203,7 +203,7 @@ public AttributeMap<E> intersect(AttributeMap<E> other) {
203203
return intersect;
204204
}
205205

206-
public boolean subsetOf(AttributeMap<E> other) {
206+
boolean subsetOf(AttributeMap<E> other) {
207207
if (this.size() > other.size()) {
208208
return false;
209209
}
@@ -216,7 +216,7 @@ public boolean subsetOf(AttributeMap<E> other) {
216216
return true;
217217
}
218218

219-
public Set<String> attributeNames() {
219+
Set<String> attributeNames() {
220220
Set<String> s = new LinkedHashSet<>(size());
221221

222222
for (AttributeWrapper aw : delegate.keySet()) {
@@ -225,67 +225,22 @@ public Set<String> attributeNames() {
225225
return s;
226226
}
227227

228-
@Override
229228
public int size() {
230229
return delegate.size();
231230
}
232231

233-
@Override
234232
public boolean isEmpty() {
235233
return delegate.isEmpty();
236234
}
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) {
253237
if (key instanceof NamedExpression) {
254-
return delegate.get(new AttributeWrapper(((NamedExpression) key).toAttribute()));
238+
return delegate.getOrDefault(new AttributeWrapper(((NamedExpression) key).toAttribute()), defaultValue);
255239
}
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;
270241
}
271242

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() {
289244
if (keySet == null) {
290245
keySet = new UnwrappingSet<>(delegate.keySet()) {
291246
@Override
@@ -296,44 +251,14 @@ protected Attribute unwrap(AttributeWrapper next) {
296251
}
297252
return keySet;
298253
}
299-
300-
@Override
301-
public Collection<E> values() {
254+
255+
protected Collection<E> values() {
302256
if (values == null) {
303257
values = unmodifiableCollection(delegate.values());
304258
}
305259
return values;
306260
}
307261

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
337262
public void forEach(BiConsumer<? super Attribute, ? super E> action) {
338263
delegate.forEach((k, v) -> action.accept(k.attr, v));
339264
}

x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/AttributeSet.java

Lines changed: 5 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
*/
77
package org.elasticsearch.xpack.ql.expression;
88

9-
import java.util.Collection;
109
import java.util.Iterator;
10+
import java.util.List;
1111
import java.util.Set;
1212
import java.util.Spliterator;
1313
import java.util.function.Consumer;
14-
import java.util.function.Predicate;
1514
import java.util.stream.Stream;
1615

17-
public class AttributeSet implements Set<Attribute> {
16+
public class AttributeSet implements Iterable<Attribute> {
1817

1918
private static final AttributeMap<Object> EMPTY_DELEGATE = AttributeMap.emptyAttributeMap();
2019

@@ -32,8 +31,8 @@ public AttributeSet() {
3231
public AttributeSet(Attribute attr) {
3332
delegate = new AttributeMap<>(attr, PRESENT);
3433
}
35-
36-
public AttributeSet(Collection<? extends Attribute> attr) {
34+
35+
public AttributeSet(List<? extends Attribute> attr) {
3736
if (attr.isEmpty()) {
3837
delegate = EMPTY_DELEGATE;
3938
}
@@ -52,7 +51,7 @@ private AttributeSet(AttributeMap<Object> delegate) {
5251

5352
// package protected - should be called through Expressions to cheaply create
5453
// a set from a collection of sets without too much copying
55-
void addAll(AttributeSet other) {
54+
protected void addAll(AttributeSet other) {
5655
delegate.addAll(other.delegate);
5756
}
5857

@@ -81,96 +80,28 @@ public void forEach(Consumer<? super Attribute> action) {
8180
delegate.forEach((k, v) -> action.accept(k));
8281
}
8382

84-
@Override
8583
public int size() {
8684
return delegate.size();
8785
}
8886

89-
@Override
9087
public boolean isEmpty() {
9188
return delegate.isEmpty();
9289
}
9390

94-
@Override
95-
public boolean contains(Object o) {
96-
return delegate.containsKey(o);
97-
}
98-
99-
@Override
100-
public boolean containsAll(Collection<?> c) {
101-
for (Object o : c) {
102-
if (delegate.containsKey(o) == false) {
103-
return false;
104-
}
105-
}
106-
return true;
107-
}
108-
10991
@Override
11092
public Iterator<Attribute> iterator() {
11193
return delegate.keySet().iterator();
11294
}
11395

114-
@Override
115-
public Object[] toArray() {
116-
return delegate.keySet().toArray();
117-
}
118-
119-
@Override
120-
public <T> T[] toArray(T[] a) {
121-
return delegate.keySet().toArray(a);
122-
}
123-
124-
@Override
125-
public boolean add(Attribute e) {
126-
throw new UnsupportedOperationException();
127-
}
128-
129-
@Override
130-
public boolean remove(Object o) {
131-
throw new UnsupportedOperationException();
132-
}
133-
134-
@Override
135-
public boolean addAll(Collection<? extends Attribute> c) {
136-
throw new UnsupportedOperationException();
137-
}
138-
139-
@Override
140-
public boolean retainAll(Collection<?> c) {
141-
throw new UnsupportedOperationException();
142-
}
143-
144-
@Override
145-
public boolean removeAll(Collection<?> c) {
146-
throw new UnsupportedOperationException();
147-
}
148-
149-
@Override
150-
public void clear() {
151-
throw new UnsupportedOperationException();
152-
}
153-
15496
@Override
15597
public Spliterator<Attribute> spliterator() {
15698
throw new UnsupportedOperationException();
15799
}
158100

159-
@Override
160-
public boolean removeIf(Predicate<? super Attribute> filter) {
161-
throw new UnsupportedOperationException();
162-
}
163-
164-
@Override
165101
public Stream<Attribute> stream() {
166102
return delegate.keySet().stream();
167103
}
168104

169-
@Override
170-
public Stream<Attribute> parallelStream() {
171-
return delegate.keySet().parallelStream();
172-
}
173-
174105
@Override
175106
public boolean equals(Object o) {
176107
return delegate.equals(o);

x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/Expressions.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,6 @@ public static List<Attribute> asAttributes(List<? extends NamedExpression> named
6363
return list;
6464
}
6565

66-
public static AttributeMap<Expression> asAttributeMap(List<? extends NamedExpression> named) {
67-
if (named.isEmpty()) {
68-
return AttributeMap.emptyAttributeMap();
69-
}
70-
71-
AttributeMap<Expression> map = new AttributeMap<>();
72-
for (NamedExpression exp : named) {
73-
map.add(exp.toAttribute(), exp);
74-
}
75-
return map;
76-
}
77-
7866
public static boolean anyMatch(List<? extends Expression> exps, Predicate<? super Expression> predicate) {
7967
for (Expression exp : exps) {
8068
if (exp.anyMatch(predicate)) {
@@ -174,7 +162,7 @@ public static List<Tuple<Attribute, Expression>> aliases(List<? extends NamedExp
174162
return aliases;
175163
}
176164

177-
public static boolean hasReferenceAttribute(Collection<Attribute> output) {
165+
public static boolean hasReferenceAttribute(Iterable<Attribute> output) {
178166
for (Attribute attribute : output) {
179167
if (attribute instanceof ReferenceAttribute) {
180168
return true;

0 commit comments

Comments
 (0)