Class Range<T>

  • Type Parameters:
    T - The type of values in this range.

    public final class Range<T>
    extends Object
    A representation of a range that can be used with any type.

    Because there are no restrictions on type of values that can be used with this Range class, it is not able to "understand" values that are passed to its various factory methods. As a result, only minimal consistency checks are performed: null-checks, mostly. In particular, this class does not check that the lower bound is actually lower than the upper bound, because it has no idea what ordering to use. Checking the relative order of bounds is the responsibility of callers of the lowerBoundValue() and upperBoundValue() methods.

    • Method Detail

      • canonical

        public static <T> Range<T> canonical​(T lowerBoundValue,
                                             T upperBoundValue)
        Create a canonical range, i.e. a range in the form [lowerBoundValue, upperBoundValue) (lower bound included, upper bound excluded), or [lowerBoundValue, +Infinity] (both bounds included) if the upper bound is +Infinity.

        This is mostly useful when creating multiple, contiguous ranges, like for example in range aggregations.

        Type Parameters:
        T - The type of range bounds.
        Parameters:
        lowerBoundValue - The lower bound of the range. May be null to represent -Infinity (no lower bound),
        upperBoundValue - The upper bound of the range. May be null to represent +Infinity (no upper bound).
        Returns:
        The range [lowerBoundValue, upperBoundValue) (lower bound included, upper bound excluded), or [lowerBoundValue, +Infinity] (both bounds included) if the upper bound is +Infinity.
      • all

        public static <T> Range<T> all()
        Type Parameters:
        T - The type of range bounds.
        Returns:
        The range [-Infinity, +Infinity] (both bounds included).
      • between

        public static <T> Range<T> between​(T lowerBoundValue,
                                           T upperBoundValue)
        Type Parameters:
        T - The type of range bounds.
        Parameters:
        lowerBoundValue - The lower bound of the range. May be null to represent -Infinity (no lower bound),
        upperBoundValue - The upper bound of the range. May be null to represent +Infinity (no upper bound).
        Returns:
        The range [lowerBoundValue, upperBoundValue] (both bounds included).
      • between

        public static <T> Range<T> between​(T lowerBoundValue,
                                           RangeBoundInclusion lowerBoundInclusion,
                                           T upperBoundValue,
                                           RangeBoundInclusion upperBoundInclusion)
        Type Parameters:
        T - The type of range bounds.
        Parameters:
        lowerBoundValue - The value of the lower bound of the range. May be null to represent -Infinity (no lower bound).
        lowerBoundInclusion - Whether the lower bound is included in the range or excluded.
        upperBoundValue - The value of the upper bound of the range. May be null to represent +Infinity (no upper bound).
        upperBoundInclusion - Whether the upper bound is included in the range or excluded.
        Returns:
        A Range.
      • atLeast

        public static <T> Range<T> atLeast​(T lowerBoundValue)
        Type Parameters:
        T - The type of range bounds.
        Parameters:
        lowerBoundValue - The value of the lower bound of the range. Must not be null.
        Returns:
        The range [lowerBoundValue, +Infinity] (both bounds included).
      • greaterThan

        public static <T> Range<T> greaterThan​(T lowerBoundValue)
        Type Parameters:
        T - The type of range bounds.
        Parameters:
        lowerBoundValue - The value of the lower bound of the range. Must not be null.
        Returns:
        The range (lowerBoundValue, +Infinity] (lower bound excluded, upper bound included).
      • atMost

        public static <T> Range<T> atMost​(T upperBoundValue)
        Type Parameters:
        T - The type of range bounds.
        Parameters:
        upperBoundValue - The value of the upper bound of the range. Must not be null.
        Returns:
        The range [-Infinity, upperBoundValue] (both bounds included).
      • lessThan

        public static <T> Range<T> lessThan​(T upperBoundValue)
        Type Parameters:
        T - The type of range bounds.
        Parameters:
        upperBoundValue - The value of the upper bound of the range. Must not be null.
        Returns:
        The range [-Infinity, upperBoundValue) (lower bound included, upper bound excluded).
      • hashCode

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

        public Optional<T> lowerBoundValue()
        Returns:
        The value of the lower bound, or an empty optional to represent {-Infinity} (no lower bound).
      • upperBoundValue

        public Optional<T> upperBoundValue()
        Returns:
        The value of the lower bound, or an empty optional to represent {+Infinity} (no upper bound).
      • map

        public <R> Range<R> map​(Function<? super T,​? extends R> function)