Skip to content

Commit ef504bb

Browse files
author
zihluwang
committed
feat: added RangeUtil
RangeUtil is a utility class providing methods for generating streams of integers that emulate the behaviour of Python's range function.
1 parent a85c562 commit ef504bb

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* Copyright (C) 2024-2025 OnixByte.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
*
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.onixbyte.devkit.utils;
19+
20+
import java.util.stream.IntStream;
21+
22+
/**
23+
* {@code RangeUtil} is a utility class providing methods for generating streams of integers that
24+
* emulate the behaviour of Python's {@code range} function.
25+
* <p>
26+
* This class offers static methods to create ranges with various configurations. These methods
27+
* leverage the {@link IntStream} to provide efficient and versatile integer sequences.
28+
*
29+
* @author zihluwang
30+
* @see IntStream
31+
*/
32+
public final class RangeUtil {
33+
34+
/**
35+
* Private constructor prevent class being instantiated.
36+
*/
37+
private RangeUtil() {
38+
}
39+
40+
/**
41+
* Generates a stream of integers starting from {@code 0} up to the specified {@code end} value.
42+
* <p>
43+
* It creates a sequential, ordered {@code IntStream} that can be used for iteration or
44+
* further processing.
45+
* <p>
46+
* <b>Example Usage:</b>
47+
* <pre>{@code
48+
* RangeUtil.range(5).forEach(System.out::println);
49+
*
50+
* // Output:
51+
* // 0
52+
* // 1
53+
* // 2
54+
* // 3
55+
* // 4
56+
* }</pre>
57+
*
58+
* @param end upper-bound of the range (exclusive)
59+
* @return an {@code IntStream} of integers from {@code 0} (inclusive) to
60+
* {@code end} (exclusive)
61+
* @throws IllegalArgumentException if the given {@code end} value is less equal to 0
62+
* @see IntStream
63+
*/
64+
public static IntStream range(int end) {
65+
if (end <= 0) {
66+
throw new IllegalArgumentException("Parameter [end] should not less than 0, provided is " +
67+
end);
68+
}
69+
return IntStream.range(0, end);
70+
}
71+
72+
/**
73+
* Generates a stream of integers starting from the specified {@code start} value up to the
74+
* specified {@code end} value.
75+
* <p>
76+
* It creates a sequential, ordered {@code IntStream} that can be used for iteration or
77+
* further processing.
78+
* <p>
79+
* <b>Example Usage:</b>
80+
* <pre>{@code
81+
* RangeUtil.range(3, 8).forEach(System.out::println);
82+
*
83+
* // Output:
84+
* // 3
85+
* // 4
86+
* // 5
87+
* // 6
88+
* // 7
89+
* }</pre>
90+
*
91+
* @param start the starting value of the range (inclusive)
92+
* @param end upper-bound of the range (exclusive)
93+
* @return an {@code IntStream} of integers from {@code 0} (inclusive) to
94+
* {@code end} (exclusive)
95+
* @throws IllegalArgumentException if the given {@code end} value is less equal to 0
96+
* @see IntStream
97+
*/
98+
public static IntStream range(int start, int end) {
99+
if (end >= start) {
100+
throw new IllegalStateException("Parameter [start] should less than parameter [end].");
101+
}
102+
return IntStream.range(start, end);
103+
}
104+
105+
/**
106+
* Generates a stream of integers starting from the specified {@code start} value up to the
107+
* specified {@code end} value.
108+
* <p>
109+
* It creates a sequential, ordered {@code IntStream} that can be used for iteration or
110+
* further processing.
111+
* <p>
112+
* <b>Example Usage:</b>
113+
* <pre>{@code
114+
* RangeUtil.rangeClosed(3, 8).forEach(System.out::println);
115+
*
116+
* // Output:
117+
* // 3
118+
* // 4
119+
* // 5
120+
* // 6
121+
* // 7
122+
* // 8
123+
* }</pre>
124+
*
125+
* @param start the starting value of the range (inclusive)
126+
* @param end upper-bound of the range (inclusive)
127+
* @return an {@code IntStream} of integers from {@code 0} (inclusive) to
128+
* {@code end} (inclusive)
129+
* @throws IllegalArgumentException if the given {@code end} value is less equal to 0
130+
* @see IntStream
131+
*/
132+
public static IntStream rangeClosed(int start, int end) {
133+
return IntStream.rangeClosed(start, end);
134+
}
135+
136+
/**
137+
* Generates a stream of integers starting from the specified {@code start} value, increment by
138+
* the specified {@code step}, up to the specified {@code end} value.
139+
* <p>
140+
* It creates a sequential, ordered {@code IntStream} that can be used for iteration or
141+
* further processing.
142+
* <p>
143+
* <b>Example Usage:</b>
144+
* <pre>{@code
145+
* RangeUtil.range(3, 8, 2).forEach(System.out::println);
146+
*
147+
* // Output:
148+
* // 3
149+
* // 5
150+
* // 7
151+
* }</pre>
152+
*
153+
* @param start the starting value of the range (inclusive)
154+
* @param end upper-bound of the range (exclusive)
155+
* @param step the increment (or decrement) between each value
156+
* @return an {@code IntStream} of integers from {@code 0} (inclusive) to
157+
* {@code end} (exclusive)
158+
* @throws IllegalArgumentException if the given {@code end} value is less equal to 0
159+
* @see IntStream
160+
*/
161+
public static IntStream range(int start, int end, int step) {
162+
if (step == 0) {
163+
throw new IllegalArgumentException("Step value must not be zero.");
164+
}
165+
if ((step > 0 && start >= end) || (step < 0 && start <= end)) {
166+
throw new IllegalArgumentException("Range parameters are inconsistent with the step value.");
167+
}
168+
return IntStream.iterate(start, (n) -> n < end, (n) -> n + step);
169+
}
170+
171+
}

0 commit comments

Comments
 (0)