Skip to content

Commit

Permalink
COLLECTIONS-873. Remove TreeSet and make SortedSet through SortedMap …
Browse files Browse the repository at this point in the history
…delegation
  • Loading branch information
ekuvardin committed Jan 19, 2025
1 parent 9a4c3cb commit 4b308e0
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import java.util.AbstractSet;
import java.util.Comparator;
import java.util.Iterator;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.commons.collections4.trie.PatriciaTrie;

Expand All @@ -38,7 +38,6 @@
* <p>
* This class is Serializable and Cloneable.
* </p>
*
*/
public class PatriciaTrieSet extends AbstractSet<String> implements TrieSet<String>, Serializable {

Expand Down Expand Up @@ -127,7 +126,7 @@ private void readObject(java.io.ObjectInputStream s)

@Override
public SortedSet<String> prefixSet(String key) {
return new TreeSet<>(trie.prefixMap(key).keySet());
return new RangePatriciaTrieSortedSet(trie.prefixMap(key));
}

@Override
Expand All @@ -137,17 +136,17 @@ public Comparator<? super String> comparator() {

@Override
public SortedSet<String> subSet(String fromElement, String toElement) {
return new TreeSet<>(trie.subMap(fromElement, toElement).keySet());
return new RangePatriciaTrieSortedSet(trie.subMap(fromElement, toElement));
}

@Override
public SortedSet<String> headSet(String toElement) {
return new TreeSet<>(trie.headMap(toElement).keySet());
return new RangePatriciaTrieSortedSet(trie.headMap(toElement));
}

@Override
public SortedSet<String> tailSet(String fromElement) {
return new TreeSet<>(trie.tailMap(fromElement).keySet());
return new RangePatriciaTrieSortedSet(trie.tailMap(fromElement));
}

@Override
Expand All @@ -160,4 +159,81 @@ public String last() {
return trie.lastKey();
}

static class RangePatriciaTrieSortedSet extends AbstractSet<String> implements SortedSet<String> {

protected SortedMap<String, Object> delegate;

RangePatriciaTrieSortedSet(SortedMap<String, Object> del) {
delegate = del;
}

@Override
public Iterator<String> iterator() {
return delegate.keySet().iterator();
}

@Override
public int size() {
return delegate.size();
}

@Override
public boolean contains(Object key) {
if (key instanceof String) {
return delegate.containsKey(key);
}

return false;
}

@Override
public Comparator<? super String> comparator() {
return delegate.comparator();
}

@Override
public SortedSet<String> subSet(String fromElement, String toElement) {
return new RangePatriciaTrieSortedSet(delegate.subMap(fromElement, toElement));
}

@Override
public SortedSet<String> headSet(String toElement) {
return new RangePatriciaTrieSortedSet(delegate.headMap(toElement));
}

@Override
public SortedSet<String> tailSet(String fromElement) {
return new RangePatriciaTrieSortedSet(delegate.tailMap(fromElement));
}

@Override
public String first() {
return delegate.firstKey();
}

@Override
public String last() {
return delegate.lastKey();
}

@Override
public boolean add(String value) {
return delegate.put(value, PRESENT) != null;
}

@Override
public boolean remove(Object o) {
return delegate.remove(o) == PRESENT;
}

@Override
public boolean isEmpty() {
return delegate.isEmpty();
}

@Override
public void clear() {
delegate.clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public void setShouldContainUniqueElements() {
patriciaTrieSet.add("Albe");

Assertions.assertEquals(2, patriciaTrieSet.size());
Assertions.assertTrue(patriciaTrieSet.contains("Alberto"));
Assertions.assertTrue(patriciaTrieSet.contains("Albe"));
}

@Test
Expand Down

0 comments on commit 4b308e0

Please sign in to comment.