Add LongRange.toLongStream()

This commit is contained in:
Gary Gregory 2024-10-13 10:34:04 -04:00
parent 4830e09b1c
commit 798caa4e51
3 changed files with 23 additions and 1 deletions

View File

@ -63,6 +63,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add JavaVersion.JAVA_23.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.IS_JAVA_23.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add IntegerRange.toIntStream().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add LongRange.toLongStream().</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>

View File

@ -17,6 +17,8 @@
package org.apache.commons.lang3;
import java.util.stream.LongStream;
/**
* Specializes {@link NumberRange} for {@link Long}s.
*
@ -70,7 +72,7 @@ public final class LongRange extends NumberRange<Long> {
}
/**
* Creates an instance.
* Creates a new instance.
*
* @param number1 the first element, not null
* @param number2 the second element, not null
@ -81,4 +83,15 @@ public final class LongRange extends NumberRange<Long> {
super(number1, number2, null);
}
/**
* Returns a sequential ordered {@code LongStream} from {@link #getMinimum()} (inclusive) to {@link #getMaximum()} (inclusive) by an incremental step of
* {@code 1}.
*
* @return a sequential {@code LongStream} for the range of {@code long} elements
* @since 3.18.0
*/
public LongStream toLongStream() {
return LongStream.rangeClosed(getMinimum(), getMaximum());
}
}

View File

@ -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.LongStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -409,6 +410,13 @@ public class LongRangeTest extends AbstractLangTest {
SerializationUtils.clone(range1);
}
@Test
public void testToIntStream() {
try (LongStream stream = range1.toLongStream()) {
assertEquals(165, stream.sum());
}
}
@Test
public void testToString() {
assertNotNull(range1.toString());