sk.baka.ambient.commons
Class Interval

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

public final class Interval
extends Object
implements Iterable<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(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 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.
 Iterator<Integer> iterator()
           
 boolean startsWith(Interval other)
          Checks if given interval covers items at the beginning of this interval.
 Object subtract(Interval other)
          Subtracts given interval from this one and returns result.
 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(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

iterator

public Iterator<Integer> iterator()
Specified by:
iterator in interface Iterable<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 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.


Copyright © 2007-2008. All Rights Reserved.