net.sf.molae.pipe.sorted
Class SetAsSortedSet<B extends Collection<E>,E>

java.lang.Object
  extended by net.sf.molae.pipe.basic.ObjectProxy<B>
      extended by net.sf.molae.pipe.basic.CollectionProxy<B,E>
          extended by net.sf.molae.pipe.basic.CollectionAsSet<B,E>
              extended by net.sf.molae.pipe.sorted.SetAsSortedSet<B,E>
All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>, SortedSet<E>
Direct Known Subclasses:
ListAsSortedSet, SortedListAsSortedSet

public class SetAsSortedSet<B extends Collection<E>,E>
extends CollectionAsSet<B,E>
implements SortedSet<E>

If it can be guaranteed that a collection is a set and that its iterator returns elements in sorted order, this wrapper can be used to create a SortedSet implementation.


Constructor Summary
  SetAsSortedSet(B base, Comparator<? super E> comparator)
          Constructs a SetAsSortedSet object.
protected SetAsSortedSet(B base, Comparator<? super E> comparator, Comparator<Bound<E>> boundComparator, Bound<E> fromElement, Bound<E> toElement)
          Creates a new SetAsSortedSet with the specified properties.
 
Method Summary
 Comparator<? super E> comparator()
          Returns the comparator associated with this sorted set, or null if it uses its elements' natural ordering.
protected  int compare(Bound<E> o1, Bound<E> o2)
          Compares its two arguments for order using a comparator with infinities.
 boolean containsAll(Collection<?> c)
          Returns true if this set contains all of the elements of the specified collection.
 E first()
          Returns the first (lowest) element currently in this sorted set.
protected  Comparator<Bound<E>> getBoundComparator()
          Returns the comparator of this set taking infinity into account.
protected  Bound<E> getFromElement()
          Returns the lower bound of this SortedSet.
protected  SortedSet<E> getSubSet(Bound<E> fromElement, Bound<E> toElement)
          Common method to create head, tail and sub sets.
protected  Bound<E> getToElement()
          Returns the upper bound of this SortedSet.
 SortedSet<E> headSet(E toElement)
          Returns a view of the portion of this sorted set whose elements are strictly less than toElement.
 E last()
          Returns the last (highest) element currently in this sorted set.
static
<E> SortedSet<E>
newInstance(Collection<E> base, Comparator<? super E> comparator)
          Constructs a SetAsSortedSet object.
 boolean removeAll(Collection<?> c)
          Removes from this set all of its elements that are contained in the specified collection.
 boolean retainAll(Collection<?> c)
          Retains only the elements in this set that are contained in the specified collection.
 void setConsistent(boolean consistent)
          Sets the consistency flag.
 SortedSet<E> subSet(E fromElement, E toElement)
          Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
 SortedSet<E> tailSet(E fromElement)
          Returns a view of the portion of this sorted set whose elements are greater than or equal to fromElement.
protected  boolean useMerge(Collection c)
          This method determines if a merge algorithm is used for containsAll, removeAll and retainAll for the specified collection.
 
Methods inherited from class net.sf.molae.pipe.basic.CollectionAsSet
equals, hashCode, isSet, newInstance
 
Methods inherited from class net.sf.molae.pipe.basic.CollectionProxy
add, addAll, clear, contains, isEmpty, iterator, remove, size, toArray, toArray
 
Methods inherited from class net.sf.molae.pipe.basic.ObjectProxy
assertNotNull, getBase, toString, toVerboseString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
add, addAll, clear, contains, equals, hashCode, isEmpty, iterator, remove, size, toArray, toArray
 

Constructor Detail

SetAsSortedSet

protected SetAsSortedSet(B base,
                         Comparator<? super E> comparator,
                         Comparator<Bound<E>> boundComparator,
                         Bound<E> fromElement,
                         Bound<E> toElement)
Creates a new SetAsSortedSet with the specified properties. This constructor is intended for classes that implement both the SortedSet and its subsets.

Parameters:
base - the underlying collection
comparator - the comparator that will be used to sort this set. A null value indicates that the elements' natural ordering should be used.
boundComparator - the comparator that will be used to compare bounds - used for subsets.
fromElement - low endpoint (inclusive) of the subSet
toElement - high endpoint (exclusive) of the subSet
Throws:
ClassCastException - if FromElement and toElement cannot be compared to one another using this set's comparator (or, if the set has no comparator, using natural ordering).
IllegalArgumentException - if fromElement is greater than toElement.
NullPointerException - if fromElement or toElement is null and this set uses natural order, or its comparator does not tolerate null elements or if the base set is null.

SetAsSortedSet

public SetAsSortedSet(B base,
                      Comparator<? super E> comparator)
Constructs a SetAsSortedSet object.

Parameters:
base - the underlying collection
comparator - the comparator that will be used to sort this set. A null value indicates that the elements' natural ordering should be used.
Method Detail

comparator

public final Comparator<? super E> comparator()
Returns the comparator associated with this sorted set, or null if it uses its elements' natural ordering.

Specified by:
comparator in interface SortedSet<E>
Returns:
the comparator associated with this sorted set, or null if it uses its elements' natural ordering

getBoundComparator

protected Comparator<Bound<E>> getBoundComparator()
Returns the comparator of this set taking infinity into account.

Returns:
the comparator of this set taking infinity into account

getFromElement

protected final Bound<E> getFromElement()
Returns the lower bound of this SortedSet. Returns negative infinity if no such bound was specified.

Returns:
the lower bound of this SortedSet.

getToElement

protected final Bound<E> getToElement()
Returns the upper bound of this SortedSet. Returns positive infinity if no such bound was specified.

Returns:
the upper bound of this SortedSet

newInstance

public static <E> SortedSet<E> newInstance(Collection<E> base,
                                           Comparator<? super E> comparator)
Constructs a SetAsSortedSet object.

Parameters:
base - the underlying collection
comparator - the comparator that will be used to sort this set. A null value indicates that the elements' natural ordering should be used.

setConsistent

public void setConsistent(boolean consistent)
Sets the consistency flag.

Parameters:
consistent - false to indicate that the Comparator associated with this set is not consistent with equals.
See Also:
Comparator

useMerge

protected boolean useMerge(Collection c)
This method determines if a merge algorithm is used for containsAll, removeAll and retainAll for the specified collection. By default the following conditions are checked:
  1. The specified collection is a sorted set.
  2. It has the same comparator as the comparator of this set.
  3. The consistency flag is set.

Parameters:
c - a collection that is parameter of a bulk operation.
Returns:
true if a merge algorithm can be used for the specified collection.

containsAll

public boolean containsAll(Collection<?> c)
Returns true if this set contains all of the elements of the specified collection. If possible, a merging implementation is used, where both iterators are run through only once. Note that the comparator must be consistent with CollectionAsSet.equals(Object) for this implementation. If it is not, set consistent to false.

Specified by:
containsAll in interface Collection<E>
Specified by:
containsAll in interface Set<E>
Overrides:
containsAll in class CollectionProxy<B extends Collection<E>,E>
Parameters:
c - collection to be checked for containment in this collection
Returns:
true if this set contains all of the elements of the specified collection

retainAll

public boolean retainAll(Collection<?> c)
Retains only the elements in this set that are contained in the specified collection. If possible, a merging implementation is used, where both iterators are run through only once. Note that the comparator must be consistent with CollectionAsSet.equals(Object) for this implementation. If it is not, set consistent to false.

Specified by:
retainAll in interface Collection<E>
Specified by:
retainAll in interface Set<E>
Overrides:
retainAll in class CollectionProxy<B extends Collection<E>,E>
Parameters:
c - elements to be removed from this set
Returns:
true if this collection changed as a result of the call.
Throws:
UnsupportedOperationException - depending on the base set

removeAll

public boolean removeAll(Collection<?> c)
Removes from this set all of its elements that are contained in the specified collection. If possible, a merging implementation is used, where both iterators are run through only once. Note that the comparator must be consistent with CollectionAsSet.equals(Object) for this implementation. If it is not, set consistent to false.

Specified by:
removeAll in interface Collection<E>
Specified by:
removeAll in interface Set<E>
Overrides:
removeAll in class CollectionProxy<B extends Collection<E>,E>
Parameters:
c - elements to be removed from this set
Returns:
true if this collection changed as a result of the call.
Throws:
UnsupportedOperationException - depending on the base set

compare

protected int compare(Bound<E> o1,
                      Bound<E> o2)
Compares its two arguments for order using a comparator with infinities.

Parameters:
o1 - first object to compare
o2 - second object to compare
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

getSubSet

protected SortedSet<E> getSubSet(Bound<E> fromElement,
                                 Bound<E> toElement)
Common method to create head, tail and sub sets. Overwrite this method for specific implementations. The default implementation creates a new object of this class with the specified limits.

Parameters:
fromElement - low endpoint (inclusive) of the subSet
toElement - high endpoint (exclusive) of the subSet
Returns:
a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.

subSet

public SortedSet<E> subSet(E fromElement,
                           E toElement)
Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. (If fromElement and toElement are equal, the returned sorted set is empty.) This implementation calls getSubSet.

Specified by:
subSet in interface SortedSet<E>
Parameters:
fromElement - low endpoint (inclusive) of the subSet.
toElement - high endpoint (exclusive) of the subSet.
Returns:
a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
Throws:
ClassCastException - if FromElement and toElement cannot be compared to one another using this set's comparator (or, if the set has no comparator, using natural ordering).
IllegalArgumentException - if fromElement is greater than toElement.
NullPointerException - if fromElement or toElement is null and this set uses natural order, or its comparator does not tolerate null elements.

headSet

public SortedSet<E> headSet(E toElement)
Returns a view of the portion of this sorted set whose elements are strictly less than toElement. This implementation calls getSubSet() with the the fromElement of this SortedSet and the specified toElement.

Specified by:
headSet in interface SortedSet<E>
Parameters:
toElement - high endpoint (exclusive) of the headSet.
Returns:
a view of the specified initial range of this sorted set.
Throws:
ClassCastException - if toElement is not compatible with this set's comparator (or, if the set has no comparator, if toElement does not implement Comparable).
NullPointerException - if toElement is null and this sorted set does not tolerate null elements.
IllegalArgumentException - if this set is itself a subSet, headSet, or tailSet, and toElement is not within the specified range of the subSet, headSet, or tailSet.

tailSet

public SortedSet<E> tailSet(E fromElement)
Returns a view of the portion of this sorted set whose elements are greater than or equal to fromElement. This implementation calls getSubSet() with the specified fromElement and the toElement of this SortedSet.

Specified by:
tailSet in interface SortedSet<E>
Parameters:
fromElement - low endpoint (inclusive) of the tailSet.
Returns:
a view of the specified final range of this sorted set.
Throws:
ClassCastException - if fromElement is not compatible with this set's comparator (or, if the set has no comparator, if fromElement does not implement Comparable).
NullPointerException - if fromElement is null and this sorted set does not tolerate null elements.
IllegalArgumentException - if this set is itself a subSet, headSet, or tailSet, and fromElement is not within the specified range of the subSet, headSet, or tailSet.

first

public E first()
Returns the first (lowest) element currently in this sorted set. This implementation returns the first element from the iterator.

Specified by:
first in interface SortedSet<E>
Returns:
the first (lowest) element currently in this sorted set
Throws:
NoSuchElementException - sorted set is empty.

last

public E last()
Returns the last (highest) element currently in this sorted set. This implementation returns the last element from the iterator.

Specified by:
last in interface SortedSet<E>
Returns:
the last (highest) element currently in this sorted set
Throws:
NoSuchElementException - sorted set is empty.