TopoWeb Commons API

org.topoweb.util
Class Range

java.lang.Object
  |
  +--org.topoweb.util.Range
Direct Known Subclasses:
StringRange

public class Range
extends java.lang.Object

Encapsulates a semi-open range of comparable objects. This class introduces the abstraction that a null compares greater than any other object.

Once an instance is created, other objects can be inspected to see whether they are contained by the range using the instance's contains(Object) method.


Constructor Summary
Range(java.lang.Object lo, java.lang.Object hi, java.util.Comparator comparator)
          Creates a new instance with the specified bounds and comparator.
 
Method Summary
 boolean contains(java.lang.Object obj)
          Tests whether this instance contains the given object.
 java.util.Comparator getComparator()
          Returns this instance's comparator or null, if the range is defined by the natural ordering of the bounds of this instance.
 java.lang.Object getHi()
          Returns the exclusive upper bound for this range.
 java.lang.Object getLo()
          Returns the inclusive lower bound for this range.
 int outcode(java.lang.Object obj)
          Returns an outcode indicating the ordering of the given object relative to this instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Range

public Range(java.lang.Object lo,
             java.lang.Object hi,
             java.util.Comparator comparator)
      throws java.lang.ClassCastException,
             java.lang.IllegalArgumentException
Creates a new instance with the specified bounds and comparator. The value null represents the largest possible comparable object. If the comparator is null, then the parameters lo and hi are taken to implement java.lang.Comparable (unless hi is null) and the natural ordering of the bounds are used to define the range.

Parameters:
lo - the inclusive lower bound for this range
hi - the exclusive upper bound for this range, or null to indicate the highest possible upper bound
comparator - the comparator defining the ordering, or null to indicate the natural ordering
Throws:
java.lang.IllegalArgumentException - if lo is null; if lo <= hi (with the value null taking on the highest possible value
java.lang.ClassCastException - if either lo or a non-null hi do not implement the java.lang.Comparable interface; if thrown by either the comparator , or by lo.compareTo(hi) throws this exception
Method Detail

contains

public final boolean contains(java.lang.Object obj)
Tests whether this instance contains the given object. An object is contained in this range iff it is not less than this instance's lower bound and it is not equal to or greater than this instance's upper bound.

Parameters:
obj - the object inspected
Returns:
true if the instance contains obj; false, o.w.

outcode

public final int outcode(java.lang.Object obj)
                  throws java.lang.ClassCastException
Returns an outcode indicating the ordering of the given object relative to this instance.

Parameters:
obj - the object inspected
Returns:
-1, if obj is to the left of this range; 0, if obj is inside (contained) by this range; 1, o.w.
java.lang.ClassCastException

getLo

public final java.lang.Object getLo()
Returns the inclusive lower bound for this range. The value returned is never null.

Returns:
the lower bound

getHi

public final java.lang.Object getHi()
Returns the exclusive upper bound for this range. The value null represents the largest possible bound.

Returns:
the upper bound

getComparator

public final java.util.Comparator getComparator()
Returns this instance's comparator or null, if the range is defined by the natural ordering of the bounds of this instance.

Returns:
the comparator for this instance, or null if naturally ordered.

TopoWeb Commons API

Copyright (C) 2002 Babak Farhang

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.