1 package net.mograsim.logic.model.editor.util;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.Comparator;
7 import java.util.Iterator;
11 * Similar to a SortedSet, except it is allowed for multiple elements to have the same priority (<code>c.compare(e1, e2) == 0</code> is
12 * allowed to be true for two different elements e1 and e2). However, to elements are not allowed to be equal according to
13 * <code>Object.equals(Object o)</code>.
15 * @author Fabian Stemmler
17 * @param <T> the type of elements in this list
19 public class PrioritySet<T> implements Set<T>
21 private ArrayList<T> list;
22 private Comparator<T> c;
24 public PrioritySet(Comparator<T> c)
27 list = new ArrayList<>();
30 @SuppressWarnings("unchecked")
33 this((e1, e2) -> ((Comparable<T>) e1).compareTo(e2));
36 public void setComparator(Comparator<T> c)
43 public int size() { return list.size(); }
45 public boolean isEmpty() { return list.isEmpty(); }
47 public boolean contains(Object o) { return list.isEmpty(); }
49 public Iterator<T> iterator() { return list.iterator(); }
51 public Object[] toArray() { return list.toArray(); }
53 public <E> E[] toArray(E[] a) { return list.toArray(a); }
55 public boolean remove(Object o) { return list.remove(o); }
57 public boolean containsAll(Collection<?> c) { return list.containsAll(c); }
59 public boolean removeAll(Collection<?> c) { return list.removeAll(c); }
61 public boolean retainAll(Collection<?> c) { return list.retainAll(c); }
63 public void clear() { list.clear(); }
67 public boolean add(T e)
74 int index = Collections.binarySearch(list, e, c);
79 if (list.get(index).equals(e))
88 public boolean addAll(Collection<? extends T> c)
90 return c.stream().map(e -> add(e)).reduce(false, (a, b) -> a || b);
94 public String toString()
96 return list.toString();