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 TreeRangeSet for 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:
  • Method Details

    • of

      public static <C extends Comparable<? super C>> ImmutableRangeSet<C> of()
      Create an empty immutable range set. 创建空的不可变范围集合。
      Type Parameters:
      C - endpoint type | 端点类型
      Returns:
      empty immutable range set | 空的不可变范围集合
    • of

      public static <C extends Comparable<? super C>> ImmutableRangeSet<C> of(Range<C> range)
      Create an immutable range set containing a single range. 创建包含单个范围的不可变范围集合。
      Type Parameters:
      C - endpoint type | 端点类型
      Parameters:
      range - the range | 范围
      Returns:
      immutable range set | 不可变范围集合
    • copyOf

      public static <C extends Comparable<? super C>> ImmutableRangeSet<C> copyOf(RangeSet<C> rangeSet)
      Create an immutable range set by copying from a RangeSet. 通过复制 RangeSet 创建不可变范围集合。
      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 a TreeRangeSet. 通过复制范围的可迭代对象创建不可变范围集合。使用 TreeRangeSet 合并范围。
      Type Parameters:
      C - endpoint type | 端点类型
      Parameters:
      ranges - the ranges | 范围
      Returns:
      immutable range set | 不可变范围集合
    • builder

      public static <C extends Comparable<? super C>> ImmutableRangeSet.Builder<C> builder()
      Create a new builder for ImmutableRangeSet. 创建 ImmutableRangeSet 的新构建器。
      Type Parameters:
      C - endpoint type | 端点类型
      Returns:
      new builder | 新构建器
    • contains

      public boolean contains(C value)
      Description copied from interface: RangeSet
      Check if the value is contained in any range. 检查值是否包含在任何范围中。
      Specified by:
      contains in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      value - the value | 值
      Returns:
      true if contained | 如果包含则返回 true
    • rangeContaining

      public Range<C> rangeContaining(C value)
      Description copied from interface: RangeSet
      Return the range containing the value, or null. 返回包含该值的范围,如果不存在则返回 null。
      Specified by:
      rangeContaining in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      value - the value | 值
      Returns:
      the containing range, or null | 包含的范围,或 null
    • encloses

      public boolean encloses(Range<C> otherRange)
      Description copied from interface: RangeSet
      Check if this range set encloses the given range. 检查此范围集合是否完全包含给定范围。
      Specified by:
      encloses in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      otherRange - the range to check | 要检查的范围
      Returns:
      true if enclosed | 如果完全包含则返回 true
    • enclosesAll

      public boolean enclosesAll(RangeSet<C> other)
      Description copied from interface: RangeSet
      Check if this range set encloses all ranges in the other set. 检查此范围集合是否完全包含另一个集合中的所有范围。
      Specified by:
      enclosesAll in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      other - the other range set | 另一个范围集合
      Returns:
      true if all enclosed | 如果全部包含则返回 true
    • intersects

      public boolean intersects(Range<C> otherRange)
      Description copied from interface: RangeSet
      Check if this range set intersects the given range. 检查此范围集合是否与给定范围相交。
      Specified by:
      intersects in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      otherRange - the range to check | 要检查的范围
      Returns:
      true if intersects | 如果相交则返回 true
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: RangeSet
      Check if this range set is empty. 检查此范围集合是否为空。
      Specified by:
      isEmpty in interface RangeSet<C extends Comparable<? super C>>
      Returns:
      true if empty | 如果为空则返回 true
    • asRanges

      public Set<Range<C>> asRanges()
      Description copied from interface: RangeSet
      Return all ranges as a set. 以集合形式返回所有范围。
      Specified by:
      asRanges in interface RangeSet<C extends Comparable<? super C>>
      Returns:
      the set of ranges | 范围集合
    • asDescendingSetOfRanges

      public Set<Range<C>> asDescendingSetOfRanges()
      Description copied from interface: RangeSet
      Return all ranges as a descending set. 以降序集合形式返回所有范围。
      Specified by:
      asDescendingSetOfRanges in interface RangeSet<C extends Comparable<? super C>>
      Returns:
      the descending set of ranges | 降序范围集合
    • complement

      public RangeSet<C> complement()
      Description copied from interface: RangeSet
      Return the complement of this range set. 返回此范围集合的补集。
      Specified by:
      complement in interface RangeSet<C extends Comparable<? super C>>
      Returns:
      the complement | 补集
    • subRangeSet

      public RangeSet<C> subRangeSet(Range<C> view)
      Description copied from interface: RangeSet
      Return a sub range set within the given range. 返回给定范围内的子范围集合。
      Specified by:
      subRangeSet in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      view - the viewing range | 视图范围
      Returns:
      the sub range set | 子范围集合
    • span

      public Range<C> span()
      Description copied from interface: RangeSet
      Return the minimal range that encloses all ranges. 返回包含所有范围的最小范围。
      Specified by:
      span in interface RangeSet<C extends Comparable<? super C>>
      Returns:
      the span | 跨度
    • add

      public void add(Range<C> range)
      Not supported. Always throws UnsupportedOperationException. 不支持。始终抛出 UnsupportedOperationException
      Specified by:
      add in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      range - the range | 范围
      Throws:
      UnsupportedOperationException - always | 始终抛出
    • remove

      public void remove(Range<C> range)
      Not supported. Always throws UnsupportedOperationException. 不支持。始终抛出 UnsupportedOperationException
      Specified by:
      remove in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      range - the range | 范围
      Throws:
      UnsupportedOperationException - always | 始终抛出
    • addAll

      public void addAll(RangeSet<C> other)
      Not supported. Always throws UnsupportedOperationException. 不支持。始终抛出 UnsupportedOperationException
      Specified by:
      addAll in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      other - the other range set | 另一个范围集合
      Throws:
      UnsupportedOperationException - always | 始终抛出
    • removeAll

      public void removeAll(RangeSet<C> other)
      Not supported. Always throws UnsupportedOperationException. 不支持。始终抛出 UnsupportedOperationException
      Specified by:
      removeAll in interface RangeSet<C extends Comparable<? super C>>
      Parameters:
      other - the other range set | 另一个范围集合
      Throws:
      UnsupportedOperationException - always | 始终抛出
    • clear

      public void clear()
      Not supported. Always throws UnsupportedOperationException. 不支持。始终抛出 UnsupportedOperationException
      Specified by:
      clear in interface RangeSet<C extends Comparable<? super C>>
      Throws:
      UnsupportedOperationException - always | 始终抛出
    • union

      public ImmutableRangeSet<C> union(ImmutableRangeSet<C> other)
      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

      public ImmutableRangeSet<C> intersection(ImmutableRangeSet<C> other)
      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

      public ImmutableRangeSet<C> difference(ImmutableRangeSet<C> other)
      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

      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