Skip to content

Commit 7014cb6

Browse files
wynnteoWynn Teo
andauthored
BAEL-9399 (#18740)
Co-authored-by: Wynn Teo <[email protected]>
1 parent 084199a commit 7014cb6

File tree

9 files changed

+167
-0
lines changed

9 files changed

+167
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.functionpointer;
2+
3+
public class AddCommand implements MathOperation {
4+
5+
@Override
6+
public int operate(int a, int b) {
7+
return a + b;
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.functionpointer;
2+
3+
import java.util.function.BiFunction;
4+
5+
public class AdvancedCalculator {
6+
7+
public int compute(int a, int b, BiFunction<Integer, Integer, Integer> operation) {
8+
return operation.apply(a, b);
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.functionpointer;
2+
3+
public class Calculator {
4+
public int calculate(int a, int b, MathOperation operation) {
5+
return operation.operate(a, b);
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.functionpointer;
2+
3+
public class DynamicOps {
4+
5+
public int power(int a, int b) {
6+
return (int) Math.pow(a, b);
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.functionpointer;
2+
3+
public class EnumCalculator {
4+
5+
public int calculate(int a, int b, MathOperationEnum operation) {
6+
return operation.apply(a, b);
7+
}
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.functionpointer;
2+
3+
public interface MathOperation {
4+
5+
int operate(int a, int b);
6+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.functionpointer;
2+
3+
public enum MathOperationEnum {
4+
ADD {
5+
@Override
6+
public int apply(int a, int b) {
7+
return a + b;
8+
}
9+
},
10+
SUBTRACT {
11+
@Override
12+
public int apply(int a, int b) {
13+
return a - b;
14+
}
15+
},
16+
MULTIPLY {
17+
@Override
18+
public int apply(int a, int b) {
19+
return a * b;
20+
}
21+
},
22+
DIVIDE {
23+
@Override
24+
public int apply(int a, int b) {
25+
if (b == 0) {
26+
throw new ArithmeticException("Division by zero");
27+
}
28+
return a / b;
29+
}
30+
};
31+
32+
public abstract int apply(int a, int b);
33+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.functionpointer;
2+
3+
public class MathUtils {
4+
5+
public static int add(int a, int b) {
6+
return a + b;
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.functionpointer;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.lang.reflect.Method;
6+
import java.util.function.BiFunction;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
public class FunctionPointerUnitTest {
11+
12+
@Test
13+
void givenAnonymousAddition_whenCalculate_thenReturnSum() {
14+
Calculator calculator = new Calculator();
15+
MathOperation addition = new MathOperation() {
16+
@Override
17+
public int operate(int a, int b) {
18+
return a + b;
19+
}
20+
};
21+
int result = calculator.calculate(2, 3, addition);
22+
assertEquals(5, result);
23+
}
24+
25+
@Test
26+
void givenLambdaSubtraction_whenCalculate_thenReturnDifference() {
27+
Calculator calculator = new Calculator();
28+
MathOperation subtract = (a, b) -> a - b;
29+
int result = calculator.calculate(10, 4, subtract);
30+
assertEquals(6, result);
31+
}
32+
33+
@Test
34+
void givenBiFunctionMultiply_whenApply_thenReturnProduct() {
35+
BiFunction<Integer, Integer, Integer> multiply = (a, b) -> a * b;
36+
int result = multiply.apply(6, 7);
37+
assertEquals(42, result);
38+
}
39+
40+
@Test
41+
void givenBiFunctionDivide_whenCompute_thenReturnQuotient() {
42+
AdvancedCalculator calculator = new AdvancedCalculator();
43+
BiFunction<Integer, Integer, Integer> divide = (a, b) -> a / b;
44+
int result = calculator.compute(20, 4, divide);
45+
assertEquals(5, result);
46+
}
47+
48+
@Test
49+
void givenMethodReference_whenCalculate_thenReturnSum() {
50+
Calculator calculator = new Calculator();
51+
MathOperation operation = MathUtils::add;
52+
int result = calculator.calculate(5, 10, operation);
53+
assertEquals(15, result);
54+
}
55+
56+
@Test
57+
void givenReflection_whenInvokePower_thenReturnResult() throws Exception {
58+
DynamicOps ops = new DynamicOps();
59+
Method method = DynamicOps.class.getMethod("power", int.class, int.class);
60+
int result = (int) method.invoke(ops, 2, 3);
61+
assertEquals(8, result);
62+
}
63+
64+
@Test
65+
void givenAddCommand_whenCalculate_thenReturnSum() {
66+
Calculator calculator = new Calculator();
67+
MathOperation add = new AddCommand();
68+
int result = calculator.calculate(3, 7, add);
69+
assertEquals(10, result);
70+
}
71+
72+
@Test
73+
void givenEnumSubtract_whenCalculate_thenReturnResult() {
74+
EnumCalculator calculator = new EnumCalculator();
75+
int result = calculator.calculate(9, 4, MathOperationEnum.SUBTRACT);
76+
assertEquals(5, result);
77+
}
78+
}

0 commit comments

Comments
 (0)