org.jaitools.numeric
Class RangeUtils

java.lang.Object
  extended by org.jaitools.numeric.RangeUtils

public class RangeUtils
extends Object

Provides static helper methods to transform, sort and merge Range objects.

Since:
1.0
Version:
$Id$
Author:
Michael Bedward, Daniele Romagnoli, GeoSolutions S.A.S.

Constructor Summary
RangeUtils()
           
 
Method Summary
static
<T extends Number & Comparable>
List<Range<T>>
createComplement(Collection<Range<T>> ranges)
          Creates the complement of the given list of Ranges.
static
<T extends Number & Comparable>
List<Range<T>>
createComplement(Range<T> range)
          Creates the complement of a Range.
static
<T extends Number & Comparable>
Range<T>
intersection(Range<T> r1, Range<T> r2)
          Gets the intersection of two ranges.
static
<T extends Number & Comparable>
List<Range<T>>
simplify(Collection<Range<T>> ranges)
          Simplifies a collection of ranges by merging those which overlap.
static
<T extends Number & Comparable>
List<Range<T>>
sort(Collection<Range<T>> ranges)
          Sorts a collection of ranges into ascending order of min value, then max value.
static
<T extends Number & Comparable>
void
sortInPlace(List<Range<T>> ranges)
          Sorts a list of ranges into ascending order of min value, then max value.
static
<T extends Number & Comparable>
List<Range<T>>
subtract(Range<T> r1, Range<T> r2)
          Subtracts the first range from the second.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RangeUtils

public RangeUtils()
Method Detail

createComplement

public static <T extends Number & Comparable> List<Range<T>> createComplement(Range<T> range)
Creates the complement of a Range. This is equivalent to subtracting the input from the infinite interval.

If the input is a finite interval or point, the result will be a list of two Ranges. For example: the complement of [-5, 5) is made up of (-Inf, -5) and [5, Inf).

If the input is an interval open at one end, the result list will contain a single Range. For example: the complement of (-Inf, 5) is [5, Inf).

If the input is a point at positive or negative infinity its complement is, by convention, (-Inf, Inf).

If the input is the infinite interval (-Inf, Inf) the result list will be empty.

Type Parameters:
T - the value type
Parameters:
range - input range
Returns:
a list of 0, 1 or 2 Ranges which form the complement
See Also:
createComplement(java.util.Collection)

createComplement

public static <T extends Number & Comparable> List<Range<T>> createComplement(Collection<Range<T>> ranges)
Creates the complement of the given list of Ranges. This method first calls simplify(java.util.Collection>) on the inputs and then subtracts each of the resulting Ranges from the whole number line.

Type Parameters:
T - value type
Parameters:
ranges - input ranges
Returns:
a list of Ranges which form the complement (may be empty)
See Also:
createComplement(Range)

sort

public static <T extends Number & Comparable> List<Range<T>> sort(Collection<Range<T>> ranges)
Sorts a collection of ranges into ascending order of min value, then max value. Returns a new List of sorted ranges, leaving the input collection unmodified.

Type Parameters:
T - the value type
Parameters:
ranges - the ranges to sort
Returns:
sorted ranges as a List

sortInPlace

public static <T extends Number & Comparable> void sortInPlace(List<Range<T>> ranges)
Sorts a list of ranges into ascending order of min value, then max value.

Type Parameters:
T - the value type
Parameters:
ranges - the ranges to sort

simplify

public static <T extends Number & Comparable> List<Range<T>> simplify(Collection<Range<T>> ranges)
Simplifies a collection of ranges by merging those which overlap.

Type Parameters:
T - value type
Parameters:
ranges - input ranges to simplify
Returns:
simplified ranges sorted by min, then max end-points

intersection

public static <T extends Number & Comparable> Range<T> intersection(Range<T> r1,
                                                                   Range<T> r2)
Gets the intersection of two ranges.

Type Parameters:
T - value type
Parameters:
r1 - first range
r2 - second range
Returns:
the intersection as a new range; or null if there was no intersection

subtract

public static <T extends Number & Comparable> List<Range<T>> subtract(Range<T> r1,
                                                                     Range<T> r2)
Subtracts the first range from the second. If the two inputs do not intersect the result will be equal to the second. If the two inputs are equal, or the first input encloses the second, the result will be an empty list. If the first input is strictly contained within the second the result will be two ranges.

Type Parameters:
T - value type
Parameters:
r1 - the first range
r2 - the second range
Returns:
0, 1 or 2 ranges representing the result of r2 - r1


Copyright © 2009-2013. All Rights Reserved.