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

java.lang.Object
cloud.opencode.base.core.Range<C>
Type Parameters:
C - the comparable type - 可比较类型
All Implemented Interfaces:
Predicate<C>

public final class Range<C extends Comparable<? super C>> extends Object implements Predicate<C>
Range - A contiguous span of values 范围 - 连续的值域

Represents a range of comparable values with configurable bounds.

表示具有可配置边界的可比较值的范围。

Bound Types | 边界类型:

  • [a, b] - closed (includes both endpoints) | 闭区间(包含两端点)
  • (a, b) - open (excludes both endpoints) | 开区间(不包含两端点)
  • [a, b) - closedOpen (includes lower, excludes upper) | 左闭右开
  • (a, b] - openClosed (excludes lower, includes upper) | 左开右闭
  • [a, +∞) - atLeast (includes lower, no upper) | 大于等于
  • (a, +∞) - greaterThan (excludes lower, no upper) | 大于
  • (-∞, b] - atMost (no lower, includes upper) | 小于等于
  • (-∞, b) - lessThan (no lower, excludes upper) | 小于
  • (-∞, +∞) - all (no bounds) | 全部

Usage Examples | 使用示例:

// Create ranges
Range<Integer> closed = Range.closed(1, 10);      // [1, 10]
Range<Integer> open = Range.open(1, 10);          // (1, 10)
Range<Integer> atLeast = Range.atLeast(5);        // [5, +∞)
Range<Integer> lessThan = Range.lessThan(100);    // (-∞, 100)

// Check containment
closed.contains(5);       // true
closed.contains(0);       // false

// Stream filtering | 流过滤
Range<Integer> range = Range.closed(1, 100);
List<Integer> inRange = numbers.stream().filter(range).toList();

// Check enclosure
Range.closed(1, 10).encloses(Range.closed(3, 7));  // true

// Intersection and span
Range<Integer> intersection = closed.intersection(Range.closed(5, 15));  // [5, 10]
Range<Integer> span = closed.span(Range.closed(15, 20));  // [1, 20]

Features | 主要功能:

  • Open, closed, and half-open intervals - 开区间、闭区间和半开区间
  • Containment checking - 包含检查
  • Intersection, union, and span operations - 交集、并集和跨度操作
  • Stream support for integer ranges - 整数范围的流支持
  • Predicate support for stream filtering (test) - 支持流过滤的 Predicate

Security | 安全性:

  • Thread-safe: Yes (immutable after creation) - 线程安全: 是(创建后不可变)
  • Null-safe: No, endpoints must not be null - 空值安全: 否,端点不可为null
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • closed

      public static <C extends Comparable<? super C>> Range<C> closed(C lower, C upper)
      Creates a closed range [lower, upper]. 创建闭区间 [lower, upper]。
    • open

      public static <C extends Comparable<? super C>> Range<C> open(C lower, C upper)
      Creates an open range (lower, upper). 创建开区间 (lower, upper)。
    • closedOpen

      public static <C extends Comparable<? super C>> Range<C> closedOpen(C lower, C upper)
      Creates a closed-open range [lower, upper). 创建左闭右开区间 [lower, upper)。
    • openClosed

      public static <C extends Comparable<? super C>> Range<C> openClosed(C lower, C upper)
      Creates an open-closed range (lower, upper]. 创建左开右闭区间 (lower, upper]。
    • atMost

      public static <C extends Comparable<? super C>> Range<C> atMost(C upper)
      Creates a range with no lower bound (-∞, upper]. 创建无下界范围 (-∞, upper]。
    • lessThan

      public static <C extends Comparable<? super C>> Range<C> lessThan(C upper)
      Creates a range with no lower bound (-∞, upper). 创建无下界范围 (-∞, upper)。
    • atLeast

      public static <C extends Comparable<? super C>> Range<C> atLeast(C lower)
      Creates a range with no upper bound [lower, +∞). 创建无上界范围 [lower, +∞)。
    • greaterThan

      public static <C extends Comparable<? super C>> Range<C> greaterThan(C lower)
      Creates a range with no upper bound (lower, +∞). 创建无上界范围 (lower, +∞)。
    • all

      public static <C extends Comparable<? super C>> Range<C> all()
      Creates a range containing all values (-∞, +∞). 创建包含所有值的范围 (-∞, +∞)。
    • singleton

      public static <C extends Comparable<? super C>> Range<C> singleton(C value)
      Creates a singleton range [value, value]. 创建单值范围 [value, value]。
    • encloseAll

      @SafeVarargs public static <C extends Comparable<? super C>> Range<C> encloseAll(C... values)
      Creates the minimal range enclosing all given values. 创建包含所有给定值的最小范围。
    • encloseAll

      public static <C extends Comparable<? super C>> Range<C> encloseAll(Iterable<C> values)
      Creates the minimal range enclosing all values in the iterable. 创建包含可迭代对象中所有值的最小范围。
    • hasLowerBound

      public boolean hasLowerBound()
      Returns true if this range has a lower bound. 如果此范围有下界返回 true。
    • hasUpperBound

      public boolean hasUpperBound()
      Returns true if this range has an upper bound. 如果此范围有上界返回 true。
    • lowerEndpoint

      public Optional<C> lowerEndpoint()
      Returns the lower endpoint if present. 返回下界端点(如果存在)。
    • upperEndpoint

      public Optional<C> upperEndpoint()
      Returns the upper endpoint if present. 返回上界端点(如果存在)。
    • lowerBoundType

      public Range.BoundType lowerBoundType()
      Returns the lower bound type. 返回下界类型。
    • upperBoundType

      public Range.BoundType upperBoundType()
      Returns the upper bound type. 返回上界类型。
    • isEmpty

      public boolean isEmpty()
      Returns true if this range is empty. 如果此范围为空返回 true。
    • contains

      public boolean contains(C value)
      Returns true if this range contains the given value. 如果此范围包含给定值返回 true。
    • test

      public boolean test(C value)
      Tests if the given value is contained in this range (Predicate support). 测试给定值是否包含在此范围内(Predicate 支持)。

      Enables using Range directly in stream filters and other Predicate-accepting APIs:

      使 Range 可以直接用于流过滤和其他接受 Predicate 的 API:

      Range<Integer> range = Range.closed(1, 10);
      List<Integer> filtered = list.stream().filter(range).toList();
      
      Specified by:
      test in interface Predicate<C extends Comparable<? super C>>
      Parameters:
      value - the value to test | 待测试的值
      Returns:
      true if the value is contained in this range | 如果值在范围内返回 true
    • containsAll

      @SafeVarargs public final boolean containsAll(C... values)
      Returns true if this range contains all given values. 如果此范围包含所有给定值返回 true。
    • containsAll

      public boolean containsAll(Iterable<C> values)
      Returns true if this range contains all values in the iterable. 如果此范围包含可迭代对象中的所有值返回 true。
    • encloses

      public boolean encloses(Range<C> other)
      Returns true if this range encloses another range. 如果此范围包含另一个范围返回 true。
    • isConnected

      public boolean isConnected(Range<C> other)
      Returns true if this range is connected to another range. 如果此范围与另一个范围相连返回 true。
    • intersection

      public Range<C> intersection(Range<C> other)
      Returns the intersection of this range with another. 返回此范围与另一个范围的交集。
    • span

      public Range<C> span(Range<C> other)
      Returns the minimal range enclosing both this range and another. 返回包含此范围和另一个范围的最小范围。
    • gap

      public Optional<Range<C>> gap(Range<C> other)
      Returns the gap between this range and another, if any. 返回此范围与另一个范围之间的间隙(如果有)。
    • canonical

      public Range<C> canonical(Function<C,C> nextValue)
      Returns a canonical form of this range using the given domain. 使用给定域返回此范围的规范形式。
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

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

      public String toString()
      Overrides:
      toString in class Object