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 2021/08/27 19:56:00 UTC

[commons-lang] branch master updated: Add IntStreams.

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


The following commit(s) were added to refs/heads/master by this push:
     new 94ffa01  Add IntStreams.
94ffa01 is described below

commit 94ffa01a031108c5a173a420cef3bde0b8efc8ed
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Aug 27 15:55:57 2021 -0400

    Add IntStreams.
---
 src/changes/changes.xml                            |  1 +
 .../apache/commons/lang3/stream/IntStreams.java    | 51 ++++++++++++++++++++++
 .../commons/lang3/stream/IntStreamsTest.java       | 37 ++++++++++++++++
 3 files changed, 89 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d681839..63b4eb5 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -72,6 +72,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add and use ArrayUtils.setAll(T[], Supplier).</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add BooleanConsumer.</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add IntToCharFunction.</action>
+    <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add IntStreams.</action>
     <!-- UPDATE -->
     <action                   type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump spotbugs-maven-plugin from 4.2.0 to 4.2.3 #735.</action>
     <action                   type="update" dev="ggregory" due-to="Dependabot, XenoAmess">Bump Bump actions/cache from v2.1.4 to v2.1.6 #742, #752, #764.</action>
diff --git a/src/main/java/org/apache/commons/lang3/stream/IntStreams.java b/src/main/java/org/apache/commons/lang3/stream/IntStreams.java
new file mode 100644
index 0000000..46badc2
--- /dev/null
+++ b/src/main/java/org/apache/commons/lang3/stream/IntStreams.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3.stream;
+
+import java.util.stream.IntStream;
+
+/**
+ * Factory for {@link IntStream}.
+ * <p>
+ * <small> Only a factory for now but could hold other functionality.</small>
+ * </p>
+ *
+ * @since 3.13.0
+ */
+public class IntStreams {
+
+    /**
+     * Shorthand for {@code IntStream.range(0, i)}.
+     *
+     * @param endExclusive the exclusive upper bound.
+     * @return a sequential {@code IntStream} for the range of {@code int} elements.
+     */
+    public static IntStream range(final int endExclusive) {
+        return IntStream.range(0, endExclusive);
+    }
+
+    /**
+     * Shorthand for {@code IntStream.rangeClosed(0, i)}.
+     *
+     * @param endInclusive the inclusive upper bound.
+     * @return a sequential {@code IntStream} for the range of {@code int} elements.
+     */
+    public static IntStream rangeClosed(final int endInclusive) {
+        return IntStream.rangeClosed(0, endInclusive);
+    }
+
+}
diff --git a/src/test/java/org/apache/commons/lang3/stream/IntStreamsTest.java b/src/test/java/org/apache/commons/lang3/stream/IntStreamsTest.java
new file mode 100644
index 0000000..e1786e1
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/stream/IntStreamsTest.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3.stream;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link IntStreams}.
+ */
+public class IntStreamsTest {
+
+    @Test
+    public void testRange() {
+        assertArrayEquals(new int[] {0, 1}, IntStreams.range(2).toArray());
+    }
+
+    @Test
+    public void testRangeClosed() {
+        assertArrayEquals(new int[] {0, 1, 2}, IntStreams.rangeClosed(2).toArray());
+    }
+}