Class Range<T extends Comparable<? super T>>

java.lang.Object
com.landawn.abacus.util.Range<T>
Type Parameters:
T -
All Implemented Interfaces:
Immutable, Serializable

@Immutable public final class Range<T extends Comparable<? super T>> extends Object implements Serializable, Immutable

Note: it's copied from Apache Commons Lang developed at The Apache Software Foundation (http://www.apache.org/), or under the Apache License 2.0. The methods copied from other products/frameworks may be modified in this class.

An immutable range of objects from a minimum to maximum point inclusive.

#ThreadSafe# if the objects and comparator are thread-safe

Since:
3.0
Version:
$Id: Range.java 1565243 2014-02-06 13:37:12Z sebb $
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    The Enum BoundType.
  • Method Summary

    Modifier and Type
    Method
    Description
     
    static <T extends Comparable<? super T>>
    Range<T>
    closed(T min, T max)
     
    static <T extends Comparable<? super T>>
    Range<T>
    closedOpen(T min, T max)
     
    int
    compareTo(T element)
    Checks where the specified element occurs relative to this range.
    boolean
    contains(T element)
    Checks whether the specified element occurs within this range.
    boolean
    containsAll(Collection<? extends T> c)
     
    boolean
    Checks whether this range contains all the elements of the specified range.
    boolean
    Compares this range to another object to test if they are equal.
    int
    Gets a suitable hash code for the range.
    Calculate the intersection of this and an overlapping Range.
    boolean
    isAfter(T element)
    Checks whether this range is after the specified element.
    boolean
    Checks whether this range is completely after the specified range.
    boolean
    isBefore(T element)
    Checks whether this range is before the specified element.
    boolean
    Checks whether this range is completely before the specified range.
    boolean
    Checks if is empty.
    boolean
    isEndedBy(T element)
    Checks whether this range starts with the specified element.
    boolean
    Checks whether this range is overlapped by the specified range.
    boolean
    isStartedBy(T element)
    Checks whether this range starts with the specified element.
    static <T extends Comparable<? super T>>
    Range<T>
    just(T element)
    Obtains a range using the specified element as both the minimum and maximum in this range.
    Gets the minimum value in this range.
    <U extends Comparable<? super U>>
    Range<U>
    map(Function<T,U> mapper)
     
    static <T extends Comparable<? super T>>
    Range<T>
    open(T min, T max)
     
    static <T extends Comparable<? super T>>
    Range<T>
    openClosed(T min, T max)
     
    span(Range<T> other)
    Copied from Guava under Apache License v2.0
    Returns the minimal range that encloses both this range and other.
     
    Gets the maximum value in this range.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • just

      public static <T extends Comparable<? super T>> Range<T> just(T element)

      Obtains a range using the specified element as both the minimum and maximum in this range.

      The range uses the natural ordering of the elements to determine where values lie in the range.

      Type Parameters:
      T - the type of the elements in this range
      Parameters:
      element - the value to use for this range, not null
      Returns:
      Throws:
      IllegalArgumentException - if the element is null
    • open

      public static <T extends Comparable<? super T>> Range<T> open(T min, T max)
      Type Parameters:
      T -
      Parameters:
      min -
      max -
      Returns:
      Throws:
      IllegalArgumentException - if the 'min' or 'max' is null, or min > max.
    • openClosed

      public static <T extends Comparable<? super T>> Range<T> openClosed(T min, T max)
      Type Parameters:
      T -
      Parameters:
      min -
      max -
      Returns:
      Throws:
      IllegalArgumentException - if the 'min' or 'max' is null, or min > max.
    • closedOpen

      public static <T extends Comparable<? super T>> Range<T> closedOpen(T min, T max)
      Type Parameters:
      T -
      Parameters:
      min -
      max -
      Returns:
      Throws:
      IllegalArgumentException - if the 'min' or 'max' is null, or min > max.
    • closed

      public static <T extends Comparable<? super T>> Range<T> closed(T min, T max)
      Type Parameters:
      T -
      Parameters:
      min -
      max -
      Returns:
      Throws:
      IllegalArgumentException - if the 'min' or 'max' is null, or min > max.
    • map

      public <U extends Comparable<? super U>> Range<U> map(Function<T,U> mapper)
      Type Parameters:
      U -
      Parameters:
      mapper -
      Returns:
    • boundType

      public Range.BoundType boundType()
      Returns:
    • lowerEndpoint

      public T lowerEndpoint()

      Gets the minimum value in this range.

      Returns:
    • upperEndpoint

      public T upperEndpoint()

      Gets the maximum value in this range.

      Returns:
    • contains

      public boolean contains(T element)

      Checks whether the specified element occurs within this range.

      Parameters:
      element - the element to check for, null returns false
      Returns:
      true if the specified element occurs within this range
    • containsAll

      public boolean containsAll(Collection<? extends T> c)
      Parameters:
      c -
      Returns:
    • isStartedBy

      public boolean isStartedBy(T element)

      Checks whether this range starts with the specified element.

      Parameters:
      element - the element to check for, null returns false
      Returns:
      true if the specified element occurs within this range
    • isEndedBy

      public boolean isEndedBy(T element)

      Checks whether this range starts with the specified element.

      Parameters:
      element - the element to check for, null returns false
      Returns:
      true if the specified element occurs within this range
    • isAfter

      public boolean isAfter(T element)

      Checks whether this range is after the specified element.

      Parameters:
      element - the element to check for, null returns false
      Returns:
      true if this range is entirely after the specified element
    • isBefore

      public boolean isBefore(T element)

      Checks whether this range is before the specified element.

      Parameters:
      element - the element to check for, null returns false
      Returns:
      true if this range is entirely before the specified element
    • compareTo

      public int compareTo(T element)

      Checks where the specified element occurs relative to this range.

      Returns -1 if this range is before the specified element, 1 if the this range is after the specified element, otherwise 0 if the specified element is contained in this range.

      Parameters:
      element - the element to check for, not null
      Returns:
      -1, 0 or +1 depending on the element's location relative to the range
    • containsRange

      public boolean containsRange(Range<T> other)

      Checks whether this range contains all the elements of the specified range.

      Parameters:
      other - the range to check, null returns false
      Returns:
      true if this range contains the specified range
      Throws:
      RuntimeException - if ranges cannot be compared
    • isAfterRange

      public boolean isAfterRange(Range<T> other)

      Checks whether this range is completely after the specified range.

      Parameters:
      other - the range to check, null returns false
      Returns:
      true if this range is completely after the specified range
      Throws:
      RuntimeException - if ranges cannot be compared
    • isBeforeRange

      public boolean isBeforeRange(Range<T> other)

      Checks whether this range is completely before the specified range.

      Parameters:
      other - the range to check, null returns false
      Returns:
      true if this range is completely before the specified range
      Throws:
      RuntimeException - if ranges cannot be compared
    • isOverlappedBy

      public boolean isOverlappedBy(Range<T> other)

      Checks whether this range is overlapped by the specified range.

      Two ranges overlap if there is at least one element in common.

      Parameters:
      other - the range to test, null returns false
      Returns:
      true if the specified range overlaps with this range; otherwise, false
      Throws:
      RuntimeException - if ranges cannot be compared
    • intersection

      public u.Optional<Range<T>> intersection(Range<T> other)
      Calculate the intersection of this and an overlapping Range.
      Parameters:
      other - overlapping Range
      Returns:
      range representing the intersection of this and other, this if equal, or Optional.empty() if they're not overlapped.
      Since:
      3.0.1
    • span

      public Range<T> span(Range<T> other)
      Copied from Guava under Apache License v2.0
      Returns the minimal range that encloses both this range and other. For example, the span of [1..3] and (5..7) is [1..7).

      If the input ranges are connected, the returned range can also be called their union. If they are not, note that the span might contain values that are not contained in either input range.

      Like intersection, this operation is commutative, associative and idempotent. Unlike it, it is always well-defined for any two input ranges.

      Parameters:
      other -
      Returns:
    • isEmpty

      public boolean isEmpty()
      Checks if is empty.
      Returns:
      true, if is empty
    • equals

      public boolean equals(Object obj)

      Compares this range to another object to test if they are equal.

      .

      To be equal, the minimum and maximum values must be equal, which ignores any differences in the comparator.

      Overrides:
      equals in class Object
      Parameters:
      obj - the reference object with which to compare
      Returns:
      true if this object is equal
    • hashCode

      public int hashCode()

      Gets a suitable hash code for the range.

      Overrides:
      hashCode in class Object
      Returns:
      a hash code value for this object
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns: