Class IntInterval
java.lang.Object
cloud.opencode.base.collections.IntInterval
- All Implemented Interfaces:
Serializable, Iterable<Integer>
IntInterval - Immutable, zero-allocation integer range with O(1) random access
IntInterval - 零分配的不可变整数区间,支持 O(1) 随机访问
Represents a closed range of integers [from, to] with a given step,
similar to Python's range() but with inclusive endpoints.
No backing array is allocated; all values are computed on demand.
表示一个闭区间整数范围 [from, to],具有给定步长,
类似于 Python 的 range() 但端点均包含在内。
不分配底层数组,所有值按需计算。
Features | 主要功能:
- Zero-allocation: no backing array - 零分配:无底层数组
- O(1) size, get, contains - O(1) 的 size、get、contains
- Inclusive endpoints (like Eclipse Collections) - 闭区间端点(类似 Eclipse Collections)
- Auto-detected direction for ascending/descending ranges - 自动检测升序/降序方向
- IntStream integration - IntStream 集成
Usage Examples | 使用示例:
// Ascending range - 升序区间
IntInterval range = IntInterval.fromTo(1, 5); // [1, 2, 3, 4, 5]
// Descending range (auto-detected) - 降序区间(自动检测)
IntInterval desc = IntInterval.fromTo(5, 1); // [5, 4, 3, 2, 1]
// Custom step - 自定义步长
IntInterval stepped = IntInterval.fromToBy(1, 10, 3); // [1, 4, 7, 10]
// For-each loop - for-each 循环
for (int i : IntInterval.oneTo(10)) {
System.out.println(i);
}
// IntStream - IntStream
int sum = IntInterval.oneTo(100).stream().sum();
Performance | 性能特性:
- size: O(1) - size: O(1)
- get: O(1) - get: O(1)
- contains: O(1) - contains: O(1)
- toArray / toList: O(n) - toArray / toList: O(n)
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Overflow-safe: Uses long arithmetic internally - 溢出安全:内部使用 long 运算
- Since:
- JDK 25, opencode-base-collections V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionby(int newStep) Return a new interval with the same range but a different step.booleancontains(int value) Check if this interval contains the given value.booleanvoidforEach(IntConsumer action) Perform the given action for each element (primitive, no boxing).static IntIntervalfromTo(int from, int to) Create an interval fromfromtoto(inclusive) with auto-detected step.static IntIntervalfromToBy(int from, int to, int step) Create an interval fromfromtoto(inclusive) with an explicit step.intget(int index) Return the element at the given index (O(1)).intgetFirst()Return the first element of this interval.intgetLast()Return the last element of this interval.inthashCode()booleanisEmpty()Check if this interval is empty.iterator()Return aPrimitiveIterator.OfIntover the elements.static IntIntervaloneTo(int to) Create an interval from 1 toto(inclusive).reversed()Return a reversed interval.intsize()Return the number of elements in this interval.stream()Return anIntStreamover the elements of this interval.int[]toArray()Convert this interval to anintarray.toList()toString()Return a string representation such as"IntInterval[1..10 step 1]".static IntIntervalzeroTo(int to) Create an interval from 0 toto(inclusive).Methods inherited from interface Iterable
forEach, spliterator
-
Method Details
-
fromTo
Create an interval fromfromtoto(inclusive) with auto-detected step. 创建从from到to(闭区间)的区间,自动检测步长。Step is
1iffrom <= to, otherwise-1.如果
from <= to则步长为1,否则为-1。- Parameters:
from- the start value (inclusive) | 起始值(包含)to- the end value (inclusive) | 结束值(包含)- Returns:
- the interval | 区间
-
fromToBy
Create an interval fromfromtoto(inclusive) with an explicit step. 创建从from到to(闭区间)的区间,使用显式步长。- Parameters:
from- the start value (inclusive) | 起始值(包含)to- the end value (inclusive) | 结束值(包含)step- the step (must not be zero, must match direction) | 步长(不能为零,必须与方向匹配)- Returns:
- the interval | 区间
- Throws:
IllegalArgumentException- if step is zero or conflicts with direction | 如果步长为零或与方向冲突
-
zeroTo
Create an interval from 0 toto(inclusive). 创建从 0 到to(闭区间)的区间。- Parameters:
to- the end value (inclusive) | 结束值(包含)- Returns:
- the interval [0..to] | 区间 [0..to]
-
oneTo
Create an interval from 1 toto(inclusive). 创建从 1 到to(闭区间)的区间。- Parameters:
to- the end value (inclusive) | 结束值(包含)- Returns:
- the interval [1..to] | 区间 [1..to]
-
contains
public boolean contains(int value) Check if this interval contains the given value. 检查此区间是否包含给定值。- Parameters:
value- the value to check | 要检查的值- Returns:
- true if the value is in this interval | 如果值在此区间中则返回 true
-
size
public int size()Return the number of elements in this interval. 返回此区间中的元素数量。- Returns:
- the size | 大小
-
get
public int get(int index) Return the element at the given index (O(1)). 返回给定索引处的元素 (O(1))。- Parameters:
index- the zero-based index | 从零开始的索引- Returns:
- the element at the index | 索引处的元素
- Throws:
IndexOutOfBoundsException- if index is out of bounds | 如果索引越界
-
getFirst
public int getFirst()Return the first element of this interval. 返回此区间的第一个元素。- Returns:
- the first element | 第一个元素
- Throws:
NoSuchElementException- if the interval is empty | 如果区间为空
-
getLast
public int getLast()Return the last element of this interval. 返回此区间的最后一个元素。The last element is the largest value
vsuch thatv = from + k * stepandvdoes not exceedto.最后一个元素是满足
v = from + k * step且v不超过to的最大值。- Returns:
- the last element | 最后一个元素
- Throws:
NoSuchElementException- if the interval is empty | 如果区间为空
-
isEmpty
public boolean isEmpty()Check if this interval is empty. 检查此区间是否为空。- Returns:
- true if the interval has no elements | 如果区间没有元素则返回 true
-
by
Return a new interval with the same range but a different step. 返回具有相同范围但不同步长的新区间。- Parameters:
newStep- the new step | 新步长- Returns:
- a new interval with the new step | 使用新步长的新区间
- Throws:
IllegalArgumentException- if the new step is zero or conflicts with direction | 如果新步长为零或与方向冲突
-
reversed
Return a reversed interval. 返回反转的区间。The reversed interval starts at
getLast()and ends atfrom, with the negated step.反转区间从
getLast()开始到from结束,步长取反。- Returns:
- the reversed interval | 反转的区间
-
toArray
public int[] toArray()Convert this interval to anintarray. 将此区间转换为int数组。- Returns:
- an array containing all elements | 包含所有元素的数组
-
toList
-
stream
-
iterator
Return aPrimitiveIterator.OfIntover the elements. 返回元素上的PrimitiveIterator.OfInt。 -
forEach
Perform the given action for each element (primitive, no boxing). 对每个元素执行给定操作(原始类型,无装箱)。- Parameters:
action- the action to perform | 要执行的操作
-
equals
-
hashCode
-
toString
-