From d6611357bdf519329c0cb550e7b25964f8e6d1c6 Mon Sep 17 00:00:00 2001 From: Johannes Mey <johannes.mey@tu-dresden.de> Date: Tue, 17 Dec 2019 13:33:00 +0100 Subject: [PATCH] extract helper class to own aspect --- .../src/main/jastadd/IdentityTupleSet.jadd | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 reusablecfg/src/main/jastadd/IdentityTupleSet.jadd diff --git a/reusablecfg/src/main/jastadd/IdentityTupleSet.jadd b/reusablecfg/src/main/jastadd/IdentityTupleSet.jadd new file mode 100644 index 0000000..02ed2d1 --- /dev/null +++ b/reusablecfg/src/main/jastadd/IdentityTupleSet.jadd @@ -0,0 +1,102 @@ +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 + "]"; + } + } + } +} -- GitLab