org.geotools.util
Class RangeSet

java.lang.Object
  extended byjava.util.AbstractCollection
      extended byjava.util.AbstractSet
          extended byorg.geotools.util.RangeSet
All Implemented Interfaces:
java.lang.Cloneable, org.opengis.util.Cloneable, java.util.Collection, java.io.Serializable, java.util.Set, java.util.SortedSet

public class RangeSet
extends java.util.AbstractSet
implements java.util.SortedSet, org.opengis.util.Cloneable, java.io.Serializable

An ordered set of ranges. objects store an arbitrary number of ranges in any Java's primitives (, , etc.) or any comparable objects. Ranges may be added in any order. When a range is added, first looks for an existing range overlapping the specified range. If an overlapping range is found, ranges are merged as of Range.union(javax.media.jai.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
Version:
$Id: RangeSet.java 17672 2006-01-19 00:25:55Z desruisseaux $
Author:
Martin Desruisseaux, Andrea Aime
See Also:
Serialized Form

Constructor Summary
RangeSet(java.lang.Class type)
          Construct an empty set of range.
 
Method Summary
 boolean add(byte lower, byte upper)
          Add a range of values to this set.
 boolean add(java.lang.Comparable lower, java.lang.Comparable 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(java.lang.Object r)
          Add a range to this set.
 boolean add(short lower, short upper)
          Add a range of values to this set.
 void clear()
          Remove all elements from this set of ranges.
 java.lang.Object clone()
          Returns a clone of this range set.
 java.util.Comparator comparator()
          Returns the comparator associated with this sorted set.
 boolean contains(java.lang.Object object)
          Returns if this set contains the specified element.
 boolean equals(java.lang.Object object)
          Compares the specified object with this set of ranges for equality.
 java.lang.Object first()
          Returns the first (lowest) range currently in this sorted set.
 double getMaxValueAsDouble(int index)
          Returns a range's maximum value as a .
 double getMinValueAsDouble(int index)
          Returns a range's minimum value as a .
 int hashCode()
          Returns a hash value for this set of ranges.
 java.util.SortedSet headSet(java.lang.Object upper)
          Returns a view of the portion of this sorted set whose elements are strictly less than .
 int indexOfRange(java.lang.Comparable value)
          If the specified value is inside a range, returns the index of this range.
 java.util.Iterator iterator()
          Returns an iterator over the elements in this set of ranges.
 java.lang.Object 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(java.lang.Comparable lower, java.lang.Comparable 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.
 int size()
          Returns the number of ranges in this set.
 java.util.SortedSet subSet(java.lang.Object lower, java.lang.Object upper)
          Returns a view of the portion of this sorted set whose elements range from , inclusive, to , exclusive.
 java.util.SortedSet tailSet(java.lang.Object lower)
          Returns a view of the portion of this sorted set whose elements are greater than or equal to .
 java.lang.String toString()
          Returns a string representation of this set of ranges.
 
Methods inherited from class java.util.AbstractSet
removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, remove, retainAll, toArray, toArray
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Constructor Detail

RangeSet

public RangeSet(java.lang.Class type)
         throws java.lang.IllegalArgumentException
Construct 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:
java.lang.IllegalArgumentException - if is not a primitive type or a class implementing Comparable.
Method Detail

comparator

public java.util.Comparator comparator()
Returns the comparator associated with this sorted set.

Specified by:
comparator in interface java.util.SortedSet

clear

public void clear()
Remove all elements from this set of ranges.

Specified by:
clear in interface java.util.Set

size

public int size()
Returns the number of ranges in this set.

Specified by:
size in interface java.util.Set

add

public boolean add(java.lang.Object r)
            throws java.lang.ClassCastException
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(javax.media.jai.util.Range).

Note: current version do not support open interval (i.e. must return ). It may be fixed in a future version.

Specified by:
add in interface java.util.Set
Parameters:
r - The range to add. The class will never modify the supplied Range object.
Returns:
if this set changed as a result of the call.
Throws:
java.lang.ClassCastException - if the argument is not a Range object.
To Do:
support open intervals.

add

public boolean add(java.lang.Comparable lower,
                   java.lang.Comparable upper)
            throws java.lang.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:
if this set changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if is greater than .

add

public boolean add(byte lower,
                   byte upper)
            throws java.lang.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:
if this set changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if is greater than .

add

public boolean add(short lower,
                   short upper)
            throws java.lang.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:
if this set changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if is greater than .

add

public boolean add(int lower,
                   int upper)
            throws java.lang.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:
if this set changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if is greater than .

add

public boolean add(long lower,
                   long upper)
            throws java.lang.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:
if this set changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if is greater than .

add

public boolean add(float lower,
                   float upper)
            throws java.lang.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:
if this set changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if is greater than .

add

public boolean add(double lower,
                   double upper)
            throws java.lang.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:
if this set changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if is greater than .

remove

public boolean remove(java.lang.Comparable lower,
                      java.lang.Comparable upper)
               throws java.lang.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:
if this set changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if is greater than .

remove

public boolean remove(byte lower,
                      byte upper)
               throws java.lang.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:
if this set changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if is greater than .

remove

public boolean remove(short lower,
                      short upper)
               throws java.lang.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:
if this set changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if is greater than .

remove

public boolean remove(int lower,
                      int upper)
               throws java.lang.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:
if this set changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if is greater than .

remove

public boolean remove(long lower,
                      long upper)
               throws java.lang.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:
if this set changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if is greater than .

remove

public boolean remove(float lower,
                      float upper)
               throws java.lang.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:
if this set changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if is greater than .

remove

public boolean remove(double lower,
                      double upper)
               throws java.lang.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:
if this set changed as a result of the call.
Throws:
java.lang.IllegalArgumentException - if is greater than .

getMinValueAsDouble

public final double getMinValueAsDouble(int index)
                                 throws java.lang.IndexOutOfBoundsException,
                                        java.lang.ClassCastException
Returns a range's minimum value as a . The can be any value from 0 inclusive to the set's size exclusive. The returned values always increase with .

Parameters:
index - The range index, from 0 inclusive to size exclusive.
Returns:
The minimum value for the range at the specified index.
Throws:
java.lang.IndexOutOfBoundsException - if is out of bounds.
java.lang.ClassCastException - if range elements are not convertible to numbers.

getMaxValueAsDouble

public final double getMaxValueAsDouble(int index)
                                 throws java.lang.IndexOutOfBoundsException,
                                        java.lang.ClassCastException
Returns a range's maximum value as a . The can be any value from 0 inclusive to the set's size exclusive. The returned values always increase with .

Parameters:
index - The range index, from 0 inclusive to size exclusive.
Returns:
The maximum value for the range at the specified index.
Throws:
java.lang.IndexOutOfBoundsException - if is out of bounds.
java.lang.ClassCastException - if range elements are not convertible to numbers.

indexOfRange

public int indexOfRange(java.lang.Comparable value)
If the specified value is inside a range, returns the index of this range. Otherwise, returns .

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(java.lang.Object object)
Returns if this set contains the specified element.

Specified by:
contains in interface java.util.Set

first

public java.lang.Object first()
                       throws java.util.NoSuchElementException
Returns the first (lowest) range currently in this sorted set.

Specified by:
first in interface java.util.SortedSet
Throws:
java.util.NoSuchElementException - if the set is empty.

last

public java.lang.Object last()
                      throws java.util.NoSuchElementException
Returns the last (highest) range currently in this sorted set.

Specified by:
last in interface java.util.SortedSet
Throws:
java.util.NoSuchElementException - if the set is empty.

subSet

public java.util.SortedSet subSet(java.lang.Object lower,
                                  java.lang.Object upper)
Returns a view of the portion of this sorted set whose elements range from , inclusive, to , exclusive.

Specified by:
subSet in interface java.util.SortedSet
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 java.util.SortedSet headSet(java.lang.Object upper)
Returns a view of the portion of this sorted set whose elements are strictly less than .

Specified by:
headSet in interface java.util.SortedSet
Parameters:
upper - High endpoint (exclusive) of the headSet.
Returns:
A view of the specified initial range of this sorted set.

tailSet

public java.util.SortedSet tailSet(java.lang.Object lower)
Returns a view of the portion of this sorted set whose elements are greater than or equal to .

Specified by:
tailSet in interface java.util.SortedSet
Parameters:
lower - Low endpoint (inclusive) of the tailSet.
Returns:
A view of the specified final range of this sorted set.

iterator

public java.util.Iterator iterator()
Returns an iterator over the elements in this set of ranges. All elements are Range objects.

Specified by:
iterator in interface java.util.Set

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 java.util.Set

equals

public boolean equals(java.lang.Object object)
Compares the specified object with this set of ranges for equality.

Specified by:
equals in interface java.util.Set

clone

public java.lang.Object clone()
Returns a clone of this range set.

Specified by:
clone in interface org.opengis.util.Cloneable

toString

public java.lang.String toString()
Returns a string representation of this set of ranges. The returned string is implementation dependent. It is usually provided for debugging purposes.



Copyright © GeoTools. All Rights Reserved.