Class RangeSet<T extends Comparable<? super T>>

  • Type Parameters:
    T - The type of range elements.
    All Implemented Interfaces:
    Serializable, Cloneable, Iterable<Range<T>>, Collection<Range<T>>, Set<Range<T>>, SortedSet<Range<T>>, Cloneable

    public class RangeSet<T extends Comparable<? super T>>
    extends AbstractSet<Range<T>>
    implements SortedSet<Range<T>>, Cloneable, Serializable
    An ordered set of ranges. RangeSet objects store an arbitrary number of ranges in any Java's primitives (int, float, etc.) or any comparable objects. Ranges may be added in any order. When a range is added, RangeSet first looks for an existing range overlapping the specified range. If an overlapping range is found, ranges are merged as of Range.union(org.geotools.util.Range<?>). Consequently, ranges returned by iterator() may not be the same than added ranges.

    All entries in this set can be seen as Range objects. This class is not thread-safe.

    Since:
    2.0
    Author:
    Martin Desruisseaux (IRD), Andrea Aime
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      RangeSet​(Class<T> type)
      Constructs an empty set of range.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(byte lower, byte upper)
      Add a range of values to this set.
      boolean add​(double lower, double upper)
      Add a range of values to this set.
      boolean add​(float lower, float upper)
      Add a range of values to this set.
      boolean add​(int lower, int upper)
      Add a range of values to this set.
      boolean add​(long lower, long upper)
      Add a range of values to this set.
      boolean add​(short lower, short upper)
      Add a range of values to this set.
      <N> boolean add​(Comparable<? super N> min, Comparable<? super N> max)
      Adds a range of values to this set.
      boolean add​(Range<T> range)
      Add a range to this set.
      void clear()
      Remove all elements from this set of ranges.
      RangeSet clone()
      Returns a clone of this range set.
      Comparator<Range<T>> comparator()
      Returns the comparator associated with this sorted set.
      boolean contains​(Object object)
      Returns true if this set contains the specified element.
      boolean equals​(Object object)
      Compares the specified object with this set of ranges for equality.
      Range<T> first()
      Returns the first (lowest) range currently in this sorted set.
      double getMaxValueAsDouble​(int index)
      Returns a range's maximum value as a double.
      double getMinValueAsDouble​(int index)
      Returns a range's minimum value as a double.
      int hashCode()
      Returns a hash value for this set of ranges.
      SortedSet<Range<T>> headSet​(Range<T> upper)
      Returns a view of the portion of this sorted set whose elements are strictly less than upper.
      <T> int indexOfRange​(Comparable<T> value)
      If the specified value is inside a range, returns the index of this range.
      Iterator<Range<T>> iterator()
      Returns an iterator over the elements in this set of ranges.
      Range<T> last()
      Returns the last (highest) range currently in this sorted set.
      boolean remove​(byte lower, byte upper)
      Remove a range of values from this set.
      boolean remove​(double lower, double upper)
      Remove a range of values from this set.
      boolean remove​(float lower, float upper)
      Remove a range of values from this set.
      boolean remove​(int lower, int upper)
      Remove a range of values from this set.
      boolean remove​(long lower, long upper)
      Remove a range of values from this set.
      boolean remove​(short lower, short upper)
      Remove a range of values from this set.
      <N> boolean remove​(Comparable<? super N> min, Comparable<? super N> max)
      Remove a range of values from this set.
      int size()
      Returns the number of ranges in this set.
      SortedSet<Range<T>> subSet​(Range<T> lower, Range<T> upper)
      Returns a view of the portion of this sorted set whose elements range from lower, inclusive, to upper, exclusive.
      SortedSet<Range<T>> tailSet​(Range<T> lower)
      Returns a view of the portion of this sorted set whose elements are greater than or equal to lower.
      String toString()
      Returns a string representation of this set of ranges.
      • Methods inherited from class AbstractSet

        removeAll
      • Methods inherited from class AbstractCollection

        addAll, containsAll, isEmpty, remove, retainAll, toArray, toArray
      • Methods inherited from class Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface Iterable

        forEach
      • Methods inherited from interface Set

        addAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
      • Methods inherited from interface SortedSet

        spliterator
    • Constructor Detail

      • RangeSet

        public RangeSet​(Class<T> type)
                 throws IllegalArgumentException
        Constructs an empty set of range.
        Parameters:
        type - The class of the range elements. It must be a primitive type or a class implementing Comparable.
        Throws:
        IllegalArgumentException - if type is not a primitive type or a class implementing Comparable.
    • Method Detail

      • comparator

        public Comparator<Range<T>> comparator()
        Returns the comparator associated with this sorted set.
        Specified by:
        comparator in interface SortedSet<T extends Comparable<? super T>>
      • clear

        public void clear()
        Remove all elements from this set of ranges.
        Specified by:
        clear in interface Collection<T extends Comparable<? super T>>
        Specified by:
        clear in interface Set<T extends Comparable<? super T>>
        Overrides:
        clear in class AbstractCollection<Range<T extends Comparable<? super T>>>
      • size

        public int size()
        Returns the number of ranges in this set.
        Specified by:
        size in interface Collection<T extends Comparable<? super T>>
        Specified by:
        size in interface Set<T extends Comparable<? super T>>
        Specified by:
        size in class AbstractCollection<Range<T extends Comparable<? super T>>>
      • add

        public boolean add​(Range<T> range)
        Add a range to this set. Range may be added in any order. If the specified range overlap an existing range, the two range will be merged as of Range.union(org.geotools.util.Range<?>).

        Note: current version do not support open interval (i.e. Range.is[Min/Max]Included() must return true).

        Specified by:
        add in interface Collection<T extends Comparable<? super T>>
        Specified by:
        add in interface Set<T extends Comparable<? super T>>
        Overrides:
        add in class AbstractCollection<Range<T extends Comparable<? super T>>>
        Parameters:
        range - The range to add.
        Returns:
        true if this set changed as a result of the call.
      • add

        public <N> boolean add​(Comparable<? super N> min,
                               Comparable<? super N> max)
                        throws IllegalArgumentException
        Adds a range of values to this set. Range may be added in any order. If the specified range overlap an existing range, the two ranges will be merged.
        Parameters:
        min - The lower value, inclusive.
        max - The upper value, inclusive.
        Returns:
        true if this set changed as a result of the call.
        Throws:
        IllegalArgumentException - if lower is greater than upper.
      • add

        public boolean add​(byte lower,
                           byte upper)
                    throws IllegalArgumentException
        Add a range of values to this set. Range may be added in any order. If the specified range overlap an existing range, the two ranges will be merged.
        Parameters:
        lower - The lower value, inclusive.
        upper - The upper value, inclusive.
        Returns:
        true if this set changed as a result of the call.
        Throws:
        IllegalArgumentException - if lower is greater than upper.
      • add

        public boolean add​(short lower,
                           short upper)
                    throws IllegalArgumentException
        Add a range of values to this set. Range may be added in any order. If the specified range overlap an existing range, the two ranges will be merged.
        Parameters:
        lower - The lower value, inclusive.
        upper - The upper value, inclusive.
        Returns:
        true if this set changed as a result of the call.
        Throws:
        IllegalArgumentException - if lower is greater than upper.
      • add

        public boolean add​(int lower,
                           int upper)
                    throws IllegalArgumentException
        Add a range of values to this set. Range may be added in any order. If the specified range overlap an existing range, the two ranges will be merged.
        Parameters:
        lower - The lower value, inclusive.
        upper - The upper value, inclusive.
        Returns:
        true if this set changed as a result of the call.
        Throws:
        IllegalArgumentException - if lower is greater than upper.
      • add

        public boolean add​(long lower,
                           long upper)
                    throws IllegalArgumentException
        Add a range of values to this set. Range may be added in any order. If the specified range overlap an existing range, the two ranges will be merged.
        Parameters:
        lower - The lower value, inclusive.
        upper - The upper value, inclusive.
        Returns:
        true if this set changed as a result of the call.
        Throws:
        IllegalArgumentException - if lower is greater than upper.
      • add

        public boolean add​(float lower,
                           float upper)
                    throws IllegalArgumentException
        Add a range of values to this set. Range may be added in any order. If the specified range overlap an existing range, the two ranges will be merged.
        Parameters:
        lower - The lower value, inclusive.
        upper - The upper value, inclusive.
        Returns:
        true if this set changed as a result of the call.
        Throws:
        IllegalArgumentException - if lower is greater than upper.
      • add

        public boolean add​(double lower,
                           double upper)
                    throws IllegalArgumentException
        Add a range of values to this set. Range may be added in any order. If the specified range overlap an existing range, the two ranges will be merged.
        Parameters:
        lower - The lower value, inclusive.
        upper - The upper value, inclusive.
        Returns:
        true if this set changed as a result of the call.
        Throws:
        IllegalArgumentException - if lower is greater than upper.
      • remove

        public <N> boolean remove​(Comparable<? super N> min,
                                  Comparable<? super N> max)
                           throws IllegalArgumentException
        Remove a range of values from this set. Range may be removed in any order.
        Parameters:
        min - The lower value to remove, exclusive.
        max - The upper value to remove, exclusive.
        Returns:
        true if this set changed as a result of the call.
        Throws:
        IllegalArgumentException - if lower is greater than upper.
      • remove

        public boolean remove​(byte lower,
                              byte upper)
                       throws IllegalArgumentException
        Remove a range of values from this set. Range may be removed in any order.
        Parameters:
        lower - The lower value to remove, exclusive.
        upper - The upper value to remove, exclusive.
        Returns:
        true if this set changed as a result of the call.
        Throws:
        IllegalArgumentException - if lower is greater than upper.
      • remove

        public boolean remove​(short lower,
                              short upper)
                       throws IllegalArgumentException
        Remove a range of values from this set. Range may be removed in any order.
        Parameters:
        lower - The lower value to remove, exclusive.
        upper - The upper value to remove, exclusive.
        Returns:
        true if this set changed as a result of the call.
        Throws:
        IllegalArgumentException - if lower is greater than upper.
      • remove

        public boolean remove​(int lower,
                              int upper)
                       throws IllegalArgumentException
        Remove a range of values from this set. Range may be removed in any order.
        Parameters:
        lower - The lower value to remove, exclusive.
        upper - The upper value to remove, exclusive.
        Returns:
        true if this set changed as a result of the call.
        Throws:
        IllegalArgumentException - if lower is greater than upper.
      • remove

        public boolean remove​(long lower,
                              long upper)
                       throws IllegalArgumentException
        Remove a range of values from this set. Range may be removed in any order.
        Parameters:
        lower - The lower value to remove, exclusive.
        upper - The upper value to remove, exclusive.
        Returns:
        true if this set changed as a result of the call.
        Throws:
        IllegalArgumentException - if lower is greater than upper.
      • remove

        public boolean remove​(float lower,
                              float upper)
                       throws IllegalArgumentException
        Remove a range of values from this set. Range may be removed in any order.
        Parameters:
        lower - The lower value to remove, exclusive.
        upper - The upper value to remove, exclusive.
        Returns:
        true if this set changed as a result of the call.
        Throws:
        IllegalArgumentException - if lower is greater than upper.
      • remove

        public boolean remove​(double lower,
                              double upper)
                       throws IllegalArgumentException
        Remove a range of values from this set. Range may be removed in any order.
        Parameters:
        lower - The lower value to remove, exclusive.
        upper - The upper value to remove, exclusive.
        Returns:
        true if this set changed as a result of the call.
        Throws:
        IllegalArgumentException - if lower is greater than upper.
      • getMinValueAsDouble

        public final double getMinValueAsDouble​(int index)
                                         throws IndexOutOfBoundsException,
                                                ClassCastException
        Returns a range's minimum value as a double. The index can be any value from 0 inclusive to the set's size exclusive. The returned values always increase with index.
        Parameters:
        index - The range index, from 0 inclusive to size exclusive.
        Returns:
        The minimum value for the range at the specified index.
        Throws:
        IndexOutOfBoundsException - if index is out of bounds.
        ClassCastException - if range elements are not convertible to numbers.
      • getMaxValueAsDouble

        public final double getMaxValueAsDouble​(int index)
                                         throws IndexOutOfBoundsException,
                                                ClassCastException
        Returns a range's maximum value as a double. The index can be any value from 0 inclusive to the set's size exclusive. The returned values always increase with index.
        Parameters:
        index - The range index, from 0 inclusive to size exclusive.
        Returns:
        The maximum value for the range at the specified index.
        Throws:
        IndexOutOfBoundsException - if index is out of bounds.
        ClassCastException - if range elements are not convertible to numbers.
      • indexOfRange

        public <T> int indexOfRange​(Comparable<T> value)
        If the specified value is inside a range, returns the index of this range. Otherwise, returns -1.
        Parameters:
        value - The value to search.
        Returns:
        The index of the range which contains this value, or -1 if there is no such range.
      • contains

        public boolean contains​(Object object)
        Returns true if this set contains the specified element.
        Specified by:
        contains in interface Collection<T extends Comparable<? super T>>
        Specified by:
        contains in interface Set<T extends Comparable<? super T>>
        Overrides:
        contains in class AbstractCollection<Range<T extends Comparable<? super T>>>
        Parameters:
        object - The object to compare to this set.
        Returns:
        true if the given object is equals to this set.
      • first

        public Range<T> first()
                       throws NoSuchElementException
        Returns the first (lowest) range currently in this sorted set.
        Specified by:
        first in interface SortedSet<T extends Comparable<? super T>>
        Throws:
        NoSuchElementException - if the set is empty.
      • last

        public Range<T> last()
                      throws NoSuchElementException
        Returns the last (highest) range currently in this sorted set.
        Specified by:
        last in interface SortedSet<T extends Comparable<? super T>>
        Throws:
        NoSuchElementException - if the set is empty.
      • subSet

        public SortedSet<Range<T>> subSet​(Range<T> lower,
                                          Range<T> upper)
        Returns a view of the portion of this sorted set whose elements range from lower, inclusive, to upper, exclusive.
        Specified by:
        subSet in interface SortedSet<T extends Comparable<? super T>>
        Parameters:
        lower - Low endpoint (inclusive) of the sub set.
        upper - High endpoint (exclusive) of the sub set.
        Returns:
        A view of the specified range within this sorted set.
      • headSet

        public SortedSet<Range<T>> headSet​(Range<T> upper)
        Returns a view of the portion of this sorted set whose elements are strictly less than upper.
        Specified by:
        headSet in interface SortedSet<T extends Comparable<? super T>>
        Parameters:
        upper - High endpoint (exclusive) of the headSet.
        Returns:
        A view of the specified initial range of this sorted set.
      • tailSet

        public SortedSet<Range<T>> tailSet​(Range<T> lower)
        Returns a view of the portion of this sorted set whose elements are greater than or equal to lower.
        Specified by:
        tailSet in interface SortedSet<T extends Comparable<? super T>>
        Parameters:
        lower - Low endpoint (inclusive) of the tailSet.
        Returns:
        A view of the specified final range of this sorted set.
      • iterator

        public Iterator<Range<T>> iterator()
        Returns an iterator over the elements in this set of ranges. All elements are Range objects.
        Specified by:
        iterator in interface Collection<T extends Comparable<? super T>>
        Specified by:
        iterator in interface Iterable<T extends Comparable<? super T>>
        Specified by:
        iterator in interface Set<T extends Comparable<? super T>>
        Specified by:
        iterator in class AbstractCollection<Range<T extends Comparable<? super T>>>
      • hashCode

        public int hashCode()
        Returns a hash value for this set of ranges. This value need not remain consistent between different implementations of the same class.
        Specified by:
        hashCode in interface Collection<T extends Comparable<? super T>>
        Specified by:
        hashCode in interface Set<T extends Comparable<? super T>>
        Overrides:
        hashCode in class AbstractSet<Range<T extends Comparable<? super T>>>
      • equals

        public boolean equals​(Object object)
        Compares the specified object with this set of ranges for equality.
        Specified by:
        equals in interface Collection<T extends Comparable<? super T>>
        Specified by:
        equals in interface Set<T extends Comparable<? super T>>
        Overrides:
        equals in class AbstractSet<Range<T extends Comparable<? super T>>>
        Parameters:
        object - The object to compare with this range.
        Returns:
        true if the given object is equals to this range.
      • clone

        public RangeSet clone()
        Returns a clone of this range set.
        Specified by:
        clone in interface Cloneable
        Overrides:
        clone in class Object
        Returns:
        A clone of this range set.
        See Also:
        Object.clone()
      • toString

        public String toString()
        Returns a string representation of this set of ranges. The returned string is implementation dependent. It is usually provided for debugging purposes.
        Overrides:
        toString in class AbstractCollection<Range<T extends Comparable<? super T>>>