The final restructured version for automatic build using maven tycho
[Mograsim.git] / plugins / net.mograsim.logic.model.editor / src / net / mograsim / logic / model / editor / util / PrioritySet.java
1 package net.mograsim.logic.model.editor.util;
2
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;
8 import java.util.Set;
9
10 /**
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>.
14  * 
15  * @author Fabian Stemmler
16  *
17  * @param <T> the type of elements in this list
18  */
19 public class PrioritySet<T> implements Set<T>
20 {
21         private ArrayList<T> list;
22         private Comparator<T> c;
23
24         public PrioritySet(Comparator<T> c)
25         {
26                 setComparator(c);
27                 list = new ArrayList<>();
28         }
29
30         @SuppressWarnings("unchecked")
31         public PrioritySet()
32         {
33                 this((e1, e2) -> ((Comparable<T>) e1).compareTo(e2));
34         }
35
36         public void setComparator(Comparator<T> c)
37         {
38                 this.c = c;
39         }
40
41         //@formatter:off
42         @Override
43         public int size() { return list.size(); }
44         @Override
45         public boolean isEmpty() { return list.isEmpty(); }
46         @Override
47         public boolean contains(Object o) { return list.isEmpty(); }
48         @Override
49         public Iterator<T> iterator() { return list.iterator(); }
50         @Override
51         public Object[] toArray() { return list.toArray(); }
52         @Override
53         public <E> E[] toArray(E[] a) { return list.toArray(a); }
54         @Override
55         public boolean remove(Object o) { return list.remove(o); }
56         @Override
57         public boolean containsAll(Collection<?> c) { return list.containsAll(c); }
58         @Override
59         public boolean removeAll(Collection<?> c) { return list.removeAll(c); }
60         @Override
61         public boolean retainAll(Collection<?> c) { return list.retainAll(c); }
62         @Override
63         public void clear() { list.clear(); }
64         //@formatter:on
65
66         @Override
67         public boolean add(T e)
68         {
69                 if (isEmpty())
70                 {
71                         list.add(e);
72                         return true;
73                 }
74                 int index = Collections.binarySearch(list, e, c);
75                 if (index < 0)
76                         index = -index - 1;
77                 if (index < size())
78                 {
79                         if (list.get(index).equals(e))
80                                 return false;
81                         list.add(index, e);
82                 } else
83                         list.add(e);
84                 return true;
85         }
86
87         @Override
88         public boolean addAll(Collection<? extends T> c)
89         {
90                 return c.stream().map(e -> add(e)).reduce(false, (a, b) -> a || b);
91         }
92
93         @Override
94         public String toString()
95         {
96                 return list.toString();
97         }
98 }