@@ -63,7 +63,7 @@ private RangeUtil() {
6363 */
6464 public static IntStream range (int end ) {
6565 if (end <= 0 ) {
66- throw new IllegalArgumentException ("Parameter [end] should not less than 0, provided is " +
66+ throw new IllegalArgumentException ("Parameter [end] should not be less than or equal to 0, provided: " +
6767 end );
6868 }
6969 return IntStream .range (0 , end );
@@ -76,6 +76,10 @@ public static IntStream range(int end) {
7676 * It creates a sequential, ordered {@code IntStream} that can be used for iteration or
7777 * further processing.
7878 * <p>
79+ * If {@code start} is less than {@code end}, an ascending range (exclusive of {@code end})
80+ * is generated. If {@code start} is greater than {@code end}, a descending range (exclusive of {@code end})
81+ * is generated. If {@code start} equals {@code end}, an empty stream is returned.
82+ * <p>
7983 * <b>Example Usage:</b>
8084 * <pre>{@code
8185 * RangeUtil.range(3, 8).forEach(System.out::println);
@@ -86,20 +90,32 @@ public static IntStream range(int end) {
8690 * // 5
8791 * // 6
8892 * // 7
93+ *
94+ * RangeUtil.range(8, 3).forEach(System.out::println);
95+ *
96+ * // Output:
97+ * // 8
98+ * // 7
99+ * // 6
100+ * // 5
101+ * // 4
89102 * }</pre>
90103 *
91104 * @param start the starting value of the range (inclusive)
92105 * @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
106+ * @return an {@code IntStream} of integers in ascending or descending order, exclusive of {@code end}
96107 * @see IntStream
97108 */
98109 public static IntStream range (int start , int end ) {
99- if (end >= start ) {
100- throw new IllegalStateException ("Parameter [start] should less than parameter [end]." );
110+ if (start == end ) {
111+ return IntStream .empty ();
112+ }
113+ if (start < end ) {
114+ return IntStream .range (start , end );
115+ } else {
116+ // Descending range (exclusive of end)
117+ return IntStream .iterate (start , n -> n > end , n -> n - 1 );
101118 }
102- return IntStream .range (start , end );
103119 }
104120
105121 /**
@@ -109,6 +125,8 @@ public static IntStream range(int start, int end) {
109125 * It creates a sequential, ordered {@code IntStream} that can be used for iteration or
110126 * further processing.
111127 * <p>
128+ * The range includes both {@code start} and {@code end}.
129+ * <p>
112130 * <b>Example Usage:</b>
113131 * <pre>{@code
114132 * RangeUtil.rangeClosed(3, 8).forEach(System.out::println);
@@ -124,38 +142,47 @@ public static IntStream range(int start, int end) {
124142 *
125143 * @param start the starting value of the range (inclusive)
126144 * @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
145+ * @return an {@code IntStream} of integers from {@code start} to {@code end} inclusive
130146 * @see IntStream
131147 */
132148 public static IntStream rangeClosed (int start , int end ) {
133149 return IntStream .rangeClosed (start , end );
134150 }
135151
136152 /**
137- * Generates a stream of integers starting from the specified {@code start} value, increment by
153+ * Generates a stream of integers starting from the specified {@code start} value, incremented by
138154 * the specified {@code step}, up to the specified {@code end} value.
139155 * <p>
140156 * It creates a sequential, ordered {@code IntStream} that can be used for iteration or
141157 * further processing.
142158 * <p>
159+ * The stream excludes the {@code end} value.
160+ * <p>
143161 * <b>Example Usage:</b>
144162 * <pre>{@code
145- * RangeUtil.range(3, 8 , 2).forEach(System.out::println);
163+ * RangeUtil.range(3, 10 , 2).forEach(System.out::println);
146164 *
147165 * // Output:
148166 * // 3
149167 * // 5
150168 * // 7
169+ * // 9
170+ *
171+ * RangeUtil.range(10, 3, -2).forEach(System.out::println);
172+ *
173+ * // Output:
174+ * // 10
175+ * // 8
176+ * // 6
177+ * // 4
151178 * }</pre>
152179 *
153180 * @param start the starting value of the range (inclusive)
154181 * @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
182+ * @param step the increment or decrement between each value (non-zero)
183+ * @return an {@code IntStream} of integers from {@code start} to {@code end} exclusive stepping by {@code step}
184+ * @throws IllegalArgumentException if {@code step} is zero or if {@code start} and {@code end} are inconsistent
185+ * with the direction imposed by {@code step}
159186 * @see IntStream
160187 */
161188 public static IntStream range (int start , int end , int step ) {
@@ -165,7 +192,7 @@ public static IntStream range(int start, int end, int step) {
165192 if ((step > 0 && start >= end ) || (step < 0 && start <= end )) {
166193 throw new IllegalArgumentException ("Range parameters are inconsistent with the step value." );
167194 }
168- return IntStream .iterate (start , (n ) -> n < end , (n ) -> n + step );
195+ return IntStream .iterate (start , (n ) -> step > 0 ? n < end : n > end , (n ) -> n + step );
169196 }
170197
171198}
0 commit comments