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>
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:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic <C extends Comparable<? super C>>
Range<C> all()Creates a range containing all values (-∞, +∞).static <C extends Comparable<? super C>>
Range<C> atLeast(C lower) Creates a range with no upper bound [lower, +∞).static <C extends Comparable<? super C>>
Range<C> atMost(C upper) Creates a range with no lower bound (-∞, upper].Returns a canonical form of this range using the given domain.static <C extends Comparable<? super C>>
Range<C> closed(C lower, C upper) Creates a closed range [lower, upper].static <C extends Comparable<? super C>>
Range<C> closedOpen(C lower, C upper) Creates a closed-open range [lower, upper).booleanReturns true if this range contains the given value.final booleancontainsAll(C... values) Returns true if this range contains all given values.booleancontainsAll(Iterable<C> values) Returns true if this range contains all values in the iterable.static <C extends Comparable<? super C>>
Range<C> encloseAll(C... values) Creates the minimal range enclosing all given values.static <C extends Comparable<? super C>>
Range<C> encloseAll(Iterable<C> values) Creates the minimal range enclosing all values in the iterable.booleanReturns true if this range encloses another range.booleanReturns the gap between this range and another, if any.static <C extends Comparable<? super C>>
Range<C> greaterThan(C lower) Creates a range with no upper bound (lower, +∞).inthashCode()booleanReturns true if this range has a lower bound.booleanReturns true if this range has an upper bound.intersection(Range<C> other) Returns the intersection of this range with another.booleanisConnected(Range<C> other) Returns true if this range is connected to another range.booleanisEmpty()Returns true if this range is empty.static <C extends Comparable<? super C>>
Range<C> lessThan(C upper) Creates a range with no lower bound (-∞, upper).Returns the lower bound type.Returns the lower endpoint if present.static <C extends Comparable<? super C>>
Range<C> open(C lower, C upper) Creates an open range (lower, upper).static <C extends Comparable<? super C>>
Range<C> openClosed(C lower, C upper) Creates an open-closed range (lower, upper].static <C extends Comparable<? super C>>
Range<C> singleton(C value) Creates a singleton range [value, value].Returns the minimal range enclosing both this range and another.booleanTests if the given value is contained in this range (Predicate support).toString()Returns the upper bound type.Returns the upper endpoint if present.
-
Method Details
-
closed
Creates a closed range [lower, upper]. 创建闭区间 [lower, upper]。 -
open
Creates an open range (lower, upper). 创建开区间 (lower, upper)。 -
closedOpen
Creates a closed-open range [lower, upper). 创建左闭右开区间 [lower, upper)。 -
openClosed
Creates an open-closed range (lower, upper]. 创建左开右闭区间 (lower, upper]。 -
atMost
Creates a range with no lower bound (-∞, upper]. 创建无下界范围 (-∞, upper]。 -
lessThan
Creates a range with no lower bound (-∞, upper). 创建无下界范围 (-∞, upper)。 -
atLeast
Creates a range with no upper bound [lower, +∞). 创建无上界范围 [lower, +∞)。 -
greaterThan
Creates a range with no upper bound (lower, +∞). 创建无上界范围 (lower, +∞)。 -
all
Creates a range containing all values (-∞, +∞). 创建包含所有值的范围 (-∞, +∞)。 -
singleton
Creates a singleton range [value, value]. 创建单值范围 [value, value]。 -
encloseAll
Creates the minimal range enclosing all given values. 创建包含所有给定值的最小范围。 -
encloseAll
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
-
upperEndpoint
-
lowerBoundType
Returns the lower bound type. 返回下界类型。 -
upperBoundType
Returns the upper bound type. 返回上界类型。 -
isEmpty
public boolean isEmpty()Returns true if this range is empty. 如果此范围为空返回 true。 -
contains
Returns true if this range contains the given value. 如果此范围包含给定值返回 true。 -
test
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:
testin interfacePredicate<C extends Comparable<? super C>>- Parameters:
value- the value to test | 待测试的值- Returns:
- true if the value is contained in this range | 如果值在范围内返回 true
-
containsAll
Returns true if this range contains all given values. 如果此范围包含所有给定值返回 true。 -
containsAll
-
encloses
-
isConnected
-
intersection
-
span
-
gap
-
canonical
-
equals
-
hashCode
-
toString
-