Skip to content

Commit

Permalink
Removes the Map and Set interface implementations from the Attribute{…
Browse files Browse the repository at this point in the history
…Map,Set}
  • Loading branch information
Andras Palinkas committed Mar 5, 2021
1 parent 2e8e058 commit fe814ec
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Stream;
Expand All @@ -20,7 +21,7 @@
import static java.util.Collections.unmodifiableCollection;
import static java.util.Collections.unmodifiableSet;

public class AttributeMap<E> implements Map<Attribute, E> {
public class AttributeMap<E> {

static class AttributeWrapper {

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

@SuppressWarnings("unchecked")
public static final <E> AttributeMap<E> emptyAttributeMap() {
public static <E> AttributeMap<E> emptyAttributeMap() {
return EMPTY;
}

private final Map<AttributeWrapper, E> delegate;
private Set<Attribute> keySet = null;
private Collection<E> values = null;
private Set<Entry<Attribute, E>> entrySet = null;

public AttributeMap() {
delegate = new LinkedHashMap<>();
Expand All @@ -161,24 +161,24 @@ public AttributeMap(Attribute key, E value) {
delegate = singletonMap(new AttributeWrapper(key), value);
}

void add(Attribute key, E value) {
protected void add(Attribute key, E value) {
delegate.put(new AttributeWrapper(key), value);
}

// a set from a collection of sets without (too much) copying
void addAll(AttributeMap<E> other) {
protected void addAll(AttributeMap<E> other) {
delegate.putAll(other.delegate);
}

public AttributeMap<E> combine(AttributeMap<E> other) {
AttributeMap<E> combine(AttributeMap<E> other) {
AttributeMap<E> combine = new AttributeMap<>();
combine.addAll(this);
combine.addAll(other);

return combine;
}

public AttributeMap<E> subtract(AttributeMap<E> other) {
AttributeMap<E> subtract(AttributeMap<E> other) {
AttributeMap<E> diff = new AttributeMap<>();
for (Entry<AttributeWrapper, E> entry : this.delegate.entrySet()) {
if (other.delegate.containsKey(entry.getKey()) == false) {
Expand All @@ -189,7 +189,7 @@ public AttributeMap<E> subtract(AttributeMap<E> other) {
return diff;
}

public AttributeMap<E> intersect(AttributeMap<E> other) {
AttributeMap<E> intersect(AttributeMap<E> other) {
AttributeMap<E> smaller = (other.size() > size() ? this : other);
AttributeMap<E> larger = (smaller == this ? other : this);

Expand All @@ -203,7 +203,7 @@ public AttributeMap<E> intersect(AttributeMap<E> other) {
return intersect;
}

public boolean subsetOf(AttributeMap<E> other) {
boolean subsetOf(AttributeMap<E> other) {
if (this.size() > other.size()) {
return false;
}
Expand All @@ -216,7 +216,7 @@ public boolean subsetOf(AttributeMap<E> other) {
return true;
}

public Set<String> attributeNames() {
Set<String> attributeNames() {
Set<String> s = new LinkedHashSet<>(size());

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

@Override
public int size() {
return delegate.size();
}

@Override
public boolean isEmpty() {
return delegate.isEmpty();
}

@Override
public boolean containsKey(Object key) {
if (key instanceof NamedExpression) {
return delegate.keySet().contains(new AttributeWrapper(((NamedExpression) key).toAttribute()));
}
return false;
}

@Override
public boolean containsValue(Object value) {
return delegate.values().contains(value);
}

@Override
public E get(Object key) {

public E getOrDefault(Object key, E defaultValue) {
if (key instanceof NamedExpression) {
return delegate.get(new AttributeWrapper(((NamedExpression) key).toAttribute()));
return delegate.getOrDefault(new AttributeWrapper(((NamedExpression) key).toAttribute()), defaultValue);
}
return null;
}

@Override
public E getOrDefault(Object key, E defaultValue) {
E e;
return (((e = get(key)) != null) || containsKey(key))
? e
: defaultValue;
}

@Override
public E put(Attribute key, E value) {
throw new UnsupportedOperationException();
return defaultValue;
}

@Override
public E remove(Object key) {
throw new UnsupportedOperationException();
}

@Override
public void putAll(Map<? extends Attribute, ? extends E> m) {
throw new UnsupportedOperationException();
}

@Override
public void clear() {
throw new UnsupportedOperationException();
}

@Override
public Set<Attribute> keySet() {
protected Set<Attribute> keySet() {
if (keySet == null) {
keySet = new UnwrappingSet<>(delegate.keySet()) {
@Override
Expand All @@ -296,44 +251,14 @@ protected Attribute unwrap(AttributeWrapper next) {
}
return keySet;
}

@Override
public Collection<E> values() {

protected Collection<E> values() {
if (values == null) {
values = unmodifiableCollection(delegate.values());
}
return values;
}

@Override
public Set<Entry<Attribute, E>> entrySet() {
if (entrySet == null) {
entrySet = new UnwrappingSet<>(delegate.entrySet()) {
@Override
protected Entry<Attribute, E> unwrap(final Entry<AttributeWrapper, E> next) {
return new Entry<>() {
@Override
public Attribute getKey() {
return next.getKey().attr;
}

@Override
public E getValue() {
return next.getValue();
}

@Override
public E setValue(E value) {
throw new UnsupportedOperationException();
}
};
}
};
}
return entrySet;
}

@Override
public void forEach(BiConsumer<? super Attribute, ? super E> action) {
delegate.forEach((k, v) -> action.accept(k.attr, v));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
*/
package org.elasticsearch.xpack.ql.expression;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class AttributeSet implements Set<Attribute> {
public class AttributeSet implements Iterable<Attribute> {

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

Expand All @@ -32,8 +31,8 @@ public AttributeSet() {
public AttributeSet(Attribute attr) {
delegate = new AttributeMap<>(attr, PRESENT);
}

public AttributeSet(Collection<? extends Attribute> attr) {
public AttributeSet(List<? extends Attribute> attr) {
if (attr.isEmpty()) {
delegate = EMPTY_DELEGATE;
}
Expand All @@ -52,7 +51,7 @@ private AttributeSet(AttributeMap<Object> delegate) {

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

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

@Override
public int size() {
return delegate.size();
}

@Override
public boolean isEmpty() {
return delegate.isEmpty();
}

@Override
public boolean contains(Object o) {
return delegate.containsKey(o);
}

@Override
public boolean containsAll(Collection<?> c) {
for (Object o : c) {
if (delegate.containsKey(o) == false) {
return false;
}
}
return true;
}

@Override
public Iterator<Attribute> iterator() {
return delegate.keySet().iterator();
}

@Override
public Object[] toArray() {
return delegate.keySet().toArray();
}

@Override
public <T> T[] toArray(T[] a) {
return delegate.keySet().toArray(a);
}

@Override
public boolean add(Attribute e) {
throw new UnsupportedOperationException();
}

@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}

@Override
public boolean addAll(Collection<? extends Attribute> c) {
throw new UnsupportedOperationException();
}

@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}

@Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}

@Override
public void clear() {
throw new UnsupportedOperationException();
}

@Override
public Spliterator<Attribute> spliterator() {
throw new UnsupportedOperationException();
}

@Override
public boolean removeIf(Predicate<? super Attribute> filter) {
throw new UnsupportedOperationException();
}

@Override
public Stream<Attribute> stream() {
return delegate.keySet().stream();
}

@Override
public Stream<Attribute> parallelStream() {
return delegate.keySet().parallelStream();
}

@Override
public boolean equals(Object o) {
return delegate.equals(o);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,6 @@ public static List<Attribute> asAttributes(List<? extends NamedExpression> named
return list;
}

public static AttributeMap<Expression> asAttributeMap(List<? extends NamedExpression> named) {
if (named.isEmpty()) {
return AttributeMap.emptyAttributeMap();
}

AttributeMap<Expression> map = new AttributeMap<>();
for (NamedExpression exp : named) {
map.add(exp.toAttribute(), exp);
}
return map;
}

public static boolean anyMatch(List<? extends Expression> exps, Predicate<? super Expression> predicate) {
for (Expression exp : exps) {
if (exp.anyMatch(predicate)) {
Expand Down Expand Up @@ -174,7 +162,7 @@ public static List<Tuple<Attribute, Expression>> aliases(List<? extends NamedExp
return aliases;
}

public static boolean hasReferenceAttribute(Collection<Attribute> output) {
public static boolean hasReferenceAttribute(Iterable<Attribute> output) {
for (Attribute attribute : output) {
if (attribute instanceof ReferenceAttribute) {
return true;
Expand Down
Loading

0 comments on commit fe814ec

Please sign in to comment.