Skip to content

Commit ed608d1

Browse files
author
thibault.faure
committed
BAEL-5725 code for the TriFunction interface article
1 parent 171e4bd commit ed608d1

File tree

6 files changed

+146
-2
lines changed

6 files changed

+146
-2
lines changed

core-java-modules/core-java-function/pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>core-java-function</artifactId>
77
<version>0.1.0-SNAPSHOT</version>
@@ -32,6 +32,11 @@
3232
<version>${mockito-inline.version}</version>
3333
<scope>test</scope>
3434
</dependency>
35+
<dependency>
36+
<groupId>io.vavr</groupId>
37+
<artifactId>vavr</artifactId>
38+
<version>${vavr.version}</version>
39+
</dependency>
3540
</dependencies>
3641

3742
<build>
@@ -48,6 +53,7 @@
4853
<mockito-inline.version>3.8.0</mockito-inline.version>
4954
<assertj.version>3.22.0</assertj.version>
5055
<commons-lang3.version>3.12.0</commons-lang3.version>
56+
<vavr.version>0.10.4</vavr.version>
5157
</properties>
5258

5359
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.trifunction;
2+
3+
import java.util.Objects;
4+
import java.util.function.Function;
5+
6+
/**
7+
* Represents a function that accepts three arguments and produces a result.
8+
* This is the three-arity specialization of {@link Function}.
9+
*
10+
* <p>This is a functional interface
11+
* whose functional method is {@link #apply(Object, Object, Object)}.
12+
*
13+
* @param <T> the type of the first argument to the function
14+
* @param <U> the type of the second argument to the function
15+
* @param <V> the type of the third argument to the function
16+
* @param <R> the type of the result of the function
17+
*
18+
* @see Function
19+
*/
20+
@FunctionalInterface
21+
public interface TriFunction<T, U, V, R> {
22+
23+
/**
24+
* Applies this function to the given arguments.
25+
*
26+
* @param t the first function argument
27+
* @param u the second function argument
28+
* @param v the third function argument
29+
* @return the function result
30+
*/
31+
R apply(T t, U u, V v);
32+
33+
/**
34+
* Returns a composed function that first applies this function to
35+
* its input, and then applies the {@code after} function to the result.
36+
* If evaluation of either function throws an exception, it is relayed to
37+
* the caller of the composed function.
38+
*
39+
* @param <K> the type of output of the {@code after} function, and of the
40+
* composed function
41+
* @param after the function to apply after this function is applied
42+
* @return a composed function that first applies this function and then
43+
* applies the {@code after} function
44+
* @throws NullPointerException if after is null
45+
*/
46+
default <K> TriFunction<T, U, V, K> andThen(Function<? super R, ? extends K> after) {
47+
Objects.requireNonNull(after);
48+
return (T t, U u, V v) -> after.apply(apply(t, u, v));
49+
}
50+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.trifunction;
2+
3+
public class TriFunctionExample {
4+
5+
public static TriFunction<Integer, Integer, Integer, Integer> multiplyThenAdd = (x, y, z) -> x * y + z;
6+
7+
public static TriFunction<Integer, Integer, Integer, Integer> multiplyThenAddThenDivideByTen = multiplyThenAdd.andThen(x -> x / 10);
8+
9+
public static TriFunction<Integer, String, Boolean, String> convertIntegerOrReturnStringDependingOnCondition = (myInt, myStr, myBool) -> {
10+
if (Boolean.TRUE.equals(myBool)) {
11+
return myInt != null ? myInt.toString() : "";
12+
} else {
13+
return myStr;
14+
}
15+
};
16+
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.trifunction;
2+
3+
import io.vavr.Function3;
4+
5+
public class VavrFunction3Example {
6+
7+
public static Function3<Integer, Integer, Integer, Integer> multiplyThenAdd = (x, y, z) -> x * y + z;
8+
9+
public static Function3<Integer, Integer, Integer, Integer> multiplyThenAddThenDivideByTen = multiplyThenAdd.andThen(x -> x / 10);
10+
11+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.trifunction;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class TriFunctionExampleUnitTest {
8+
9+
private static final String BAELDUNG = "baeldung";
10+
11+
@Test
12+
void whenMultiplyThenAdd_ThenReturnsCorrectResult() {
13+
assertEquals(25, TriFunctionExample.multiplyThenAdd.apply(2, 10, 5));
14+
}
15+
16+
@Test
17+
void whenMultiplyThenAddThenDivideByTen_ThenReturnsCorrectResult() {
18+
assertEquals(2, TriFunctionExample.multiplyThenAddThenDivideByTen.apply(2, 10, 5));
19+
}
20+
21+
@Test
22+
void givenTrueBooleanAndNullInteger_WhenConvertIntegerOrReturnStringDependingOnCondition_ThenEmptyString() {
23+
assertEquals("", TriFunctionExample.convertIntegerOrReturnStringDependingOnCondition.apply(null, BAELDUNG, true));
24+
}
25+
26+
@Test
27+
void givenTrueBooleanAndNonNullInteger_WhenConvertIntegerOrReturnStringDependingOnCondition_ThenConvertIntegerToString() {
28+
assertEquals("88", TriFunctionExample.convertIntegerOrReturnStringDependingOnCondition.apply(88, BAELDUNG, true));
29+
}
30+
31+
@Test
32+
void givenFalseBoolean_WhenConvertIntegerOrReturnStringDependingOnCondition_ThenReturnTheString() {
33+
assertEquals(BAELDUNG, TriFunctionExample.convertIntegerOrReturnStringDependingOnCondition.apply(null, BAELDUNG, false));
34+
}
35+
36+
@Test
37+
void givenNullBoolean_WhenConvertIntegerOrReturnStringDependingOnCondition_ThenReturnTheString() {
38+
assertEquals(BAELDUNG, TriFunctionExample.convertIntegerOrReturnStringDependingOnCondition.apply(null, BAELDUNG, null));
39+
}
40+
41+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.trifunction;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class VavrFunction3ExampleUnitTest {
8+
9+
@Test
10+
void whenMultiplyThenAdd_ThenReturnsCorrectResult() {
11+
assertEquals(25, VavrFunction3Example.multiplyThenAdd.apply(2, 10, 5));
12+
}
13+
14+
@Test
15+
void whenMultiplyThenAddThenDivideByTen_ThenReturnsCorrectResult() {
16+
assertEquals(2, VavrFunction3Example.multiplyThenAddThenDivideByTen.apply(2, 10, 5));
17+
}
18+
19+
}

0 commit comments

Comments
 (0)