com.metamatrix.common.util
Class Intervals

java.lang.Object
  extended by com.metamatrix.common.util.Intervals
All Implemented Interfaces:
java.io.Serializable

public class Intervals
extends java.lang.Object
implements java.io.Serializable

This class can be used to represent a collection of intervals. As new intervals are added, they are merged into existing intervals to maintain the most compact description of the intervals that is possible. Interval endpoints are inclusive - a single index can be identified by an interval like [1,1].For instance, if you added the intervals 1-3, 5-9, 4-4, they would be stored as 1-9.

See Also:
Serialized Form

Constructor Summary
Intervals()
          Constructor for Intervals.
Intervals(int begin, int end)
           
 
Method Summary
 void addInterval(int begin, int end)
          Add an interval from begin to end, inclusive.
 Intervals addIntervals(Intervals additionalIntervals)
           
 boolean containsInterval(int begin, int end)
          Returns true if the specified interval is contained in the current intervals.
 Intervals copy()
           
 boolean equals(java.lang.Object obj)
          Compares two intervals for equality
 int[] getBoundingInterval()
           
 Intervals getIntersection(int begin, int end)
          Determine the portion of the current intervals that overlaps with the specified interval.
 Intervals getIntersectionIntervals(Intervals intersectionIntervals)
           
 java.util.List getIntervals()
          Return ordered list of intervals representing current intervals.
 int hashCode()
          Returns hash code for the set of intervals.
 boolean hasIntervals()
          True if this set of intervals contains any intervals.
 boolean isContiguous()
          Determine there is one interval with no gaps.
 void removeInterval(int begin, int end)
          Remove an interval from begin to end, inclusive.
 Intervals removeIntervals(Intervals intervalsToRemove)
           
 java.lang.String toString()
          Returns string representation of intervals.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Intervals

public Intervals()
Constructor for Intervals.


Intervals

public Intervals(int begin,
                 int end)
Method Detail

hasIntervals

public boolean hasIntervals()
True if this set of intervals contains any intervals.

Returns:
True if covers anything, false otherwise

addInterval

public void addInterval(int begin,
                        int end)
Add an interval from begin to end, inclusive.

Parameters:
begin - Begin index
end - End index
Throws:
java.lang.IllegalArgumentException - If begin > end

removeInterval

public void removeInterval(int begin,
                           int end)
Remove an interval from begin to end, inclusive. If the current intervals do not contain anything in removed interval, no error is thrown.

Parameters:
begin - Begin index
end - End index
Throws:
java.lang.IllegalArgumentException - If begin > end

removeIntervals

public Intervals removeIntervals(Intervals intervalsToRemove)

containsInterval

public boolean containsInterval(int begin,
                                int end)
Returns true if the specified interval is contained in the current intervals.

Parameters:
begin - Begin index
end - End index
Throws:
java.lang.IllegalArgumentException - If begin > end

getIntersection

public Intervals getIntersection(int begin,
                                 int end)
Determine the portion of the current intervals that overlaps with the specified interval. For example, if the current intervals were [1-5, 10-15] and we asked for the overlap with region [4-11], we would get a result [4-5, 10-11].

Parameters:
begin -
end -
Returns:
Intervals set defining overlap
Throws:
java.lang.IllegalArgumentException - If begin > end

getIntersectionIntervals

public Intervals getIntersectionIntervals(Intervals intersectionIntervals)

addIntervals

public Intervals addIntervals(Intervals additionalIntervals)

getIntervals

public java.util.List getIntervals()
Return ordered list of intervals representing current intervals. Each element of the returned list is an int[2] representing a begin/end pair of included interval.

Returns:
Ordered list of int[2] representing intervals

isContiguous

public boolean isContiguous()
Determine there is one interval with no gaps. Returns true for an empty interval.

Returns:
Boolean indicator of whether there is one interval.

equals

public boolean equals(java.lang.Object obj)
Compares two intervals for equality

Overrides:
equals in class java.lang.Object
Parameters:
obj - Other object
Returns:
True if this equal to obj

hashCode

public int hashCode()
Returns hash code for the set of intervals. This hash code is based on the intervals and WILL change with any add or remove call. This requires re-hashing the object.

Overrides:
hashCode in class java.lang.Object
Returns:
Hash code

toString

public java.lang.String toString()
Returns string representation of intervals. This should be used for debugging only.

Overrides:
toString in class java.lang.Object
Returns:
String representing intervals

getBoundingInterval

public int[] getBoundingInterval()

copy

public Intervals copy()


Copyright © 2009. All Rights Reserved.