BAEL-5725 code for the TriFunction interface article
This commit is contained in:
parent
171e4bd7c8
commit
ed608d1ede
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-function</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
|
@ -32,6 +32,11 @@
|
|||
<version>${mockito-inline.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vavr</groupId>
|
||||
<artifactId>vavr</artifactId>
|
||||
<version>${vavr.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -48,6 +53,7 @@
|
|||
<mockito-inline.version>3.8.0</mockito-inline.version>
|
||||
<assertj.version>3.22.0</assertj.version>
|
||||
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||
<vavr.version>0.10.4</vavr.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.trifunction;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Represents a function that accepts three arguments and produces a result.
|
||||
* This is the three-arity specialization of {@link Function}.
|
||||
*
|
||||
* <p>This is a functional interface
|
||||
* whose functional method is {@link #apply(Object, Object, Object)}.
|
||||
*
|
||||
* @param <T> the type of the first argument to the function
|
||||
* @param <U> the type of the second argument to the function
|
||||
* @param <V> the type of the third argument to the function
|
||||
* @param <R> the type of the result of the function
|
||||
*
|
||||
* @see Function
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface TriFunction<T, U, V, R> {
|
||||
|
||||
/**
|
||||
* Applies this function to the given arguments.
|
||||
*
|
||||
* @param t the first function argument
|
||||
* @param u the second function argument
|
||||
* @param v the third function argument
|
||||
* @return the function result
|
||||
*/
|
||||
R apply(T t, U u, V v);
|
||||
|
||||
/**
|
||||
* Returns a composed function that first applies this function to
|
||||
* its input, and then applies the {@code after} function to the result.
|
||||
* If evaluation of either function throws an exception, it is relayed to
|
||||
* the caller of the composed function.
|
||||
*
|
||||
* @param <K> the type of output of the {@code after} function, and of the
|
||||
* composed function
|
||||
* @param after the function to apply after this function is applied
|
||||
* @return a composed function that first applies this function and then
|
||||
* applies the {@code after} function
|
||||
* @throws NullPointerException if after is null
|
||||
*/
|
||||
default <K> TriFunction<T, U, V, K> andThen(Function<? super R, ? extends K> after) {
|
||||
Objects.requireNonNull(after);
|
||||
return (T t, U u, V v) -> after.apply(apply(t, u, v));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.trifunction;
|
||||
|
||||
public class TriFunctionExample {
|
||||
|
||||
public static TriFunction<Integer, Integer, Integer, Integer> multiplyThenAdd = (x, y, z) -> x * y + z;
|
||||
|
||||
public static TriFunction<Integer, Integer, Integer, Integer> multiplyThenAddThenDivideByTen = multiplyThenAdd.andThen(x -> x / 10);
|
||||
|
||||
public static TriFunction<Integer, String, Boolean, String> convertIntegerOrReturnStringDependingOnCondition = (myInt, myStr, myBool) -> {
|
||||
if (Boolean.TRUE.equals(myBool)) {
|
||||
return myInt != null ? myInt.toString() : "";
|
||||
} else {
|
||||
return myStr;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.trifunction;
|
||||
|
||||
import io.vavr.Function3;
|
||||
|
||||
public class VavrFunction3Example {
|
||||
|
||||
public static Function3<Integer, Integer, Integer, Integer> multiplyThenAdd = (x, y, z) -> x * y + z;
|
||||
|
||||
public static Function3<Integer, Integer, Integer, Integer> multiplyThenAddThenDivideByTen = multiplyThenAdd.andThen(x -> x / 10);
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.trifunction;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class TriFunctionExampleUnitTest {
|
||||
|
||||
private static final String BAELDUNG = "baeldung";
|
||||
|
||||
@Test
|
||||
void whenMultiplyThenAdd_ThenReturnsCorrectResult() {
|
||||
assertEquals(25, TriFunctionExample.multiplyThenAdd.apply(2, 10, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenMultiplyThenAddThenDivideByTen_ThenReturnsCorrectResult() {
|
||||
assertEquals(2, TriFunctionExample.multiplyThenAddThenDivideByTen.apply(2, 10, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTrueBooleanAndNullInteger_WhenConvertIntegerOrReturnStringDependingOnCondition_ThenEmptyString() {
|
||||
assertEquals("", TriFunctionExample.convertIntegerOrReturnStringDependingOnCondition.apply(null, BAELDUNG, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTrueBooleanAndNonNullInteger_WhenConvertIntegerOrReturnStringDependingOnCondition_ThenConvertIntegerToString() {
|
||||
assertEquals("88", TriFunctionExample.convertIntegerOrReturnStringDependingOnCondition.apply(88, BAELDUNG, true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenFalseBoolean_WhenConvertIntegerOrReturnStringDependingOnCondition_ThenReturnTheString() {
|
||||
assertEquals(BAELDUNG, TriFunctionExample.convertIntegerOrReturnStringDependingOnCondition.apply(null, BAELDUNG, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNullBoolean_WhenConvertIntegerOrReturnStringDependingOnCondition_ThenReturnTheString() {
|
||||
assertEquals(BAELDUNG, TriFunctionExample.convertIntegerOrReturnStringDependingOnCondition.apply(null, BAELDUNG, null));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.trifunction;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class VavrFunction3ExampleUnitTest {
|
||||
|
||||
@Test
|
||||
void whenMultiplyThenAdd_ThenReturnsCorrectResult() {
|
||||
assertEquals(25, VavrFunction3Example.multiplyThenAdd.apply(2, 10, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenMultiplyThenAddThenDivideByTen_ThenReturnsCorrectResult() {
|
||||
assertEquals(2, VavrFunction3Example.multiplyThenAddThenDivideByTen.apply(2, 10, 5));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue