Record Class ShardingRange<C extends Comparable<? super C>>

java.lang.Object
java.lang.Record
me.ahoo.cosid.sharding.ShardingRange<C>
Type Parameters:
C - the type of the values
Record Components:
lower - the lower endpoint, null for none
lowerType - whether the lower endpoint belongs to the range
upper - the upper endpoint, null for none
upperType - whether the upper endpoint belongs to the range

public record ShardingRange<C extends Comparable<? super C>>(C extends Comparable<? super C> lower, BoundType lowerType, C extends Comparable<? super C> upper, BoundType upperType) extends Record
A range of sharding values, the query a RangeSharding answers. It replaces the Guava Range of 4.x, so that cosid-sharding depends on cosid-core alone (D7); the factory methods keep Guava's names.

A missing endpoint (null) means the range is unbounded on that side; its bound type is then BoundType.OPEN. A range is never inverted: the lower bound is at most the upper bound, and (a, a) is rejected, as in Guava. [a, a) and (a, a] are the empty ranges.

  • Constructor Details

    • ShardingRange

      public ShardingRange(C lower, BoundType lowerType, C upper, BoundType upperType)
      Checks the bounds.
  • Method Details

    • all

      public static <C extends Comparable<? super C>> ShardingRange<C> all()
      Every value.
      Type Parameters:
      C - the type of the values
      Returns:
      (-∞..+∞)
    • closed

      public static <C extends Comparable<? super C>> ShardingRange<C> closed(C lower, C upper)
      [lower..upper].
      Type Parameters:
      C - the type of the values
      Parameters:
      lower - the lower endpoint, included
      upper - the upper endpoint, included
      Returns:
      the range
    • open

      public static <C extends Comparable<? super C>> ShardingRange<C> open(C lower, C upper)
      (lower..upper).
      Type Parameters:
      C - the type of the values
      Parameters:
      lower - the lower endpoint, excluded
      upper - the upper endpoint, excluded
      Returns:
      the range
    • closedOpen

      public static <C extends Comparable<? super C>> ShardingRange<C> closedOpen(C lower, C upper)
      [lower..upper).
      Type Parameters:
      C - the type of the values
      Parameters:
      lower - the lower endpoint, included
      upper - the upper endpoint, excluded
      Returns:
      the range
    • openClosed

      public static <C extends Comparable<? super C>> ShardingRange<C> openClosed(C lower, C upper)
      (lower..upper].
      Type Parameters:
      C - the type of the values
      Parameters:
      lower - the lower endpoint, excluded
      upper - the upper endpoint, included
      Returns:
      the range
    • atLeast

      public static <C extends Comparable<? super C>> ShardingRange<C> atLeast(C lower)
      [lower..+∞).
      Type Parameters:
      C - the type of the values
      Parameters:
      lower - the lower endpoint, included
      Returns:
      the range
    • greaterThan

      public static <C extends Comparable<? super C>> ShardingRange<C> greaterThan(C lower)
      (lower..+∞).
      Type Parameters:
      C - the type of the values
      Parameters:
      lower - the lower endpoint, excluded
      Returns:
      the range
    • atMost

      public static <C extends Comparable<? super C>> ShardingRange<C> atMost(C upper)
      (-∞..upper].
      Type Parameters:
      C - the type of the values
      Parameters:
      upper - the upper endpoint, included
      Returns:
      the range
    • lessThan

      public static <C extends Comparable<? super C>> ShardingRange<C> lessThan(C upper)
      (-∞..upper).
      Type Parameters:
      C - the type of the values
      Parameters:
      upper - the upper endpoint, excluded
      Returns:
      the range
    • singleton

      public static <C extends Comparable<? super C>> ShardingRange<C> singleton(C value)
      [value..value].
      Type Parameters:
      C - the type of the values
      Parameters:
      value - the only value
      Returns:
      the range
    • hasLowerBound

      public boolean hasLowerBound()
      Whether the range has a lower endpoint.
      Returns:
      true when bounded below
    • hasUpperBound

      public boolean hasUpperBound()
      Whether the range has an upper endpoint.
      Returns:
      true when bounded above
    • lowerEndpoint

      public C lowerEndpoint()
      The lower endpoint.
      Returns:
      the lower endpoint
      Throws:
      IllegalStateException - when the range is unbounded below
    • upperEndpoint

      public C upperEndpoint()
      The upper endpoint.
      Returns:
      the upper endpoint
      Throws:
      IllegalStateException - when the range is unbounded above
    • lowerBoundType

      public BoundType lowerBoundType()
      Whether the lower endpoint belongs to the range.
      Returns:
      the lower bound type
      Throws:
      IllegalStateException - when the range is unbounded below
    • upperBoundType

      public BoundType upperBoundType()
      Whether the upper endpoint belongs to the range.
      Returns:
      the upper bound type
      Throws:
      IllegalStateException - when the range is unbounded above
    • isEmpty

      public boolean isEmpty()
      Whether the range holds no value: [a..a) or (a..a].
      Returns:
      true when empty
    • contains

      public boolean contains(C value)
      Whether the value lies in the range.
      Parameters:
      value - the value
      Returns:
      true when the range contains it
    • isConnected

      public boolean isConnected(ShardingRange<C> other)
      Whether some range, possibly empty, is enclosed by both ranges, as Guava's Range.isConnected: [1..2) and [2..3] are connected, [1..2) and (2..3] are not.
      Parameters:
      other - the other range
      Returns:
      true when connected
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • lower

      public C lower()
      Returns the value of the lower record component.
      Returns:
      the value of the lower record component
    • lowerType

      public BoundType lowerType()
      Returns the value of the lowerType record component.
      Returns:
      the value of the lowerType record component
    • upper

      public C upper()
      Returns the value of the upper record component.
      Returns:
      the value of the upper record component
    • upperType

      public BoundType upperType()
      Returns the value of the upperType record component.
      Returns:
      the value of the upperType record component