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 2023/08/04 23:57:56 UTC

[commons-lang] branch master updated: Add Functions#from(Function)

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 0ede42bbd Add Functions#from(Function)
0ede42bbd is described below

commit 0ede42bbdcf93560679a43ad1ac694fc6c05be59
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Aug 4 19:57:50 2023 -0400

    Add Functions#from(Function)
    
    Add FailableFunction#function(FailableFunction)
---
 pom.xml                                            |   4 +-
 src/changes/changes.xml                            |   5 +-
 .../commons/lang3/function/FailableFunction.java   |  16 +-
 .../apache/commons/lang3/function/Functions.java   |  38 ++++
 .../lang3/function/FailableFunctionsTest.java      | 229 +++++++++++----------
 .../commons/lang3/function/FunctionsTest.java      |  30 +++
 6 files changed, 208 insertions(+), 114 deletions(-)

diff --git a/pom.xml b/pom.xml
index 0ddeb6223..c8f915dd9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -26,7 +26,7 @@
   </parent>
   <modelVersion>4.0.0</modelVersion>
   <artifactId>commons-lang3</artifactId>
-  <version>3.13.1-SNAPSHOT</version>
+  <version>3.14.0-SNAPSHOT</version>
   <name>Apache Commons Lang</name>
 
   <inceptionYear>2001</inceptionYear>
@@ -607,7 +607,7 @@
     <commons.packageId>lang3</commons.packageId>
     <commons.module.name>org.apache.commons.lang3</commons.module.name>
     <!-- Current 3.x release series -->
-    <commons.release.version>3.13.1</commons.release.version>
+    <commons.release.version>3.14.0</commons.release.version>
     <commons.release.desc>(Java 8+)</commons.release.desc>
     <!-- Previous 2.x release series -->
     <commons.release.2.version>2.6</commons.release.2.version>
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index cbbebe2d0..f3c3a141c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -46,12 +46,15 @@ The <action> type attribute can be add,update,fix,remove.
   </properties>
   <body>
 
-  <release version="3.13.1" date="202Y-MM-DD" description="New features and bug fixes (Java 8).">
+  <release version="3.14.0" date="202Y-MM-DD" description="New features and bug fixes (Java 8).">
     <!-- FIX -->
     <action                   type="fix" dev="ggregory" due-to="remeio">Rename variable names from 'clss' to 'clazz' #1087.</action>
     <action                   type="fix" dev="ggregory" due-to="remeio">Javadoc: ComparableUtils'c1' to 'comparable1', 'c2' to '</action>
     <action                   type="fix" dev="ggregory" due-to="Elliotte Rusty Harold">Javadoc: Remove 2.1 specific comment #1091.</action>
     <action issue="LANG-1704" type="fix" dev="ggregory" due-to="Dan Ziemba, Gilles Sadowski, Alex Herbert, Gary Gregory">ImmutablePair and ImmutableTriple implementation don't match final in Javadoc.</action>
+    <!-- ADD -->
+    <action                   type="add" dev="ggregory" due-to="Rob Spoor, Gary Gregory">Add Functions#function(Function).</action>
+    <action                   type="add" dev="ggregory" due-to="Rob Spoor, Gary Gregory">Add FailableFunction#function(FailableFunction).</action>
     <!-- UPDATE -->
     <action                   type="update" dev="ggregory" due-to="Gary Gregory">Bump commons-parent from 58 to 59.</action>
   </release>
diff --git a/src/main/java/org/apache/commons/lang3/function/FailableFunction.java b/src/main/java/org/apache/commons/lang3/function/FailableFunction.java
index b37bce6c4..369be4aa9 100644
--- a/src/main/java/org/apache/commons/lang3/function/FailableFunction.java
+++ b/src/main/java/org/apache/commons/lang3/function/FailableFunction.java
@@ -35,6 +35,20 @@ public interface FailableFunction<T, R, E extends Throwable> {
     @SuppressWarnings("rawtypes")
     FailableFunction NOP = t -> null;
 
+    /**
+     * Starts a fluent chain like {@code function(foo::bar).andThen(...).andThen(...).apply(...);}
+     * 
+     * @param <T> Input type.
+     * @param <R> Return type.
+     * @param <E> The kind of thrown exception or error.
+     * @param function   the argument to return.
+     * @return the argument
+     * @since 3.14.0
+     */
+    static <T, R, E extends Throwable> FailableFunction<T, R, E> function(final FailableFunction<T, R, E> function) {
+        return function;
+    }
+
     /**
      * Returns a function that always returns its input argument.
      *
@@ -49,7 +63,7 @@ public interface FailableFunction<T, R, E extends Throwable> {
     /**
      * Returns The NOP singleton.
      *
-     * @param <T> Consumed type 1.
+     * @param <T> Consumed type.
      * @param <R> Return type.
      * @param <E> The kind of thrown exception or error.
      * @return The NOP singleton.
diff --git a/src/main/java/org/apache/commons/lang3/function/Functions.java b/src/main/java/org/apache/commons/lang3/function/Functions.java
new file mode 100644
index 000000000..97b82e761
--- /dev/null
+++ b/src/main/java/org/apache/commons/lang3/function/Functions.java
@@ -0,0 +1,38 @@
+/*
+ * 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.function;
+
+import java.util.function.Function;
+
+public class Functions {
+
+    /**
+     * Starts a fluent chain like {@code function(foo::bar).andThen(...).andThen(...).apply(...);}
+     * 
+     * @param <T> Input type.
+     * @param <R> Return type.
+     * @param <E> The kind of thrown exception or error.
+     * @param function   the argument to return.
+     * @return the argument
+     * @since 3.14.0
+     */
+    static <T, R> Function<T, R> function(final Function<T, R> function) {
+        return function;
+    }
+
+}
diff --git a/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java b/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java
index 37cd3a161..4914c054b 100644
--- a/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java
+++ b/src/test/java/org/apache/commons/lang3/function/FailableFunctionsTest.java
@@ -894,6 +894,110 @@ public class FailableFunctionsTest extends AbstractLangTest {
         assertThrows(NullPointerException.class, () -> nop.compose(null));
     }
 
+    @Test
+    public void testFailableBiFunctionNop() throws Throwable {
+        assertNull(FailableBiFunction.nop().apply("Foo", "Bar"), "Expect NOP to return null");
+    }
+
+    @Test
+    public void testFailableConsumerNop() throws Throwable {
+        // Expect nothing thrown
+        FailableConsumer.nop().accept("Foo");
+    }
+
+    @Test
+    public void testFailableDoubleFunctionNop() throws Throwable {
+        assertNull(FailableDoubleFunction.nop().apply(Double.MAX_VALUE), "Expect NOP to return null");
+    }
+
+    @Test
+    public void testFailableDoubleToIntFunctionNop() throws Throwable {
+        assertEquals(0, FailableDoubleToIntFunction.nop().applyAsInt(Double.MAX_VALUE), "Expect NOP to return 0");
+    }
+
+    @Test
+    public void testFailableDoubleToLongFunctionNop() throws Throwable {
+        assertEquals(0, FailableDoubleToLongFunction.nop().applyAsLong(Double.MAX_VALUE), "Expect NOP to return 0");
+    }
+
+    @Test
+    public void testFailableIntFunctionNop() throws Throwable {
+        assertNull(FailableIntFunction.nop().apply(Integer.MAX_VALUE), "Expect NOP to return null");
+    }
+
+    @Test
+    public void testFailableIntToDoubleFunctionNop() throws Throwable {
+        assertEquals(0, FailableIntToDoubleFunction.nop().applyAsDouble(Integer.MAX_VALUE), "Expect NOP to return 0");
+    }
+
+    @Test
+    public void testFailableIntToLongFunctionNop() throws Throwable {
+        assertEquals(0, FailableIntToLongFunction.nop().applyAsLong(Integer.MAX_VALUE), "Expect NOP to return 0");
+    }
+
+    @Test
+    public void testFailableLongFunctionNop() throws Throwable {
+        assertNull(FailableLongFunction.nop().apply(Long.MAX_VALUE), "Expect NOP to return null");
+    }
+
+    @Test
+    public void testFailableLongToDoubleFunctionNop() throws Throwable {
+        assertEquals(0, FailableLongToDoubleFunction.nop().applyAsDouble(Long.MAX_VALUE), "Expect NOP to return 0");
+    }
+
+    @Test
+    public void testFailableLongToIntFunctionNop() throws Throwable {
+        assertEquals(0, FailableLongToIntFunction.nop().applyAsInt(Long.MAX_VALUE), "Expect NOP to return 0");
+    }
+
+    @Test
+    public void testFailableObjDoubleConsumerNop() throws Throwable {
+        // Expect nothing thrown
+        FailableObjDoubleConsumer.nop().accept("Foo", Double.MAX_VALUE);
+    }
+
+    @Test
+    public void testFailableObjIntConsumerNop() throws Throwable {
+        // Expect nothing thrown
+        FailableObjIntConsumer.nop().accept("Foo", Integer.MAX_VALUE);
+    }
+
+    @Test
+    public void testFailableObjLongConsumerNop() throws Throwable {
+        // Expect nothing thrown
+        FailableObjLongConsumer.nop().accept("Foo", Long.MAX_VALUE);
+    }
+
+    @Test
+    public void testFailableToDoubleBiFunctionNop() throws Throwable {
+        assertEquals(0, FailableToDoubleBiFunction.nop().applyAsDouble("Foo", "Bar"), "Expect NOP to return 0");
+    }
+
+    @Test
+    public void testFailableToDoubleFunctionNop() throws Throwable {
+        assertEquals(0, FailableToDoubleFunction.nop().applyAsDouble("Foo"), "Expect NOP to return 0");
+    }
+
+    @Test
+    public void testFailableToIntBiFunctionNop() throws Throwable {
+        assertEquals(0, FailableToIntBiFunction.nop().applyAsInt("Foo", "Bar"), "Expect NOP to return 0");
+    }
+
+    @Test
+    public void testFailableToIntFunctionNop() throws Throwable {
+        assertEquals(0, FailableToIntFunction.nop().applyAsInt("Foo"), "Expect NOP to return 0");
+    }
+
+    @Test
+    public void testFailableToLongBiFunctionNop() throws Throwable {
+        assertEquals(0, FailableToLongBiFunction.nop().applyAsLong("Foo", "Bar"), "Expect NOP to return 0");
+    }
+
+    @Test
+    public void testFailableToLongFunctionNop() throws Throwable {
+        assertEquals(0, FailableToLongFunction.nop().applyAsLong("Foo"), "Expect NOP to return 0");
+    }
+
     @Test
     public void testFunction() {
         final Testable<?, ?> testable = new Testable<>(ILLEGAL_STATE_EXCEPTION);
@@ -958,6 +1062,11 @@ public class FailableFunctionsTest extends AbstractLangTest {
         assertThrows(NullPointerException.class, () -> failing.compose(null));
     }
 
+    @Test
+    public void testFunctionFunction() throws Exception {
+        assertEquals("foo", FailableFunction.function(this::throwingFunction).andThen(this::throwingFunction).apply("foo"));
+    }
+
     @Test
     public void testFunctionIdentity() throws Throwable {
         final FailableFunction<Integer, Integer, Throwable> nop = FailableFunction.identity();
@@ -1352,6 +1461,14 @@ public class FailableFunctionsTest extends AbstractLangTest {
         assertThrows(NullPointerException.class, () -> assertTrue(FailablePredicate.TRUE.and(null).test(null)));
     }
 
+    @Test
+    public void testPredicateNegate() throws Throwable {
+        assertFalse(FailablePredicate.TRUE.negate().test(null));
+        assertFalse(FailablePredicate.truePredicate().negate().test(null));
+        assertTrue(FailablePredicate.FALSE.negate().test(null));
+        assertTrue(FailablePredicate.falsePredicate().negate().test(null));
+    }
+
     @Test
     public void testPredicateOr() throws Throwable {
         assertTrue(FailablePredicate.TRUE.or(FailablePredicate.TRUE).test(null));
@@ -1363,14 +1480,6 @@ public class FailableFunctionsTest extends AbstractLangTest {
         assertThrows(NullPointerException.class, () -> assertTrue(FailablePredicate.TRUE.or(null).test(null)));
     }
 
-    @Test
-    public void testPredicateNegate() throws Throwable {
-        assertFalse(FailablePredicate.TRUE.negate().test(null));
-        assertFalse(FailablePredicate.truePredicate().negate().test(null));
-        assertTrue(FailablePredicate.FALSE.negate().test(null));
-        assertTrue(FailablePredicate.falsePredicate().negate().test(null));
-    }
-
     @Test
     public void testRunnable() {
         FailureOnOddInvocations.invocations = 0;
@@ -2564,108 +2673,8 @@ public class FailableFunctionsTest extends AbstractLangTest {
         assertTrue(closeable.isClosed());
     }
 
-    @Test
-    public void testFailableDoubleToIntFunctionNop() throws Throwable {
-        assertEquals(0, FailableDoubleToIntFunction.nop().applyAsInt(Double.MAX_VALUE), "Expect NOP to return 0");
-    }
-
-    @Test
-    public void testFailableDoubleToLongFunctionNop() throws Throwable {
-        assertEquals(0, FailableDoubleToLongFunction.nop().applyAsLong(Double.MAX_VALUE), "Expect NOP to return 0");
-    }
-
-    @Test
-    public void testFailableIntToDoubleFunctionNop() throws Throwable {
-        assertEquals(0, FailableIntToDoubleFunction.nop().applyAsDouble(Integer.MAX_VALUE), "Expect NOP to return 0");
-    }
-
-    @Test
-    public void testFailableIntToLongFunctionNop() throws Throwable {
-        assertEquals(0, FailableIntToLongFunction.nop().applyAsLong(Integer.MAX_VALUE), "Expect NOP to return 0");
-    }
-
-    @Test
-    public void testFailableLongToDoubleFunctionNop() throws Throwable {
-        assertEquals(0, FailableLongToDoubleFunction.nop().applyAsDouble(Long.MAX_VALUE), "Expect NOP to return 0");
-    }
-
-    @Test
-    public void testFailableLongToIntFunctionNop() throws Throwable {
-        assertEquals(0, FailableLongToIntFunction.nop().applyAsInt(Long.MAX_VALUE), "Expect NOP to return 0");
-    }
-
-    @Test
-    public void testFailableToIntFunctionNop() throws Throwable {
-        assertEquals(0, FailableToIntFunction.nop().applyAsInt("Foo"), "Expect NOP to return 0");
-    }
-
-    @Test
-    public void testFailableToIntBiFunctionNop() throws Throwable {
-        assertEquals(0, FailableToIntBiFunction.nop().applyAsInt("Foo", "Bar"), "Expect NOP to return 0");
-    }
-
-    @Test
-    public void testFailableToLongFunctionNop() throws Throwable {
-        assertEquals(0, FailableToLongFunction.nop().applyAsLong("Foo"), "Expect NOP to return 0");
-    }
-
-    @Test
-    public void testFailableToLongBiFunctionNop() throws Throwable {
-        assertEquals(0, FailableToLongBiFunction.nop().applyAsLong("Foo", "Bar"), "Expect NOP to return 0");
-    }
-
-    @Test
-    public void testFailableToDoubleFunctionNop() throws Throwable {
-        assertEquals(0, FailableToDoubleFunction.nop().applyAsDouble("Foo"), "Expect NOP to return 0");
-    }
-
-    @Test
-    public void testFailableToDoubleBiFunctionNop() throws Throwable {
-        assertEquals(0, FailableToDoubleBiFunction.nop().applyAsDouble("Foo", "Bar"), "Expect NOP to return 0");
-    }
-
-    @Test
-    public void testFailableBiFunctionNop() throws Throwable {
-        assertNull(FailableBiFunction.nop().apply("Foo", "Bar"), "Expect NOP to return null");
-    }
-
-    @Test
-    public void testFailableDoubleFunctionNop() throws Throwable {
-        assertNull(FailableDoubleFunction.nop().apply(Double.MAX_VALUE), "Expect NOP to return null");
-    }
-
-    @Test
-    public void testFailableIntFunctionNop() throws Throwable {
-        assertNull(FailableIntFunction.nop().apply(Integer.MAX_VALUE), "Expect NOP to return null");
-    }
-
-    @Test
-    public void testFailableLongFunctionNop() throws Throwable {
-        assertNull(FailableLongFunction.nop().apply(Long.MAX_VALUE), "Expect NOP to return null");
-    }
-
-    @Test
-    public void testFailableConsumerNop() throws Throwable {
-        // Expect nothing thrown
-        FailableConsumer.nop().accept("Foo");
-    }
-
-    @Test
-    public void testFailableObjDoubleConsumerNop() throws Throwable {
-        // Expect nothing thrown
-        FailableObjDoubleConsumer.nop().accept("Foo", Double.MAX_VALUE);
-    }
-
-    @Test
-    public void testFailableObjIntConsumerNop() throws Throwable {
-        // Expect nothing thrown
-        FailableObjIntConsumer.nop().accept("Foo", Integer.MAX_VALUE);
-    }
-
-    @Test
-    public void testFailableObjLongConsumerNop() throws Throwable {
-        // Expect nothing thrown
-        FailableObjLongConsumer.nop().accept("Foo", Long.MAX_VALUE);
+    private String throwingFunction(final String input) throws Exception {
+        return input;
     }
 
 }
diff --git a/src/test/java/org/apache/commons/lang3/function/FunctionsTest.java b/src/test/java/org/apache/commons/lang3/function/FunctionsTest.java
new file mode 100644
index 000000000..f97cb0c45
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/function/FunctionsTest.java
@@ -0,0 +1,30 @@
+/*
+ * 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.function;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+public class FunctionsTest {
+
+    @Test
+    public void testFunction() {
+        assertEquals("foo", Functions.function(String::valueOf).andThen(String::toString).apply("foo"));
+    }
+}