diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TTreeSet.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TTreeSet.java new file mode 100644 index 000000000..5d8ef7815 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TTreeSet.java @@ -0,0 +1,144 @@ +package org.teavm.classlib.java.util; + +/** + * + * @author Alexey Andreev + */ +public class TTreeSet extends TAbstractSet implements TNavigableSet { + private static final Object VALUE = new Object(); + private TTreeMap map; + + public TTreeSet() { + map = new TTreeMap<>(); + } + + public TTreeSet(TComparator comparator) { + map = new TTreeMap<>(comparator); + } + + public TTreeSet(TCollection coll) { + map = new TTreeMap<>(); + for (TIterator iter = coll.iterator(); iter.hasNext();) { + map.put(iter.next(), VALUE); + } + } + + public TTreeSet(TSortedSet s) { + map = new TTreeMap<>(s.comparator()); + for (TIterator iter = s.iterator(); iter.hasNext();) { + map.put(iter.next(), VALUE); + } + } + + @Override + public int size() { + return map.size(); + } + + @Override + public TIterator iterator() { + return map.keySet().iterator(); + } + + @Override + public void clear() { + map.clear(); + } + + @Override + public boolean add(E e) { + return map.put(e, e) != VALUE; + } + + @Override + public boolean remove(Object o) { + return map.remove(o) == VALUE; + } + + @Override + public TComparator comparator() { + return map.comparator(); + } + + @Override + public TSortedSet subSet(E fromElement, E toElement) { + return map.subMap(fromElement, true, toElement, false).navigableKeySet(); + } + + @Override + public TSortedSet headSet(E toElement) { + return map.headMap(toElement, false).navigableKeySet(); + } + + @Override + public TSortedSet tailSet(E fromElement) { + return map.tailMap(fromElement, true).navigableKeySet(); + } + + @Override + public E first() { + return map.firstKey(); + } + + @Override + public E last() { + return map.lastKey(); + } + + @Override + public E lower(E e) { + return map.lowerKey(e); + } + + @Override + public E floor(E e) { + return map.floorKey(e); + } + + @Override + public E ceiling(E e) { + return map.ceilingKey(e); + } + + @Override + public E higher(E e) { + return map.higherKey(e); + } + + @Override + public E pollFirst() { + TMap.Entry entry = map.pollFirstEntry(); + return entry != null ? entry.getKey() : null; + } + + @Override + public E pollLast() { + TMap.Entry entry = map.pollLastEntry(); + return entry != null ? entry.getKey() : null; + } + + @Override + public TNavigableSet descendingSet() { + return map.descendingKeySet(); + } + + @Override + public TIterator descendingIterator() { + return map.descendingKeySet().iterator(); + } + + @Override + public TNavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) { + return map.subMap(fromElement, true, toElement, false).navigableKeySet(); + } + + @Override + public TNavigableSet headSet(E toElement, boolean inclusive) { + return map.headMap(toElement, inclusive).navigableKeySet(); + } + + @Override + public TNavigableSet tailSet(E fromElement, boolean inclusive) { + return map.tailMap(fromElement, inclusive).navigableKeySet(); + } +}