You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/10/13 14:31:06 UTC
(commons-lang) 01/03: Add IntegerRange.toIntStream()
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit c64cf945161ec9cad47d9b921387645c1a369053
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Oct 13 10:29:43 2024 -0400
Add IntegerRange.toIntStream()
---
src/changes/changes.xml | 1 +
src/main/java/org/apache/commons/lang3/IntegerRange.java | 12 ++++++++++++
src/test/java/org/apache/commons/lang3/IntegerRangeTest.java | 8 ++++++++
3 files changed, 21 insertions(+)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a448c90fd..e5b12ddae 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -62,6 +62,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1747" type="add" dev="ggregory" due-to="Oliver B. Fischer, Gary Gregory">Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">JavaVersion.JAVA_23.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">SystemUtils.IS_JAVA_23.</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add IntegerRange.toIntStream().</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 77 #1267, #1277, #1283, #1288.</action>
</release>
diff --git a/src/main/java/org/apache/commons/lang3/IntegerRange.java b/src/main/java/org/apache/commons/lang3/IntegerRange.java
index f3666ae89..eea7781ab 100644
--- a/src/main/java/org/apache/commons/lang3/IntegerRange.java
+++ b/src/main/java/org/apache/commons/lang3/IntegerRange.java
@@ -17,6 +17,8 @@
package org.apache.commons.lang3;
+import java.util.stream.IntStream;
+
/**
* Specializes {@link NumberRange} for {@link Integer}s.
*
@@ -81,4 +83,14 @@ public final class IntegerRange extends NumberRange<Integer> {
super(number1, number2, null);
}
+ /**
+ * Returns a sequential ordered {@code IntStream} from {@link #getMinimum()} (inclusive) to {@link #getMaximum()} (inclusive) by an incremental step of
+ * {@code 1}.
+ *
+ * @return a sequential {@code IntStream} for the range of {@code int} elements
+ * @since 3.18.0
+ */
+ public IntStream toIntStream() {
+ return IntStream.rangeClosed(getMinimum(), getMaximum());
+ }
}
diff --git a/src/test/java/org/apache/commons/lang3/IntegerRangeTest.java b/src/test/java/org/apache/commons/lang3/IntegerRangeTest.java
index 7c894f298..d30f36ffb 100644
--- a/src/test/java/org/apache/commons/lang3/IntegerRangeTest.java
+++ b/src/test/java/org/apache/commons/lang3/IntegerRangeTest.java
@@ -26,6 +26,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Comparator;
+import java.util.stream.IntStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -394,6 +395,13 @@ public class IntegerRangeTest extends AbstractLangTest {
SerializationUtils.clone(range1);
}
+ @Test
+ public void testToIntStream() {
+ try (IntStream stream = range1.toIntStream()) {
+ assertEquals(165, stream.sum());
+ }
+ }
+
@Test
public void testToString() {
assertNotNull(range1.toString());