sk.baka.ambient.commons
Class Interval

java.lang.Object
  extended by sk.baka.ambient.commons.Interval
All Implemented Interfaces:
java.lang.Iterable<java.lang.Integer>

public final class Interval
extends java.lang.Object
implements java.lang.Iterable<java.lang.Integer>

Specifies an integer index interval. Immutable. Iterator simply iterates over indices contained in this interval.

Author:
Martin Vysny

Field Summary
static Interval EMPTY
          An empty interval.
 int end
          Index of last item in the interval.
 int length
          Number of successive items in the interval.
 int start
          Index of first item in the interval.
 
Constructor Summary
Interval(int start, int length)
          Creates new interval.
 
Method Summary
 boolean contains(int index)
          Checks if given item is contained in the interval.
 boolean contains(Interval other)
          Checks if given interval is contained in the interval.
 boolean endsWith(Interval other)
          Checks if given interval covers items at the end of this interval.
 boolean equals(int start, int end)
          Compares this interval to the interval specified by two integers.
 boolean equals(java.lang.Object o)
           
static Interval fromItem(int itemIndex)
          Returns interval which contains only a single item.
static Interval fromRange(int intervalStart, int intervalEnd)
          Creates given interval.
 int getMiddleItem()
          Returns the item in the middle of the interval.
 int hashCode()
           
 boolean isEmpty()
          Checks if at least a single item is in the interval.
 boolean isEndpoint(int i)
          Checks if given index is an endpoint of this interval (i.e.
 java.util.Iterator<java.lang.Integer> iterator()
           
 Interval[] split(int index)
          Splits this interval into two intervals at specified index.
 boolean startsWith(Interval other)
          Checks if given interval covers items at the beginning of this interval.
 java.lang.Object subtract(Interval other)
          Subtracts given interval from this one and returns result.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

start

public final int start
Index of first item in the interval.


length

public final int length
Number of successive items in the interval.


EMPTY

public static final Interval EMPTY
An empty interval.


end

public final int end
Index of last item in the interval.

Constructor Detail

Interval

public Interval(int start,
                int length)
Creates new interval.

Parameters:
start - Index of first item in the interval.
length - Number of successive items in the interval.
Method Detail

isEmpty

public boolean isEmpty()
Checks if at least a single item is in the interval.

Returns:
false if at least single item is in the interval, true otherwise.

contains

public boolean contains(int index)
Checks if given item is contained in the interval.

Parameters:
index - index of item
Returns:
true if the item belongs to the interval.

contains

public boolean contains(Interval other)
Checks if given interval is contained in the interval.

Parameters:
other - the other interval
Returns:
true if given interval belongs to the interval.

fromRange

public static Interval fromRange(int intervalStart,
                                 int intervalEnd)
Creates given interval. The interval includes both endpoints.

Parameters:
intervalStart - start of interval
intervalEnd - end of interval
Returns:
interval which includes both endpoints.

fromItem

public static Interval fromItem(int itemIndex)
Returns interval which contains only a single item.

Parameters:
itemIndex - the item index.
Returns:
interval containing only a single item.

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

iterator

public java.util.Iterator<java.lang.Integer> iterator()
Specified by:
iterator in interface java.lang.Iterable<java.lang.Integer>

isEndpoint

public boolean isEndpoint(int i)
Checks if given index is an endpoint of this interval (i.e. it is minimal or maximal index still belonging to this interval).

Parameters:
i - the index to check
Returns:
true if given index is an endpoint, false otherwise.

equals

public boolean equals(int start,
                      int end)
Compares this interval to the interval specified by two integers.

Parameters:
start - the first end of the interval
end - the second end of the interval
Returns:
true if two intervals contain the same items, false otherwise.

startsWith

public boolean startsWith(Interval other)
Checks if given interval covers items at the beginning of this interval.

Parameters:
other - the other interval
Returns:
true if other interval covers items at the beginning of this interval.

endsWith

public boolean endsWith(Interval other)
Checks if given interval covers items at the end of this interval.

Parameters:
other - the other interval
Returns:
true if other interval covers items at the end of this interval.

subtract

public java.lang.Object subtract(Interval other)
Subtracts given interval from this one and returns result.

Parameters:
other - the interval to subtract
Returns:
an Interval instance if result is a single interval, an array of intervals otherwise.

getMiddleItem

public int getMiddleItem()
Returns the item in the middle of the interval. Fails if the interval is empty.

Returns:
the middle item.

split

public Interval[] split(int index)
Splits this interval into two intervals at specified index.

Parameters:
index - split the interval at this index. This index will not be contained in returned intervals.
Returns:
a non-null array of two intervals.


Copyright © 2007-2009. All Rights Reserved.