Skip to content
Snippets Groups Projects
Commit d6611357 authored by Johannes Mey's avatar Johannes Mey
Browse files

extract helper class to own aspect

parent 2184c802
No related branches found
No related tags found
No related merge requests found
aspect IdentityTupleSet {
/**
* Set that contains either one or two unique objects. The objects are
* compared with reference equality.
*/
class IdentityTupleSet<E> implements Set<E> {
final E a, b;
public IdentityTupleSet(E a, E b) {
this.a = a;
this.b = b;
}
@Override
public boolean add(E e) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends E> e) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public boolean contains(Object o) {
return o == a || o == b;
}
@Override
public boolean containsAll(Collection<?> c) {
for (Object o : c) {
if (!contains(o)) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
return a.hashCode() + b.hashCode();
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
int index = 0;
@Override
public boolean hasNext() {
return (a == b && index < 1)
|| (a != b && index < 2);
}
@Override
public E next() {
return ++index == 1 ? a : b;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
@Override
public int size() {
return a == b ? 1 : 2;
}
@Override
public Object[] toArray() {
return new Object[] { a, b };
}
@Override
public <T> T[] toArray(T[] array) {
array[0] = (T) a;
array[1] = (T) b;
return array;
}
@Override
public String toString() {
if (a == b) {
return "[" + a + "]";
} else {
return "[" + a + ", " + b + "]";
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment