Class ImmutableRangeSet<C extends Comparable<? super C>>
java.lang.Object
cloud.opencode.base.collections.immutable.ImmutableRangeSet<C>
- Type Parameters:
C- the type of range endpoints | 范围端点类型
- All Implemented Interfaces:
RangeSet<C>, Serializable
public final class ImmutableRangeSet<C extends Comparable<? super C>>
extends Object
implements RangeSet<C>, Serializable
ImmutableRangeSet - Immutable Range Set
ImmutableRangeSet - 不可变范围集合
An immutable implementation of RangeSet that stores non-overlapping,
non-adjacent ranges sorted by lower bound. All mutation methods throw
UnsupportedOperationException.
RangeSet 的不可变实现,存储按下界排序的非重叠、非相邻范围。
所有变更方法抛出 UnsupportedOperationException。
Features | 主要功能:
- Immutable & thread-safe - 不可变且线程安全
- Binary search for efficient queries - 二分搜索实现高效查询
- Set operations (union, intersection, difference) - 集合操作(并集、交集、差集)
- Builder pattern using
TreeRangeSetfor coalescing - 使用 TreeRangeSet 合并的构建器模式
Usage Examples | 使用示例:
// Create via builder - 通过构建器创建
ImmutableRangeSet<Integer> rangeSet = ImmutableRangeSet.<Integer>builder()
.add(Range.closed(1, 10))
.add(Range.closed(20, 30))
.build();
// Query - 查询
boolean contains = rangeSet.contains(5); // true
Range<Integer> range = rangeSet.rangeContaining(5); // [1, 10]
// Set operations - 集合操作
ImmutableRangeSet<Integer> other = ImmutableRangeSet.of(Range.closed(5, 25));
ImmutableRangeSet<Integer> union = rangeSet.union(other);
ImmutableRangeSet<Integer> intersection = rangeSet.intersection(other);
Performance | 性能特性:
- contains / rangeContaining: O(log n) via binary search - O(log n) 通过二分搜索
- encloses / intersects: O(log n) - O(log n)
- complement / subRangeSet: O(n) - O(n)
- union / intersection / difference: O(n + m) - O(n + m)
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Null-safe: No (nulls not allowed) - 空值安全: 否(不允许空值)
- Since:
- JDK 25, opencode-base-collections V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classImmutableRangeSet.Builder<C extends Comparable<? super C>>Builder forImmutableRangeSet. -
Method Summary
Modifier and TypeMethodDescriptionvoidNot supported.voidNot supported.Return all ranges as a descending set.asRanges()Return all ranges as a set.static <C extends Comparable<? super C>>
ImmutableRangeSet.Builder<C> builder()Create a new builder forImmutableRangeSet.voidclear()Not supported.Return the complement of this range set.booleanCheck if the value is contained in any range.static <C extends Comparable<? super C>>
ImmutableRangeSet<C> Create an immutable range set by copying from aRangeSet.static <C extends Comparable<? super C>>
ImmutableRangeSet<C> Create an immutable range set by copying from an iterable of ranges.difference(ImmutableRangeSet<C> other) Return a new immutable range set representing the difference (this minus other).booleanCheck if this range set encloses the given range.booleanenclosesAll(RangeSet<C> other) Check if this range set encloses all ranges in the other set.booleaninthashCode()intersection(ImmutableRangeSet<C> other) Return a new immutable range set representing the intersection of this set and the other.booleanintersects(Range<C> otherRange) Check if this range set intersects the given range.booleanisEmpty()Check if this range set is empty.static <C extends Comparable<? super C>>
ImmutableRangeSet<C> of()Create an empty immutable range set.static <C extends Comparable<? super C>>
ImmutableRangeSet<C> Create an immutable range set containing a single range.rangeContaining(C value) Return the range containing the value, or null.voidNot supported.voidNot supported.span()Return the minimal range that encloses all ranges.subRangeSet(Range<C> view) Return a sub range set within the given range.toString()union(ImmutableRangeSet<C> other) Return a new immutable range set representing the union of this set and the other.
-
Method Details
-
of
Create an empty immutable range set. 创建空的不可变范围集合。- Type Parameters:
C- endpoint type | 端点类型- Returns:
- empty immutable range set | 空的不可变范围集合
-
of
Create an immutable range set containing a single range. 创建包含单个范围的不可变范围集合。- Type Parameters:
C- endpoint type | 端点类型- Parameters:
range- the range | 范围- Returns:
- immutable range set | 不可变范围集合
-
copyOf
- Type Parameters:
C- endpoint type | 端点类型- Parameters:
rangeSet- the range set to copy | 要复制的范围集合- Returns:
- immutable range set | 不可变范围集合
-
copyOf
public static <C extends Comparable<? super C>> ImmutableRangeSet<C> copyOf(Iterable<Range<C>> ranges) Create an immutable range set by copying from an iterable of ranges. Ranges are coalesced using aTreeRangeSet. 通过复制范围的可迭代对象创建不可变范围集合。使用TreeRangeSet合并范围。- Type Parameters:
C- endpoint type | 端点类型- Parameters:
ranges- the ranges | 范围- Returns:
- immutable range set | 不可变范围集合
-
builder
Create a new builder forImmutableRangeSet. 创建ImmutableRangeSet的新构建器。- Type Parameters:
C- endpoint type | 端点类型- Returns:
- new builder | 新构建器
-
contains
-
rangeContaining
Description copied from interface:RangeSetReturn the range containing the value, or null. 返回包含该值的范围,如果不存在则返回 null。- Specified by:
rangeContainingin interfaceRangeSet<C extends Comparable<? super C>>- Parameters:
value- the value | 值- Returns:
- the containing range, or null | 包含的范围,或 null
-
encloses
-
enclosesAll
Description copied from interface:RangeSetCheck if this range set encloses all ranges in the other set. 检查此范围集合是否完全包含另一个集合中的所有范围。- Specified by:
enclosesAllin interfaceRangeSet<C extends Comparable<? super C>>- Parameters:
other- the other range set | 另一个范围集合- Returns:
- true if all enclosed | 如果全部包含则返回 true
-
intersects
Description copied from interface:RangeSetCheck if this range set intersects the given range. 检查此范围集合是否与给定范围相交。- Specified by:
intersectsin interfaceRangeSet<C extends Comparable<? super C>>- Parameters:
otherRange- the range to check | 要检查的范围- Returns:
- true if intersects | 如果相交则返回 true
-
isEmpty
-
asRanges
-
asDescendingSetOfRanges
Description copied from interface:RangeSetReturn all ranges as a descending set. 以降序集合形式返回所有范围。- Specified by:
asDescendingSetOfRangesin interfaceRangeSet<C extends Comparable<? super C>>- Returns:
- the descending set of ranges | 降序范围集合
-
complement
Description copied from interface:RangeSetReturn the complement of this range set. 返回此范围集合的补集。- Specified by:
complementin interfaceRangeSet<C extends Comparable<? super C>>- Returns:
- the complement | 补集
-
subRangeSet
Description copied from interface:RangeSetReturn a sub range set within the given range. 返回给定范围内的子范围集合。- Specified by:
subRangeSetin interfaceRangeSet<C extends Comparable<? super C>>- Parameters:
view- the viewing range | 视图范围- Returns:
- the sub range set | 子范围集合
-
span
-
add
Not supported. Always throwsUnsupportedOperationException. 不支持。始终抛出UnsupportedOperationException。- Specified by:
addin interfaceRangeSet<C extends Comparable<? super C>>- Parameters:
range- the range | 范围- Throws:
UnsupportedOperationException- always | 始终抛出
-
remove
Not supported. Always throwsUnsupportedOperationException. 不支持。始终抛出UnsupportedOperationException。- Specified by:
removein interfaceRangeSet<C extends Comparable<? super C>>- Parameters:
range- the range | 范围- Throws:
UnsupportedOperationException- always | 始终抛出
-
addAll
Not supported. Always throwsUnsupportedOperationException. 不支持。始终抛出UnsupportedOperationException。- Specified by:
addAllin interfaceRangeSet<C extends Comparable<? super C>>- Parameters:
other- the other range set | 另一个范围集合- Throws:
UnsupportedOperationException- always | 始终抛出
-
removeAll
Not supported. Always throwsUnsupportedOperationException. 不支持。始终抛出UnsupportedOperationException。- Specified by:
removeAllin interfaceRangeSet<C extends Comparable<? super C>>- Parameters:
other- the other range set | 另一个范围集合- Throws:
UnsupportedOperationException- always | 始终抛出
-
clear
public void clear()Not supported. Always throwsUnsupportedOperationException. 不支持。始终抛出UnsupportedOperationException。- Specified by:
clearin interfaceRangeSet<C extends Comparable<? super C>>- Throws:
UnsupportedOperationException- always | 始终抛出
-
union
Return a new immutable range set representing the union of this set and the other. 返回表示此集合与另一个集合并集的新不可变范围集合。- Parameters:
other- the other immutable range set | 另一个不可变范围集合- Returns:
- union of both sets | 两个集合的并集
-
intersection
Return a new immutable range set representing the intersection of this set and the other. 返回表示此集合与另一个集合交集的新不可变范围集合。- Parameters:
other- the other immutable range set | 另一个不可变范围集合- Returns:
- intersection of both sets | 两个集合的交集
-
difference
Return a new immutable range set representing the difference (this minus other). 返回表示差集(此集合减去另一个集合)的新不可变范围集合。- Parameters:
other- the other immutable range set | 另一个不可变范围集合- Returns:
- difference of both sets | 两个集合的差集
-
equals
-
hashCode
-
toString
-