Package org.organicdesign.fp.collections
Class PersistentTreeSet<E>
- java.lang.Object
-
- org.organicdesign.fp.collections.AbstractUnmodIterable<T>
-
- org.organicdesign.fp.collections.AbstractUnmodSet<E>
-
- org.organicdesign.fp.collections.PersistentTreeSet<E>
-
- All Implemented Interfaces:
Serializable
,Iterable<E>
,Collection<E>
,Set<E>
,SortedSet<E>
,BaseSet<E>
,ImSortedSet<E>
,Sized
,UnmodCollection<E>
,UnmodIterable<E>
,UnmodSet<E>
,UnmodSortedCollection<E>
,UnmodSortedIterable<E>
,UnmodSortedSet<E>
,Transformable<E>
public class PersistentTreeSet<E> extends AbstractUnmodSet<E> implements ImSortedSet<E>, Serializable
A wrapper that turns a PersistentTreeMap into a set. This file is a derivative work based on a Clojure collection licensed under the Eclipse Public License 1.0 Copyright Rich Hickey. Errors by Glen Peterson.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.organicdesign.fp.collections.UnmodIterable
UnmodIterable.UnIterable
-
-
Field Summary
Fields Modifier and Type Field Description static PersistentTreeSet
EMPTY
Be extremely careful with this because it uses the default comparator, which only works for items that implement Comparable (have a "natural ordering").
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Comparator<? super E>
comparator()
Returns the comparator used to order the items in this set, or null if it uses Fn2.DEFAULT_COMPARATOR (for compatibility with java.util.SortedSet).boolean
contains(Object o)
Returns true if the set contains the given item in O(log n) time.static <T extends Comparable<T>>
PersistentTreeSet<T>empty()
Be extremely careful with this because it uses the default comparator, which only works for items that implement Comparable (have a "natural ordering").boolean
equals(Object other)
This is designed to be correct, rather than fully compatible with TreeSet.equals().E
first()
Use head() inherited from Sequence instead of this method which is inherited from SortedSet.@NotNull Option<E>
head()
The first item in this iterable.boolean
isEmpty()
This is a convenience method inherited from Collection that returns true if size() == 0 (if this set contains no elements).@NotNull UnmodSortedIterator<E>
iterator()
Iterates over contents in a guaranteed order.E
last()
Inherited from SortedSet, returns the last item in this set, or throw an exception if this set is empty.static <T extends Comparable<T>>
PersistentTreeSet<T>of(Iterable<T> items)
Returns a new PersistentTreeSet of the given comparable items.static <T> PersistentTreeSet<T>
ofComp(Comparator<? super T> comp)
Returns a new PersistentTreeSet of the given comparator.static <T> PersistentTreeSet<T>
ofComp(Comparator<? super T> comp, Iterable<T> elements)
Returns a new PersistentTreeSet of the given comparator and items.static <T> PersistentTreeSet<T>
ofMap(ImSortedMap<T,?> i)
Returns a new PersistentTreeSet of the keys and comparator in the given map.@NotNull PersistentTreeSet<E>
put(E e)
Adds an element.int
size()
The size of this set.@NotNull ImSortedSet<E>
subSet(E fromElement, E toElement)
Return the elements in this set from the start element (inclusive) to the end element (exclusive)@NotNull ImSortedSet<E>
tailSet(E fromElement)
@NotNull PersistentTreeSet<E>
without(E key)
Removes this key from the set-
Methods inherited from class org.organicdesign.fp.collections.AbstractUnmodIterable
hashCode, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, stream, toArray
-
Methods inherited from interface org.organicdesign.fp.collections.ImSortedSet
headSet, union
-
Methods inherited from interface java.util.Set
add, addAll, clear, containsAll, hashCode, remove, removeAll, retainAll, toArray, toArray
-
Methods inherited from interface java.util.SortedSet
spliterator
-
Methods inherited from interface org.organicdesign.fp.xform.Transformable
any, toImList, toImMap, toImRrbt, toImSet, toImSortedMap, toImSortedSet, toMutList, toMutMap, toMutRrbt, toMutSet, toMutSortedMap, toMutSortedSet
-
-
-
-
Field Detail
-
EMPTY
public static final PersistentTreeSet EMPTY
Be extremely careful with this because it uses the default comparator, which only works for items that implement Comparable (have a "natural ordering"). An attempt to use it with other items will blow up at runtime. Either a withComparator() method will be added, or this will be removed.
-
-
Method Detail
-
empty
public static <T extends Comparable<T>> PersistentTreeSet<T> empty()
Be extremely careful with this because it uses the default comparator, which only works for items that implement Comparable (have a "natural ordering"). An attempt to use it with other items will blow up at runtime. Either a withComparator() method will be added, or this will be removed.
-
ofComp
public static <T> PersistentTreeSet<T> ofComp(Comparator<? super T> comp)
Returns a new PersistentTreeSet of the given comparator. Always use this instead of starting with empty() because there is no way to assign a comparator to an existing set.
-
ofComp
public static <T> PersistentTreeSet<T> ofComp(Comparator<? super T> comp, Iterable<T> elements)
Returns a new PersistentTreeSet of the given comparator and items.- Parameters:
comp
- A comparator that defines the sort order of elements in the new set. This becomes part of the set (it's not for pre-sorting).elements
- items to go into the set. In the case of a duplicate element, later values in the input list overwrite the earlier ones.- Returns:
- a new PersistentTreeSet of the specified comparator and the given elements
-
of
public static <T extends Comparable<T>> PersistentTreeSet<T> of(Iterable<T> items)
Returns a new PersistentTreeSet of the given comparable items.
-
ofMap
public static <T> PersistentTreeSet<T> ofMap(ImSortedMap<T,?> i)
Returns a new PersistentTreeSet of the keys and comparator in the given map. Since PersistentTreeSet is just a wrapper for a PersistentTreeMap, this can be a very cheap operation.
-
comparator
public Comparator<? super E> comparator()
Returns the comparator used to order the items in this set, or null if it uses Fn2.DEFAULT_COMPARATOR (for compatibility with java.util.SortedSet).- Specified by:
comparator
in interfaceSortedSet<E>
-
contains
public boolean contains(Object o)
Returns true if the set contains the given item in O(log n) time. This is the defining method of a set.
-
without
@NotNull public @NotNull PersistentTreeSet<E> without(E key)
Removes this key from the set
-
iterator
@NotNull public @NotNull UnmodSortedIterator<E> iterator()
Iterates over contents in a guaranteed order. Iterates over contents with no guarantees about their ordering. An unmodifiable iterator A one-time use, mutable, not-thread-safe way to get each value of the underling collection in turn. I experimented with various thread-safe alternatives, but the JVM is optimized around iterators so this is the lowest common denominator of collection iteration, even though iterators are inherently mutable.- Specified by:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceImSortedSet<E>
- Specified by:
iterator
in interfaceIterable<E>
- Specified by:
iterator
in interfaceSet<E>
- Specified by:
iterator
in interfaceUnmodCollection<E>
- Specified by:
iterator
in interfaceUnmodIterable<E>
- Specified by:
iterator
in interfaceUnmodSet<E>
- Specified by:
iterator
in interfaceUnmodSortedCollection<E>
- Specified by:
iterator
in interfaceUnmodSortedIterable<E>
- Specified by:
iterator
in interfaceUnmodSortedSet<E>
-
equals
public boolean equals(Object other)
This is designed to be correct, rather than fully compatible with TreeSet.equals(). TreeSet.equals() does not take ordering into account and this does. You want better equality? Define an Equator. This is for Java@trade; interop.- Specified by:
equals
in interfaceCollection<E>
- Specified by:
equals
in interfaceSet<E>
- Overrides:
equals
in classAbstractUnmodSet<E>
-
first
public E first()
Use head() inherited from Sequence instead of this method which is inherited from SortedSet. This method returns the first element if it exists, or throws a NoSuchElementException if the set is empty. head() returns an Option of the first element where as this method throws an exception if this set is empty. I had to rename the method on Sequence from first() to head() to work around this. Also returning an Option is thread-safe for building a lazy sequence. The alternative, examining the rest() of a sequence to see if it's == Sequence.empty() gets ugly very quickly and makes many transformations eager (especially flatMap).
-
head
@NotNull public @NotNull Option<E> head()
The first item in this iterable.- Specified by:
head
in interfaceTransformable<E>
- Specified by:
head
in interfaceUnmodIterable<E>
- Returns:
- an eagerly evaluated result which is a single item.
-
isEmpty
public boolean isEmpty()
This is a convenience method inherited from Collection that returns true if size() == 0 (if this set contains no elements).
-
last
public E last()
Inherited from SortedSet, returns the last item in this set, or throw an exception if this set is empty. Good luck with that.
-
put
@NotNull public @NotNull PersistentTreeSet<E> put(E e)
Adds an element. If the element already exists in this set, the new value overwrites the old one. If the new element is the same as an old element (based on the address of that item in memory, not an equals test), the old set may be returned unchanged.
-
size
public int size()
The size of this set.
-
subSet
@NotNull public @NotNull ImSortedSet<E> subSet(E fromElement, E toElement)
Return the elements in this set from the start element (inclusive) to the end element (exclusive)- Specified by:
subSet
in interfaceImSortedSet<E>
- Specified by:
subSet
in interfaceSortedSet<E>
- Specified by:
subSet
in interfaceUnmodSortedSet<E>
-
tailSet
@NotNull public @NotNull ImSortedSet<E> tailSet(E fromElement)
- Specified by:
tailSet
in interfaceImSortedSet<E>
- Specified by:
tailSet
in interfaceSortedSet<E>
- Specified by:
tailSet
in interfaceUnmodSortedSet<E>
-
-