You are currently viewing a snapshot of www.mozilla.org taken on April 21, 2008. Most of this content is highly out of date (some pages haven't been updated since the project began in 1998) and exists for historical purposes only. If there are any pages on this archive site that you think should be added back to www.mozilla.org, please file a bug.




org.mozilla.util
Class Range

java.lang.Object
  |
  +--org.mozilla.util.Range

public class Range
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

An Range is an object representing a range of integer values. A range has a start index and a count. The count must be greater than or equal to 0. Instances of this class are immutable, but subclasses may introduce mutability. A mutable range will return true from isMutable().

See Also:
, Serialized Form

Field Summary
protected  int count
          The length of the range
protected  int start
          The start index of the range
static Range ZeroRange
          A zero range
 
Constructor Summary
Range(int newStart, int newCount)
          Creates an instance of Range with a start index newStart and an extent newCount.
Range(Range otherRange)
          Creates an instance of Range from another Range object otherRange which must be non-null.
 
Method Summary
 java.lang.Object clone()
          Creates and returns an Range that is identical to this one.
 boolean containsIndex(int index)
          Returns true if and only if index lies within this range.
 boolean containsRange(Range otherRange)
          Returns true if and only if every element in otherRange is contained in this range.
 boolean equals(java.lang.Object otherRange)
          Returns true if and only if otherRange is an Range with the same start and count as this range.
protected  int getConstrainedInt(int number)
          Returns a number guaranteed to be within this range, including endpoints
 int getCount()
          Returns the number of elements in this range.
 int getEnd()
          Returns the the last index contained within the range.
 int getMax()
          Returns the max index (the index at start + count).
 int getStart()
          Returns the start index of the range.
 int hashCode()
          Overridden because equals() is overridden.
 boolean intersectsWithRange(Range otherRange)
          Returns true if and only if otherRange intersects with this range.
 boolean isAdjacentToRange(Range otherRange)
          Returns true if and only if otherRange is adjacent to this range; two ranges are adjacent if the max of one range is equal to the start of the other.
 boolean isAfterIndex(int index)
          Returns true if and only if the start index of this range is greater than index.
protected  boolean isAfterRange(Range otherRange)
          Returns true if this range is after otherRange and the two ranges do not overlap.
 boolean isBeforeIndex(int index)
          Return true if and only if the end index of this range is less than index.
 boolean isBeforeRange(Range otherRange)
          Returns true if and only if this range is before otherRange and the two ranges do not overlap.
 boolean isMutable()
          Returns true only if this instance can change after it is created. The default implementation returns false because instances of this class can't change; subclasses that introduce mutability should override this method to return true.
 int overlapWithRange(Range otherRange)
          Returns the number of elements that are in both this range and otherRange.
 Range rangeFromIntersection(Range otherRange)
          Returns the intersection of this range and otherRange.
 Range rangeFromUnion(Range otherRange)
          Returns the union of this range and otherRange.
 Range rangeShiftedByOffset(int offset)
          Returns this range, with its start shifted by offset.
 java.lang.String toString()
          Returns a String representation of this Range.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZeroRange

public static final Range ZeroRange
A zero range

start

protected int start
The start index of the range

count

protected int count
The length of the range
Constructor Detail

Range

public Range(Range otherRange)
Creates an instance of Range from another Range object otherRange which must be non-null.
Parameters:
range - the reference range to create this range from
Throws:
java.lang.IllegalArgumentException - if otherRange is null

Range

public Range(int newStart,
             int newCount)
Creates an instance of Range with a start index newStart and an extent newCount. newCount must be greater than or equal to 0.
Parameters:
newStart - the start index of the range
newCount - the number of elements in the range
Throws:
RangeException - if newCount is less than 0.
Method Detail

getStart

public int getStart()
Returns the start index of the range.
Returns:
the start index of this range

getCount

public int getCount()
Returns the number of elements in this range.
Returns:
the number of elements in this range

getEnd

public int getEnd()
Returns the the last index contained within the range.
Returns:
the end index of this range

getMax

public int getMax()
Returns the max index (the index at start + count).
Returns:
the max index.

getConstrainedInt

protected int getConstrainedInt(int number)
Returns a number guaranteed to be within this range, including endpoints
Returns:
a number within the range, including endpoints.

containsIndex

public boolean containsIndex(int index)
Returns true if and only if index lies within this range. Note that if the receiver is of zero length, then this method will return false.
Returns:
true if and only if and only if index lies within this range

isAfterIndex

public boolean isAfterIndex(int index)
Returns true if and only if the start index of this range is greater than index.
Returns:
true if and only if the start index of this range is greater than index

isBeforeIndex

public boolean isBeforeIndex(int index)
Return true if and only if the end index of this range is less than index.
Returns:
true if and only if the end index of this range is less than index

containsRange

public boolean containsRange(Range otherRange)
Returns true if and only if every element in otherRange is contained in this range. If the receiver is a zero length range, then this method will return false. Note that containment can apply to zero length ranges; a non-zero-length range contains any zero-length range. Contrast with range intersection.
Returns:
true if and only if every element in otherRange is contained in this range
Throws:
java.lang.IllegalArgumentException - if otherRange is null
See Also:
intersectsWithRange(Range otherRange)

intersectsWithRange

public boolean intersectsWithRange(Range otherRange)
Returns true if and only if otherRange intersects with this range. A zero-length range intersects with no range.
Returns:
true if and only if otherRange intersects with this range
Throws:
java.lang.IllegalArgumentException - if otherRange is null

overlapWithRange

public int overlapWithRange(Range otherRange)
Returns the number of elements that are in both this range and otherRange. A zero-length range has no overlapping elements with any range.
Parameters:
a - range to check overlaps with this range
Returns:
number of elements in both this range and otherRange
Throws:
java.lang.IllegalArgumentException - if otherRange is null

isAdjacentToRange

public boolean isAdjacentToRange(Range otherRange)
Returns true if and only if otherRange is adjacent to this range; two ranges are adjacent if the max of one range is equal to the start of the other. A zero length range is adjacent to no range.
Returns:
true if and only if otherRange is adjacent to this range
Throws:
java.lang.IllegalArgumentException - if otherRange is null

isBeforeRange

public boolean isBeforeRange(Range otherRange)
Returns true if and only if this range is before otherRange and the two ranges do not overlap.
Returns:
true if and only if this range is before otherRange and the two ranges do not overlap.
Throws:
java.lang.IllegalArgumentException - if otherRange is null

isAfterRange

protected boolean isAfterRange(Range otherRange)
Returns true if this range is after otherRange and the two ranges do not overlap.
Returns:
true if this range is after otherRange and the two ranges do not overlap.
Throws:
java.lang.IllegalArgumentException - if otherRange is null

rangeFromIntersection

public Range rangeFromIntersection(Range otherRange)
Returns the intersection of this range and otherRange. If the ranges do not intersect then Range.ZeroRange is returned. Note that a zero length range has no intersection with a non-zero length range.
Returns:
the intersection of this range and otherRange
Throws:
java.lang.IllegalArgumentException - if otherRange is null

rangeFromUnion

public Range rangeFromUnion(Range otherRange)
Returns the union of this range and otherRange. Returns the other range if one of them are zero length range. Returns Range.ZeroRange if both are zero length range.Note that the union of a non-zero length range with a zero length range is merely the non-zero length range.
Returns:
the union of this range and otherRange if both are non-zero range
Throws:
java.lang.IllegalArgumentException - if otherRange is null

rangeShiftedByOffset

public Range rangeShiftedByOffset(int offset)
Returns this range, with its start shifted by offset.
Returns:
this range, with its start offset by offset.

toString

public java.lang.String toString()
Returns a String representation of this Range.
Returns:
string representation of this range
Overrides:
toString in class java.lang.Object

isMutable

public boolean isMutable()
Returns true only if this instance can change after it is created.

The default implementation returns false because instances of this class can't change; subclasses that introduce mutability should override this method to return true.

Returns:
true if and only if the range may change

clone

public java.lang.Object clone()
Creates and returns an Range that is identical to this one.
Returns:
a reference to the immutable instance or a copy of the mutable instance
Overrides:
clone in class java.lang.Object

equals

public boolean equals(java.lang.Object otherRange)
Returns true if and only if otherRange is an Range with the same start and count as this range.
Returns:
true if and only if otherRange equals this range
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overridden because equals() is overridden. Returns a hash code that is based on the start and count of this range.
Returns:
a hash code based on the start index and count of this range
Overrides:
hashCode in class java.lang.Object