X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.logic.model.editor%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Feditor%2Futil%2FPrioritySet.java;fp=plugins%2Fnet.mograsim.logic.model.editor%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Feditor%2Futil%2FPrioritySet.java;h=253dfdd373b1da6e5814e3597628e12aa448d489;hb=7d05144c25daa53e60fc9ed9fd503546a86567f8;hp=0000000000000000000000000000000000000000;hpb=8bed58cd47f4e53a0a83e066d38864aa6875502f;p=Mograsim.git
diff --git a/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/util/PrioritySet.java b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/util/PrioritySet.java
new file mode 100644
index 00000000..253dfdd3
--- /dev/null
+++ b/plugins/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/util/PrioritySet.java
@@ -0,0 +1,98 @@
+package net.mograsim.logic.model.editor.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.Set;
+
+/**
+ * Similar to a SortedSet, except it is allowed for multiple elements to have the same priority (c.compare(e1, e2) == 0
is
+ * allowed to be true for two different elements e1 and e2). However, to elements are not allowed to be equal according to
+ * Object.equals(Object o)
.
+ *
+ * @author Fabian Stemmler
+ *
+ * @param the type of elements in this list
+ */
+public class PrioritySet implements Set
+{
+ private ArrayList list;
+ private Comparator c;
+
+ public PrioritySet(Comparator c)
+ {
+ setComparator(c);
+ list = new ArrayList<>();
+ }
+
+ @SuppressWarnings("unchecked")
+ public PrioritySet()
+ {
+ this((e1, e2) -> ((Comparable) e1).compareTo(e2));
+ }
+
+ public void setComparator(Comparator c)
+ {
+ this.c = c;
+ }
+
+ //@formatter:off
+ @Override
+ public int size() { return list.size(); }
+ @Override
+ public boolean isEmpty() { return list.isEmpty(); }
+ @Override
+ public boolean contains(Object o) { return list.isEmpty(); }
+ @Override
+ public Iterator iterator() { return list.iterator(); }
+ @Override
+ public Object[] toArray() { return list.toArray(); }
+ @Override
+ public E[] toArray(E[] a) { return list.toArray(a); }
+ @Override
+ public boolean remove(Object o) { return list.remove(o); }
+ @Override
+ public boolean containsAll(Collection> c) { return list.containsAll(c); }
+ @Override
+ public boolean removeAll(Collection> c) { return list.removeAll(c); }
+ @Override
+ public boolean retainAll(Collection> c) { return list.retainAll(c); }
+ @Override
+ public void clear() { list.clear(); }
+ //@formatter:on
+
+ @Override
+ public boolean add(T e)
+ {
+ if (isEmpty())
+ {
+ list.add(e);
+ return true;
+ }
+ int index = Collections.binarySearch(list, e, c);
+ if (index < 0)
+ index = -index - 1;
+ if (index < size())
+ {
+ if (list.get(index).equals(e))
+ return false;
+ list.add(index, e);
+ } else
+ list.add(e);
+ return true;
+ }
+
+ @Override
+ public boolean addAll(Collection extends T> c)
+ {
+ return c.stream().map(e -> add(e)).reduce(false, (a, b) -> a || b);
+ }
+
+ @Override
+ public String toString()
+ {
+ return list.toString();
+ }
+}