| 14 | import avian.Cell; |
| 15 | |
| 16 | public class TreeSet<T> extends AbstractSet<T> implements Collection<T> { |
| 17 | private PersistentSet<Cell<T>> set; |
| 18 | |
| 19 | public TreeSet(final Comparator<T> comparator) { |
| 20 | set = new PersistentSet(new Comparator<Cell<T>>() { |
| 21 | public int compare(Cell<T> a, Cell<T> b) { |
| 22 | return comparator.compare(a.value, b.value); |
| 23 | } |
| 24 | }); |
| 25 | } |
| 26 | |
| 27 | public TreeSet() { |
| 28 | this(new Comparator<T>() { |
| 29 | public int compare(T a, T b) { |
| 30 | return ((Comparable) a).compareTo(b); |
| 31 | } |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | public TreeSet(Collection<? extends T> collection) { |
| 36 | this(); |
| 37 | |
| 38 | for (T item: collection) { |
| 39 | add(item); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public T first() { |
| 44 | if (isEmpty()) throw new NoSuchElementException(); |
| 45 | |
| 46 | return set.first().value().value; |
| 47 | } |
| 48 | |
| 49 | public T last() { |
| 50 | if (isEmpty()) throw new NoSuchElementException(); |
| 51 | |
| 52 | return set.last().value().value; |
| 53 | } |
| 54 | |
| 55 | public Iterator<T> iterator() { |
| 56 | return new MyIterator<T>(set.first()); |
| 57 | } |
| 58 | |
| 59 | public Iterator<T> descendingIterator() { |
| 60 | return new MyIterator<T>(set.last(), true); |
| 61 | } |
| 62 | |
| 63 | public String toString() { |
| 64 | return avian.Data.toString(this); |
| 65 | } |
| 66 | |
| 67 | public boolean add(T value) { |
| 68 | PersistentSet.Path<Cell<T>> p = set.find(new Cell(value, null)); |
| 69 | if (p.fresh()) { |
| 70 | set = p.add(); |
| 71 | return true; |
| 72 | } |
| 73 | return false; |
nothing calls this directly
no outgoing calls
no test coverage detected