Interface Range<C extends Comparable<C>>

  • Type Parameters:
    C - the type of this range
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @Immutable
    @ParametersAreNonnullByDefault
    @FunctionalInterface
    public interface Range<C extends Comparable<C>>
    A range (or "interval") defines the boundaries around a contiguous span of values of some Comparable type.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      static <C extends Comparable<C>>
      Range<C>
      all()
      Creates a range that contains every value of type C.
      static <C extends Comparable<C>>
      Range<C>
      and​(Range<C> r1, Range<C> r2)
      Creates a range that contains all values enclosed by both ranges.
      static <C extends Comparable<C>>
      Range<C>
      atLeast​(C lower)
      Creates a range that contains all values greater than or equal to lower.
      static <C extends Comparable<C>>
      Range<C>
      atMost​(C upper)
      Creates a range that contains all values less than or equal to upper.
      static <C extends Comparable<C>>
      Range<C>
      closed​(C lower, C upper)
      Creates a range that contains all values greater than or equal to lower and less than or equal to upper.
      static <C extends Comparable<C>>
      Range<C>
      closedOpen​(C lower, C upper)
      Creates a range that contains all values greater than or equal to lower and strictly less than upper.
      boolean contains​(C value)
      Returns true if value is within the bounds of this range.
      default boolean containsAll​(Iterable<? extends C> values)
      Returns true if every element in values is contained in this range.
      static <C extends Comparable<C>>
      Range<C>
      empty()
      Creates a range that does not contain any value.
      static <C extends Comparable<C>>
      Range<C>
      greaterThan​(C lower)
      Creates a range that contains all values strictly greater than lower.
      static <C extends Comparable<C>>
      Range<C>
      lessThan​(C upper)
      Creates a range that contains all values strictly less than upper.
      static <C extends Comparable<C>>
      Range<C>
      open​(C lower, C upper)
      Creates a range that contains all values strictly greater than lower and strictly less than upper.
      static <C extends Comparable<C>>
      Range<C>
      openClosed​(C lower, C upper)
      Creates a range that contains all values strictly greater than lower and less than or equal to upper.
      static <C extends Comparable<C>>
      Range<C>
      or​(Range<C> r1, Range<C> r2)
      Creates a range that contains all values enclosed by at least one range.
      static <C extends Comparable<C>>
      Range<C>
      singleton​(C value)
      Creates a range that contains only the given value.
      static <C extends Comparable<C>>
      Range<C>
      xor​(Range<C> r1, Range<C> r2)
      Creates a range that contains all values enclosed by one range, but not another.
    • Method Detail

      • open

        @Nonnull
        static <C extends Comparable<C>> Range<C> open​(C lower,
                                                       C upper)
        Creates a range that contains all values strictly greater than lower and strictly less than upper.
        Type Parameters:
        C - the type of the range
        Parameters:
        lower - the lower bound of the range (exclusive)
        upper - the upper bound of the range (exclusive)
        Returns:
        a new range
        Throws:
        NullPointerException - if any argument is null
        IllegalArgumentException - if lower is greater than upper
      • closed

        @Nonnull
        static <C extends Comparable<C>> Range<C> closed​(C lower,
                                                         C upper)
        Creates a range that contains all values greater than or equal to lower and less than or equal to upper.
        Type Parameters:
        C - the type of the range
        Parameters:
        lower - the lower bound of the range (inclusive)
        upper - the upper bound of the range (inclusive)
        Returns:
        a new range
        Throws:
        NullPointerException - if any argument is null
        IllegalArgumentException - if lower is greater than upper
      • openClosed

        @Nonnull
        static <C extends Comparable<C>> Range<C> openClosed​(C lower,
                                                             C upper)
        Creates a range that contains all values strictly greater than lower and less than or equal to upper.
        Type Parameters:
        C - the type of the range
        Parameters:
        lower - the lower bound of the range (exclusive)
        upper - the upper bound of the range (inclusive)
        Returns:
        a new range
        Throws:
        NullPointerException - if any argument is null
        IllegalArgumentException - if lower is greater than upper
      • closedOpen

        @Nonnull
        static <C extends Comparable<C>> Range<C> closedOpen​(C lower,
                                                             C upper)
        Creates a range that contains all values greater than or equal to lower and strictly less than upper.
        Type Parameters:
        C - the type of the range
        Parameters:
        lower - the lower bound of the range (inclusive)
        upper - the upper bound of the range (exclusive)
        Returns:
        a new instance of range
        Throws:
        NullPointerException - if any argument is null
        IllegalArgumentException - if lower is greater than upper
      • singleton

        @Nonnull
        static <C extends Comparable<C>> Range<C> singleton​(C value)
        Creates a range that contains only the given value. The returned range is closed on both ends.
        Type Parameters:
        C - the type of the range
        Parameters:
        value - the unique value of the range
        Returns:
        a new range
        Throws:
        NullPointerException - if any argument is null
        See Also:
        closed(Comparable, Comparable)
      • atLeast

        @Nonnull
        static <C extends Comparable<C>> Range<C> atLeast​(C lower)
        Creates a range that contains all values greater than or equal to lower.
        Type Parameters:
        C - the type of the range
        Parameters:
        lower - the lower bound of the range (inclusive)
        Returns:
        a new range
        Throws:
        NullPointerException - if any argument is null
      • atMost

        @Nonnull
        static <C extends Comparable<C>> Range<C> atMost​(C upper)
        Creates a range that contains all values less than or equal to upper.
        Type Parameters:
        C - the type of the range
        Parameters:
        upper - the upper bound of the range (inclusive)
        Returns:
        a new range
        Throws:
        NullPointerException - if any argument is null
      • greaterThan

        @Nonnull
        static <C extends Comparable<C>> Range<C> greaterThan​(C lower)
        Creates a range that contains all values strictly greater than lower.
        Type Parameters:
        C - the type of the range
        Parameters:
        lower - the lower bound of the range (exclusive)
        Returns:
        a new range
        Throws:
        NullPointerException - if any argument is null
      • lessThan

        @Nonnull
        static <C extends Comparable<C>> Range<C> lessThan​(C upper)
        Creates a range that contains all values strictly less than upper.
        Type Parameters:
        C - the type of the range
        Parameters:
        upper - the upper bound of the range (exclusive)
        Returns:
        a new range
        Throws:
        NullPointerException - if any argument is null
      • all

        @Nonnull
        static <C extends Comparable<C>> Range<C> all()
        Creates a range that contains every value of type C.
        Type Parameters:
        C - the type of the range
        Returns:
        a new range
      • empty

        @Nonnull
        static <C extends Comparable<C>> Range<C> empty()
        Creates a range that does not contain any value.
        Type Parameters:
        C - the type of the range
        Returns:
        a new range
      • and

        @Nonnull
        static <C extends Comparable<C>> Range<C> and​(Range<C> r1,
                                                      Range<C> r2)
        Creates a range that contains all values enclosed by both ranges.

        The intersection exists if and only if the two ranges are connected.

        Type Parameters:
        C - the type of the range
        Parameters:
        r1 - the first range
        r2 - the second range
        Returns:
        a new range
        Throws:
        NullPointerException - if any argument is null
        See Also:
        Boolean.logicalAnd(boolean, boolean)
      • contains

        boolean contains​(C value)
        Returns true if value is within the bounds of this range.
        Parameters:
        value - the value to be tested for inclusion
        Returns:
        true if value is within the bounds of this range
        Throws:
        NullPointerException - if any argument is null
      • containsAll

        default boolean containsAll​(Iterable<? extends C> values)
        Returns true if every element in values is contained in this range.
        Parameters:
        values - the value to be tested for inclusion
        Returns:
        true if every element in values is contained in this range
        Throws:
        NullPointerException - if any argument is null
        See Also:
        contains(Comparable)