com.vaadin.shared.ui.grid
Class Range

java.lang.Object
  extended by com.vaadin.shared.ui.grid.Range
All Implemented Interfaces:
java.io.Serializable

public final class Range
extends java.lang.Object
implements java.io.Serializable

An immutable representation of a range, marked by start and end points.

The range is treated as inclusive at the start, and exclusive at the end. I.e. the range [0..1[ has the length 1, and represents one integer: 0.

The range is considered empty if the start is the same as the end.

Since:
7.4
Author:
Vaadin Ltd
See Also:
Serialized Form

Method Summary
static Range between(int start, int end)
          Creates a range between two integers.
 Range combineWith(Range other)
          Combines two ranges to create a range containing all values in both ranges, provided there are no gaps between the ranges.
 boolean contains(int integer)
          Checks whether an integer is found within this range.
 boolean endsAfter(Range other)
          Checks whether this range ends after the end of another range.
 boolean endsBefore(Range other)
          Checks whether this range ends before the start of another range.
 boolean equals(java.lang.Object obj)
           
 Range expand(int startDelta, int endDelta)
          Creates a range that is expanded the given amounts in both ends.
 int getEnd()
          Returns the exclusive end point of this range.
 int getStart()
          Returns the inclusive start point of this range.
 int hashCode()
           
 boolean intersects(Range other)
          Checks whether this range and another range are at least partially covering the same values.
 boolean isEmpty()
          Checks whether the range has no elements between the start and end.
 boolean isSubsetOf(Range other)
          Checks whether this range is a subset of another range.
 int length()
          The number of integers contained in the range.
 Range offsetBy(int offset)
          Get a range that is based on this one, but offset by a number.
 Range[] partitionWith(Range other)
          Overlay this range with another one, and partition the ranges according to how they position relative to each other.
 Range restrictTo(Range bounds)
          Limits this range to be within the bounds of the provided range.
 Range[] splitAt(int integer)
          Split the range into two at a certain integer.
 Range[] splitAtFromStart(int length)
          Split the range into two after a certain number of integers into the range.
 boolean startsAfter(Range other)
          Checks whether this range starts after the end of another range.
 boolean startsBefore(Range other)
          Checks whether this range starts before the start of another range.
 java.lang.String toString()
           
static Range withLength(int start, int length)
          Creates a range from a start point, with a given length.
static Range withOnly(int integer)
          Creates a range object representing a single integer.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

withOnly

public static Range withOnly(int integer)
Creates a range object representing a single integer.

Parameters:
integer - the number to represent as a range
Returns:
the range represented by integer

between

public static Range between(int start,
                            int end)
                     throws java.lang.IllegalArgumentException
Creates a range between two integers.

The range start is inclusive and the end is exclusive. So, a range "between" 0 and 5 represents the numbers 0, 1, 2, 3 and 4, but not 5.

Parameters:
start - the start of the the range, inclusive
end - the end of the range, exclusive
Returns:
a range representing [start..end[
Throws:
java.lang.IllegalArgumentException - if start > end

withLength

public static Range withLength(int start,
                               int length)
                        throws java.lang.IllegalArgumentException
Creates a range from a start point, with a given length.

Parameters:
start - the first integer to include in the range
length - the length of the resulting range
Returns:
a range starting from start, with length number of integers following
Throws:
java.lang.IllegalArgumentException - if length < 0

getStart

public int getStart()
Returns the inclusive start point of this range.

Returns:
the start point of this range

getEnd

public int getEnd()
Returns the exclusive end point of this range.

Returns:
the end point of this range

length

public int length()
The number of integers contained in the range.

Returns:
the number of integers contained in the range

isEmpty

public boolean isEmpty()
Checks whether the range has no elements between the start and end.

Returns:
true iff the range contains no elements.

intersects

public boolean intersects(Range other)
Checks whether this range and another range are at least partially covering the same values.

Parameters:
other - the other range to check against
Returns:
true if this and other intersect

contains

public boolean contains(int integer)
Checks whether an integer is found within this range.

Parameters:
integer - an integer to test for presence in this range
Returns:
true iff integer is in this range

isSubsetOf

public boolean isSubsetOf(Range other)
Checks whether this range is a subset of another range.

Returns:
true iff other completely wraps this range

partitionWith

public Range[] partitionWith(Range other)
Overlay this range with another one, and partition the ranges according to how they position relative to each other.

The three partitions are returned as a three-element Range array:

Parameters:
other - the other range to act as delimiters.
Returns:
a three-element Range array of partitions depicting the elements before (index 0), shared/inside (index 1) and after (index 2).

offsetBy

public Range offsetBy(int offset)
Get a range that is based on this one, but offset by a number.

Parameters:
offset - the number to offset by
Returns:
a copy of this range, offset by offset

toString

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

hashCode

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

equals

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

startsBefore

public boolean startsBefore(Range other)
Checks whether this range starts before the start of another range.

Parameters:
other - the other range to compare against
Returns:
true iff this range starts before the other

endsBefore

public boolean endsBefore(Range other)
Checks whether this range ends before the start of another range.

Parameters:
other - the other range to compare against
Returns:
true iff this range ends before the other

endsAfter

public boolean endsAfter(Range other)
Checks whether this range ends after the end of another range.

Parameters:
other - the other range to compare against
Returns:
true iff this range ends after the other

startsAfter

public boolean startsAfter(Range other)
Checks whether this range starts after the end of another range.

Parameters:
other - the other range to compare against
Returns:
true iff this range starts after the other

splitAt

public Range[] splitAt(int integer)
Split the range into two at a certain integer.

Example: [5..10[.splitAt(7) == [5..7[, [7..10[

Parameters:
integer - the integer at which to split the range into two
Returns:
an array of two ranges, with [start..integer[ in the first element, and [integer..end[ in the second element.

If integer is less than start, [empty, this ] is returned. if integer is equal to or greater than end, [this, empty] is returned instead.


splitAtFromStart

public Range[] splitAtFromStart(int length)
Split the range into two after a certain number of integers into the range.

Calling this method is equivalent to calling splitAt(getStart()+length);

Example: [5..10[.splitAtFromStart(2) == [5..7[, [7..10[

Parameters:
length - the length at which to split this range into two
Returns:
an array of two ranges, having the length-first elements of this range, and the second range having the rest. If length ≤ 0, the first element will be empty, and the second element will be this range. If lengthlength(), the first element will be this range, and the second element will be empty.

combineWith

public Range combineWith(Range other)
                  throws java.lang.IllegalArgumentException
Combines two ranges to create a range containing all values in both ranges, provided there are no gaps between the ranges.

Parameters:
other - the range to combine with this range
Returns:
the combined range
Throws:
java.lang.IllegalArgumentException - if the two ranges aren't connected

expand

public Range expand(int startDelta,
                    int endDelta)
             throws java.lang.IllegalArgumentException
Creates a range that is expanded the given amounts in both ends.

Parameters:
startDelta - the amount to expand by in the beginning of the range
endDelta - the amount to expand by in the end of the range
Returns:
an expanded range
Throws:
java.lang.IllegalArgumentException - if the new range would have start > end

restrictTo

public Range restrictTo(Range bounds)
Limits this range to be within the bounds of the provided range.

This is basically an optimized way of calculating partitionWith(Range)[1] without the overhead of defining the parts that do not overlap.

If the two ranges do not intersect, an empty range is returned. There are no guarantees about the position of that range.

Parameters:
bounds - the bounds that the returned range should be limited to
Returns:
a bounded range


Copyright © 2000-2014 Vaadin Ltd. All Rights Reserved.