Add Functions#from(Function)

Add FailableFunction#function(FailableFunction)
This commit is contained in:
Gary Gregory 2023-08-04 19:57:50 -04:00
parent 0995b83bed
commit 0ede42bbdc
6 changed files with 208 additions and 114 deletions

View File

@ -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>

View File

@ -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>

View File

@ -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.

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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"));
}
}