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

java.lang.Object
rgu.transport.algorithms.util.Range<T>
Type Parameters:
T - the type
All Implemented Interfaces:
Predicate<T>

public final class Range<T extends Comparable<? super T>> extends Object implements Predicate<T>
A non-empty range in between two values. This also implements the Predicate functional interface and tests true if the tested value is contained in the range.
Author:
Lee A. Christie
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T extends Comparable<? super T>>
    Range<T>
    any()
    Creates a range of all values (contains everything of type T).
    static <T extends Comparable<? super T>>
    Range<T>
    closed(T lower, T upper)
    Creates a closed range.
    static <T extends Comparable<? super T>>
    Range<T>
    halfOpen(T lower, T bound)
    Creates a half-open range.
    boolean
    intersects(Range<T> other)
    Checks if this range intersects another range or not.
    boolean
    Returns true if this range is an instance of any.
    boolean
    test(T object)
    Checks if a given object is contained with the range.
    Returns a string representation of the range, [lower, upper] is closed, [lower, bound) if half-open.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface Predicate

    and, negate, or
  • Method Details

    • closed

      public static <T extends Comparable<? super T>> Range<T> closed(T lower, T upper)
      Creates a closed range.
      Type Parameters:
      T - the type
      Parameters:
      lower - the minimum value in the range, not null
      upper - the maximum value (inclusive), not null
      Returns:
      a range
      Throws:
      IllegalArgumentException - if lower > bound
    • halfOpen

      public static <T extends Comparable<? super T>> Range<T> halfOpen(T lower, T bound)
      Creates a half-open range.
      Type Parameters:
      T - the type
      Parameters:
      lower - the minimum value in the range, not null
      bound - the upper bound (exclusive), not null
      Returns:
      a range
      Throws:
      IllegalArgumentException - if lower >= bound
    • any

      public static <T extends Comparable<? super T>> Range<T> any()
      Creates a range of all values (contains everything of type T).
      Type Parameters:
      T - the type
      Returns:
      a range
    • isAny

      public boolean isAny()
      Returns true if this range is an instance of any. Note that there may be other instances for a given type T for which test(Comparable) always returns true, but for which isAny() returns false.
      Returns:
      true if any, false otherwise
    • toString

      public String toString()
      Returns a string representation of the range, [lower, upper] is closed, [lower, bound) if half-open.
      Overrides:
      toString in class Object
      Returns:
      a string representation
    • intersects

      public boolean intersects(Range<T> other)
      Checks if this range intersects another range or not.
      Parameters:
      other - the other range, not null
      Returns:
      true if intersecting, false otherwise
    • test

      public boolean test(T object)
      Checks if a given object is contained with the range.
      Specified by:
      test in interface Predicate<T extends Comparable<? super T>>
      Parameters:
      object - the object, not null
      Returns:
      true if contained, false otherwise