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:
java.io.Serializable,java.lang.Iterable<E>,java.util.Collection<E>,java.util.Set<E>,java.util.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>, java.io.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
-
-
Field Summary
Fields Modifier and Type Field Description static PersistentTreeSetEMPTYBe 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 java.util.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).booleancontains(java.lang.Object o)Returns true if the set contains the given item in O(log n) time.static <T extends java.lang.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").booleanequals(java.lang.Object other)This is designed to be correct, rather than fully compatible with TreeSet.equals().Efirst()Use head() inherited from Sequence instead of this method which is inherited from SortedSet.Option<E>head()The first item in this iterable.booleanisEmpty()This is a convenience method inherited from Collection that returns true if size() == 0 (if this set contains no elements).UnmodSortedIterator<E>iterator()Iterates over contents in a guaranteed order.Elast()Inherited from SortedSet, returns the last item in this set, or throw an exception if this set is empty.static <T extends java.lang.Comparable<T>>
PersistentTreeSet<T>of(java.lang.Iterable<T> items)Returns a new PersistentTreeSet of the given comparable items.static <T> PersistentTreeSet<T>ofComp(java.util.Comparator<? super T> comp)Returns a new PersistentTreeSet of the given comparator.static <T> PersistentTreeSet<T>ofComp(java.util.Comparator<? super T> comp, java.lang.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.PersistentTreeSet<E>put(E e)Adds an element.intsize()The size of this set.ImSortedSet<E>subSet(E fromElement, E toElement)Return the elements in this set from the start element (inclusive) to the end element (exclusive)ImSortedSet<E>tailSet(E fromElement)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 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 org.organicdesign.fp.xform.Transformable
toImList, toImMap, toImRrbt, toImSet, toImSortedMap, toImSortedSet, toMutableList, toMutableMap, toMutableRrbt, toMutableSet, toMutableSortedMap, toMutableSortedSet
-
-
-
-
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 java.lang.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(java.util.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(java.util.Comparator<? super T> comp, java.lang.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 java.lang.Comparable<T>> PersistentTreeSet<T> of(java.lang.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 java.util.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:
comparatorin interfacejava.util.SortedSet<E>
-
contains
public boolean contains(java.lang.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
public PersistentTreeSet<E> without(E key)
Removes this key from the set
-
iterator
public 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:
iteratorin interfacejava.util.Collection<E>- Specified by:
iteratorin interfaceImSortedSet<E>- Specified by:
iteratorin interfacejava.lang.Iterable<E>- Specified by:
iteratorin interfacejava.util.Set<E>- Specified by:
iteratorin interfaceUnmodCollection<E>- Specified by:
iteratorin interfaceUnmodIterable<E>- Specified by:
iteratorin interfaceUnmodSet<E>- Specified by:
iteratorin interfaceUnmodSortedCollection<E>- Specified by:
iteratorin interfaceUnmodSortedIterable<E>- Specified by:
iteratorin interfaceUnmodSortedSet<E>
-
equals
public boolean equals(java.lang.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:
equalsin interfacejava.util.Collection<E>- Specified by:
equalsin interfacejava.util.Set<E>- Overrides:
equalsin 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).- Specified by:
firstin interfacejava.util.SortedSet<E>
-
head
public Option<E> head()
The first item in this iterable.- Specified by:
headin interfaceTransformable<E>- Specified by:
headin 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.- Specified by:
lastin interfacejava.util.SortedSet<E>
-
put
public 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
public 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:
subSetin interfaceImSortedSet<E>- Specified by:
subSetin interfacejava.util.SortedSet<E>- Specified by:
subSetin interfaceUnmodSortedSet<E>
-
tailSet
public ImSortedSet<E> tailSet(E fromElement)
- Specified by:
tailSetin interfaceImSortedSet<E>- Specified by:
tailSetin interfacejava.util.SortedSet<E>- Specified by:
tailSetin interfaceUnmodSortedSet<E>
-
-