Class Range<T extends java.lang.Comparable>

  • All Implemented Interfaces:
    java.io.Serializable

    public final class Range<T extends java.lang.Comparable>
    extends java.lang.Object
    implements java.io.Serializable
    Represents the range/interval with two bounds. Abstraction follows the semantics of the mathematical interval. The range can be unbounded or open from the left or/and unbounded from the right. The range supports half-open or closed bounds on both sides.

    The class has some very simple methods for usability. For example contains(Comparable) method can tell user whether this range contains argument or not. The contains(Range) helps to find out whether this range fully enclosing argument or not.

    For more details about how to use it, check out this article on vladmihalcea.com.

    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Range​(T lower, T upper, int mask, java.lang.Class<T> clazz)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String asString()  
      static Range<java.math.BigDecimal> bigDecimalRange​(java.lang.String range)
      Creates the BigDecimal range from provided string:
      private java.util.function.Function<T,​java.lang.String> boundToString()  
      static <T extends java.lang.Comparable<?>>
      Range<T>
      closed​(T lower, T upper)
      Creates the closed range with provided bounds.
      static <T extends java.lang.Comparable<?>>
      Range<T>
      closedInfinite​(T lower)
      Creates the left-bounded, left-closed and right-unbounded range with provided lower bound.
      static <T extends java.lang.Comparable<?>>
      Range<T>
      closedOpen​(T lower, T upper)
      Creates the left-closed, right-open range with provided bounds.
      boolean contains​(Range<T> range)
      Determines whether this range contains this point or not.
      boolean contains​(T point)
      Determines whether this range contains this point or not.
      static <R extends java.lang.Comparable<R>>
      Range<R>
      emptyRange​(java.lang.Class<R> clazz)  
      boolean equals​(java.lang.Object o)  
      (package private) java.lang.Class<T> getClazz()  
      int hashCode()  
      boolean hasLowerBound()  
      boolean hasMask​(int flag)  
      boolean hasUpperBound()  
      static <T extends java.lang.Comparable<?>>
      Range<T>
      infinite​(java.lang.Class<T> cls)
      Creates the unbounded at both ends range with provided upper bound.
      static <T extends java.lang.Comparable<?>>
      Range<T>
      infiniteClosed​(T upper)
      Creates the left-unbounded, right-bounded and right-closed range with provided upper bound.
      static <T extends java.lang.Comparable<?>>
      Range<T>
      infiniteOpen​(T upper)
      Creates the left-unbounded, right-bounded and right-open range with provided upper bound.
      static Range<java.lang.Integer> integerRange​(java.lang.String range)
      Creates the Integer range from provided string:
      private boolean isBounded()  
      boolean isLowerBoundClosed()  
      boolean isUpperBoundClosed()  
      static Range<java.time.LocalDate> localDateRange​(java.lang.String range)
      Creates the LocalDate range from provided string:
      static Range<java.time.LocalDateTime> localDateTimeRange​(java.lang.String range)
      Creates the LocalDateTime range from provided string:
      static Range<java.lang.Long> longRange​(java.lang.String range)
      Creates the Long range from provided string:
      T lower()
      Returns the lower bound of this range.
      static <T extends java.lang.Comparable>
      Range<T>
      ofString​(java.lang.String str, java.util.function.Function<java.lang.String,​T> converter, java.lang.Class<T> clazz)  
      static <T extends java.lang.Comparable<?>>
      Range<T>
      open​(T lower, T upper)
      Creates the open range with provided bounds.
      static <T extends java.lang.Comparable<?>>
      Range<T>
      openClosed​(T lower, T upper)
      Creates the left-open, right-closed range with provided bounds.
      static <T extends java.lang.Comparable<?>>
      Range<T>
      openInfinite​(T lower)
      Creates the left-bounded, left-open and right-unbounded range with provided lower bound.
      private static java.util.function.Function<java.lang.String,​java.time.LocalDateTime> parseLocalDateTime()  
      private static java.util.function.Function<java.lang.String,​java.time.ZonedDateTime> parseZonedDateTime()  
      java.lang.String toString()  
      private static java.util.function.Function<java.lang.String,​java.lang.String> unquote()  
      T upper()
      Returns the upper bound of this range.
      static Range<java.time.ZonedDateTime> zonedDateTimeRange​(java.lang.String rangeStr)
      Creates the ZonedDateTime range from provided string:
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • LOCAL_DATE_TIME

        private static final java.time.format.DateTimeFormatter LOCAL_DATE_TIME
      • ZONE_DATE_TIME

        private static final java.time.format.DateTimeFormatter ZONE_DATE_TIME
      • lower

        private final T extends java.lang.Comparable lower
      • upper

        private final T extends java.lang.Comparable upper
      • mask

        private final int mask
      • clazz

        private final java.lang.Class<T extends java.lang.Comparable> clazz
    • Constructor Detail

      • Range

        private Range​(T lower,
                      T upper,
                      int mask,
                      java.lang.Class<T> clazz)
    • Method Detail

      • closed

        public static <T extends java.lang.Comparable<?>> Range<T> closed​(T lower,
                                                                          T upper)
        Creates the closed range with provided bounds.

        The mathematical equivalent will be:

        
             [a, b] = {x | a <= x <= b}
         
        .
        Type Parameters:
        T - The type of bounds.
        Parameters:
        lower - The lower bound, never null.
        upper - The upper bound, never null.
        Returns:
        The closed range.
      • open

        public static <T extends java.lang.Comparable<?>> Range<T> open​(T lower,
                                                                        T upper)
        Creates the open range with provided bounds.

        The mathematical equivalent will be:

        
             (a, b) = {x | a < x < b}
         
        Type Parameters:
        T - The type of bounds.
        Parameters:
        lower - The lower bound, never null.
        upper - The upper bound, never null.
        Returns:
        The range.
      • openClosed

        public static <T extends java.lang.Comparable<?>> Range<T> openClosed​(T lower,
                                                                              T upper)
        Creates the left-open, right-closed range with provided bounds.

        The mathematical equivalent will be:

        
             (a, b] = {x | a < x <= b}
         
        Type Parameters:
        T - The type of bounds.
        Parameters:
        lower - The lower bound, never null.
        upper - The upper bound, never null.
        Returns:
        The range.
      • closedOpen

        public static <T extends java.lang.Comparable<?>> Range<T> closedOpen​(T lower,
                                                                              T upper)
        Creates the left-closed, right-open range with provided bounds.

        The mathematical equivalent will be:

        
             [a, b) = {x | a <= x < b}
         
        Type Parameters:
        T - The type of bounds.
        Parameters:
        lower - The lower bound, never null.
        upper - The upper bound, never null.
        Returns:
        The range.
      • openInfinite

        public static <T extends java.lang.Comparable<?>> Range<T> openInfinite​(T lower)
        Creates the left-bounded, left-open and right-unbounded range with provided lower bound.

        The mathematical equivalent will be:

        
             (a, +∞) = {x | x > a}
         
        Type Parameters:
        T - The type of bounds.
        Parameters:
        lower - The lower bound, never null.
        Returns:
        The range.
      • closedInfinite

        public static <T extends java.lang.Comparable<?>> Range<T> closedInfinite​(T lower)
        Creates the left-bounded, left-closed and right-unbounded range with provided lower bound.

        The mathematical equivalent will be:

        
             [a, +∞) = {x | x >= a}
         
        Type Parameters:
        T - The type of bounds.
        Parameters:
        lower - The lower bound, never null.
        Returns:
        The range.
      • infiniteOpen

        public static <T extends java.lang.Comparable<?>> Range<T> infiniteOpen​(T upper)
        Creates the left-unbounded, right-bounded and right-open range with provided upper bound.

        The mathematical equivalent will be:

        
             (-∞, b) = {x | x < b}
         
        Type Parameters:
        T - The type of bounds.
        Parameters:
        upper - The upper bound, never null.
        Returns:
        The range.
      • infiniteClosed

        public static <T extends java.lang.Comparable<?>> Range<T> infiniteClosed​(T upper)
        Creates the left-unbounded, right-bounded and right-closed range with provided upper bound.

        The mathematical equivalent will be:

        
             (-∞, b] = {x | x =< b}
         
        Type Parameters:
        T - The type of bounds.
        Parameters:
        upper - The upper bound, never null.
        Returns:
        The range.
      • infinite

        public static <T extends java.lang.Comparable<?>> Range<T> infinite​(java.lang.Class<T> cls)
        Creates the unbounded at both ends range with provided upper bound.

        The mathematical equivalent will be:

        
             (-∞, +∞) = ℝ
         
        Type Parameters:
        T - The type of bounds.
        Parameters:
        cls - The range class, never null.
        Returns:
        The infinite range.
      • ofString

        public static <T extends java.lang.Comparable> Range<T> ofString​(java.lang.String str,
                                                                         java.util.function.Function<java.lang.String,​T> converter,
                                                                         java.lang.Class<T> clazz)
      • bigDecimalRange

        public static Range<java.math.BigDecimal> bigDecimalRange​(java.lang.String range)
        Creates the BigDecimal range from provided string:
        
             Range<BigDecimal> closed = Range.bigDecimalRange("[0.1,1.1]");
             Range<BigDecimal> halfOpen = Range.bigDecimalRange("(0.1,1.1]");
             Range<BigDecimal> open = Range.bigDecimalRange("(0.1,1.1)");
             Range<BigDecimal> leftUnbounded = Range.bigDecimalRange("(,1.1)");
         
        Parameters:
        range - The range string, for example "[5.5,7.8]".
        Returns:
        The range of BigDecimals.
        Throws:
        java.lang.NumberFormatException - when one of the bounds are invalid.
      • integerRange

        public static Range<java.lang.Integer> integerRange​(java.lang.String range)
        Creates the Integer range from provided string:
        
             Range<Integer> closed = Range.integerRange("[1,5]");
             Range<Integer> halfOpen = Range.integerRange("(-1,1]");
             Range<Integer> open = Range.integerRange("(1,2)");
             Range<Integer> leftUnbounded = Range.integerRange("(,10)");
             Range<Integer> unbounded = Range.integerRange("(,)");
         
        Parameters:
        range - The range string, for example "[5,7]".
        Returns:
        The range of Integers.
        Throws:
        java.lang.NumberFormatException - when one of the bounds are invalid.
      • longRange

        public static Range<java.lang.Long> longRange​(java.lang.String range)
        Creates the Long range from provided string:
        
             Range<Long> closed = Range.longRange("[1,5]");
             Range<Long> halfOpen = Range.longRange("(-1,1]");
             Range<Long> open = Range.longRange("(1,2)");
             Range<Long> leftUnbounded = Range.longRange("(,10)");
             Range<Long> unbounded = Range.longRange("(,)");
         
        Parameters:
        range - The range string, for example "[5,7]".
        Returns:
        The range of Longs.
        Throws:
        java.lang.NumberFormatException - when one of the bounds are invalid.
      • localDateTimeRange

        public static Range<java.time.LocalDateTime> localDateTimeRange​(java.lang.String range)
        Creates the LocalDateTime range from provided string:
        
             Range<LocalDateTime> closed = Range.localDateTimeRange("[2014-04-28 16:00:49,2015-04-28 16:00:49]");
             Range<LocalDateTime> quoted = Range.localDateTimeRange("[\"2014-04-28 16:00:49\",\"2015-04-28 16:00:49\"]");
             Range<LocalDateTime> iso = Range.localDateTimeRange("[\"2014-04-28T16:00:49.2358\",\"2015-04-28T16:00:49\"]");
         

        The valid formats for bounds are:

        • yyyy-MM-dd HH:mm:ss[.SSSSSS]
        • yyyy-MM-dd'T'HH:mm:ss[.SSSSSS]
        Parameters:
        range - The range string, for example "[2014-04-28 16:00:49,2015-04-28 16:00:49]".
        Returns:
        The range of LocalDateTimes.
        Throws:
        java.time.format.DateTimeParseException - when one of the bounds are invalid.
      • localDateRange

        public static Range<java.time.LocalDate> localDateRange​(java.lang.String range)
        Creates the LocalDate range from provided string:
        
             Range<LocalDate> closed = Range.localDateRange("[2014-04-28,2015-04-289]");
             Range<LocalDate> quoted = Range.localDateRange("[\"2014-04-28\",\"2015-04-28\"]");
             Range<LocalDate> iso = Range.localDateRange("[\"2014-04-28\",\"2015-04-28\"]");
         

        The valid formats for bounds are:

        • yyyy-MM-dd
        • yyyy-MM-dd
        Parameters:
        range - The range string, for example "[2014-04-28,2015-04-28]".
        Returns:
        The range of LocalDates.
        Throws:
        java.time.format.DateTimeParseException - when one of the bounds are invalid.
      • zonedDateTimeRange

        public static Range<java.time.ZonedDateTime> zonedDateTimeRange​(java.lang.String rangeStr)
        Creates the ZonedDateTime range from provided string:
        
             Range<ZonedDateTime> closed = Range.zonedDateTimeRange("[2007-12-03T10:15:30+01:00\",\"2008-12-03T10:15:30+01:00]");
             Range<ZonedDateTime> quoted = Range.zonedDateTimeRange("[\"2007-12-03T10:15:30+01:00\",\"2008-12-03T10:15:30+01:00\"]");
             Range<ZonedDateTime> iso = Range.zonedDateTimeRange("[2011-12-03T10:15:30+01:00[Europe/Paris], 2012-12-03T10:15:30+01:00[Europe/Paris]]");
         

        The valid formats for bounds are:

        • yyyy-MM-dd HH:mm:ss[.SSSSSS]X
        • yyyy-MM-dd'T'HH:mm:ss[.SSSSSS]X
        Parameters:
        rangeStr - The range string, for example "[2011-12-03T10:15:30+01:00,2012-12-03T10:15:30+01:00]".
        Returns:
        The range of ZonedDateTimes.
        Throws:
        java.time.format.DateTimeParseException - when one of the bounds are invalid.
        java.lang.IllegalArgumentException - when bounds time zones are different.
      • parseLocalDateTime

        private static java.util.function.Function<java.lang.String,​java.time.LocalDateTime> parseLocalDateTime()
      • parseZonedDateTime

        private static java.util.function.Function<java.lang.String,​java.time.ZonedDateTime> parseZonedDateTime()
      • unquote

        private static java.util.function.Function<java.lang.String,​java.lang.String> unquote()
      • isBounded

        private boolean isBounded()
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • hasMask

        public boolean hasMask​(int flag)
      • isLowerBoundClosed

        public boolean isLowerBoundClosed()
      • isUpperBoundClosed

        public boolean isUpperBoundClosed()
      • hasLowerBound

        public boolean hasLowerBound()
      • hasUpperBound

        public boolean hasUpperBound()
      • lower

        public T lower()
        Returns the lower bound of this range. If null is returned then this range is left-unbounded.
        Returns:
        The lower bound.
      • upper

        public T upper()
        Returns the upper bound of this range. If null is returned then this range is right-unbounded.
        Returns:
        The upper bound.
      • contains

        public boolean contains​(T point)
        Determines whether this range contains this point or not.

        For example:

        
             assertTrue(integerRange("[1,2]").contains(1))
             assertTrue(integerRange("[1,2]").contains(2))
             assertTrue(integerRange("[-1,1]").contains(0))
             assertTrue(infinity(Integer.class).contains(Integer.MAX_VALUE))
             assertTrue(infinity(Integer.class).contains(Integer.MIN_VALUE))
        
             assertFalse(integerRange("(1,2]").contains(1))
             assertFalse(integerRange("(1,2]").contains(3))
             assertFalse(integerRange("[-1,1]").contains(0))
         
        Parameters:
        point - The point to check.
        Returns:
        Whether point in this range or not.
      • contains

        public boolean contains​(Range<T> range)
        Determines whether this range contains this point or not.

        For example:

        
             assertTrue(integerRange("[-2,2]").contains(integerRange("[-1,1]")))
             assertTrue(integerRange("(,)").contains(integerRange("(,)"))
        
             assertFalse(integerRange("[-2,2)").contains(integerRange("[-1,2]")))
             assertFalse(integerRange("(-2,2]").contains(1))
         
        Parameters:
        range - The range to check.
        Returns:
        Whether range in this range or not.
      • asString

        public java.lang.String asString()
      • boundToString

        private java.util.function.Function<T,​java.lang.String> boundToString()
      • getClazz

        java.lang.Class<T> getClazz()
      • emptyRange

        public static <R extends java.lang.Comparable<R>> Range<R> emptyRange​(java.lang.Class<R> clazz)