Class Range

java.lang.Object
com.vaadin.flow.internal.Range
All Implemented Interfaces:
Serializable

public final class Range extends Object implements 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.

For internal use only. May be renamed or removed in a future release.

Since:
1.0
Author:
Vaadin Ltd
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static Range
    between(int start, int end)
    Creates a range between two integers.
    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
    Checks whether this range ends after the end of another range.
    boolean
    Checks whether this range ends before the start of another range.
    boolean
     
    expand(int startDelta, int endDelta)
    Creates a range that is expanded the given amounts in both ends.
    int
    Returns the exclusive end point of this range.
    int
    Returns the inclusive start point of this range.
    int
     
    boolean
    Checks whether this range and another range are at least partially covering the same values.
    boolean
    Checks whether the range has no elements between the start and end.
    boolean
    Checks whether this range is a subset of another range.
    int
    The number of integers contained in the range.
    offsetBy(int offset)
    Get a range that is based on this one, but offset by a number.
    Overlay this range with another one, and partition the ranges according to how they position relative to each other.
    restrictTo(Range bounds)
    Limits this range to be within the bounds of the provided range.
    splitAt(int integer)
    Split the range into two at a certain integer.
    splitAtFromStart(int length)
    Split the range into two after a certain number of integers into the range.
    boolean
    Checks whether this range starts after the end of another range.
    boolean
    Checks whether this range starts before the start of another range.
    Returns the range as a stream of integers.
     
    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 Details

    • 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:

      • Elements in this range that occur before elements in other.
      • Elements that are shared between the two ranges.
      • Elements in this range that occur after elements in other.
      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
    • 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 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 range, inclusive
      end - the end of the range, exclusive
      Returns:
      a range representing [start..end[
      Throws:
      IllegalArgumentException - if start > end
    • withLength

      public static Range withLength(int start, int length) throws 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:
      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 if and only if 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 if and only if integer is in this range
    • isSubsetOf

      public boolean isSubsetOf(Range other)
      Checks whether this range is a subset of another range.
      Parameters:
      other - the range to check against of
      Returns:
      true if and only if other completely wraps this range
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class 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 if and only if 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 if and only if 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 if and only if 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 if and only if 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 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:
      IllegalArgumentException - if the two ranges aren't connected
    • expand

      public Range expand(int startDelta, int endDelta) throws 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:
      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
    • stream

      public IntStream stream()
      Returns the range as a stream of integers.

      The first element of the stream is the getStart() return value and the last one is the getEnd() return value.

      Returns:
      the range as a stream
      See Also: