Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0e5f3d6

Browse files
authored
Merge pull request #45 from bobocode-projects/GP-47-Migrate_lambda-math-functions
GP-47 Migrate lambda-math-functions
2 parents 5b41a24 + ccf7b46 commit 0e5f3d6

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.bobocode.lambda_math_functions;
2+
3+
import com.bobocode.util.ExerciseNotCompletedException;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.function.Function;
8+
9+
/**
10+
* FunctionMap is an API that allows you to store and retrieve functions by string name. FunctionMap are stored in a
11+
* HashMap, where the key is a function name, and the value is a Function<T,R> instance
12+
*/
13+
public class FunctionMap<T, R> {
14+
private Map<String, Function<T, R>> functionMap;
15+
16+
FunctionMap() {
17+
functionMap = new HashMap<>();
18+
}
19+
20+
public void addFunction(String name, Function<T, R> function) {
21+
functionMap.put(name, function);
22+
}
23+
24+
public Function<T, R> getFunction(String name) {
25+
if (functionMap.containsKey(name)) {
26+
return functionMap.get(name);
27+
} else {
28+
throw new ExerciseNotCompletedException();
29+
}
30+
}
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.bobocode.lambda_math_functions;
2+
3+
public class Functions {
4+
/**
5+
* A static factory method that creates an integer function map with basic functions:
6+
* - abs (absolute value)
7+
* - sgn (signum function)
8+
* - increment
9+
* - decrement
10+
* - square
11+
*
12+
* @return an instance of {@link FunctionMap} that contains all listed functions
13+
*/
14+
public static FunctionMap<Integer, Integer> intFunctionMap() {
15+
FunctionMap<Integer, Integer> intFunctionMap = new FunctionMap<>();
16+
17+
// todo: add simple functions to the function map (abs, sgn, increment, decrement, square)
18+
19+
return intFunctionMap;
20+
}
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.bobocode.lambda_math_functions;
2+
3+
public class InvalidFunctionNameException extends RuntimeException {
4+
public InvalidFunctionNameException(String functionName) {
5+
super("Function " + functionName + " doesn't exist.");
6+
}
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Math functions exercise 💪
2+
Improve your lambda and method reference skills
3+
### Task
4+
**FunctionMap** is an API that allows you to store and retrieve math functions by string name. Your job is to implement the *todo* section of `Functions.java` class using **Lambda expressions and method reference**, so all tests in `FunctionsTest.java` should pass
5+
6+
### Pre-conditions ❗
7+
You're supposed to be familiar with Java 8
8+
9+
### How to start ❓
10+
* Just clone the repository and start implementing the **todo** section, verify your changes by running tests
11+
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source)
12+
* Don't worry if you got stuck, checkout the [exercise/completed](to do) branch and see the final implementation
13+
14+
### Related materials i️
15+
* [Lambda tutorial](to do) <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=20/>
16+
* [State of lambda (JSR 335)](http://htmlpreview.github.io/?https://github.com/bobocode-projects/resources/blob/master/java8/lambda/sotl.html)
17+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.bobocode.lambda_math_functions;
2+
3+
import org.junit.jupiter.api.*;
4+
5+
import java.util.function.Function;
6+
7+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
8+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
9+
10+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
11+
public class FunctionsTest {
12+
private FunctionMap<Integer, Integer> integerFunctionMap;
13+
14+
@BeforeEach
15+
public void init() {
16+
integerFunctionMap = Functions.intFunctionMap();
17+
}
18+
19+
@Test
20+
@Order(7)
21+
void squareFunction() {
22+
Function<Integer, Integer> squareFunction = integerFunctionMap.getFunction("square");
23+
24+
int result = squareFunction.apply(5);
25+
26+
assertThat(result).isEqualTo(25);
27+
}
28+
29+
@Test
30+
@Order(1)
31+
void absFunction() {
32+
Function<Integer, Integer> absFunction = integerFunctionMap.getFunction("abs");
33+
34+
int result = absFunction.apply(-192);
35+
36+
assertThat(result).isEqualTo(192);
37+
}
38+
39+
@Test
40+
@Order(5)
41+
void incrementFunction() {
42+
Function<Integer, Integer> incrementFunction = integerFunctionMap.getFunction("increment");
43+
44+
int result = incrementFunction.apply(399);
45+
46+
assertThat(result).isEqualTo(400);
47+
}
48+
49+
@Test
50+
@Order(6)
51+
void destDecrementFunction() {
52+
Function<Integer, Integer> decrementFunction = integerFunctionMap.getFunction("decrement");
53+
54+
int result = decrementFunction.apply(800);
55+
56+
assertThat(result).isEqualTo(799);
57+
}
58+
59+
@Test
60+
@Order(2)
61+
void signFunctionOnNegativeValue() {
62+
Function<Integer, Integer> signFunction = integerFunctionMap.getFunction("sgn");
63+
64+
int result = signFunction.apply(-123);
65+
66+
assertThat(result).isEqualTo(-1);
67+
}
68+
69+
@Test
70+
@Order(3)
71+
void signFunctionOnPositiveValue() {
72+
Function<Integer, Integer> signFunction = integerFunctionMap.getFunction("sgn");
73+
74+
int result = signFunction.apply(23);
75+
76+
assertThat(result).isEqualTo(1);
77+
}
78+
79+
@Test
80+
@Order(4)
81+
void signFunctionOnZero() {
82+
Function<Integer, Integer> signFunction = integerFunctionMap.getFunction("sgn");
83+
84+
int result = signFunction.apply(0);
85+
86+
assertThat(result).isEqualTo(0);
87+
}
88+
89+
@Test
90+
@Order(8)
91+
void getUnknownFunction() {
92+
assertThatExceptionOfType(InvalidFunctionNameException.class).isThrownBy(()
93+
-> integerFunctionMap.getFunction("sqrt"));
94+
}
95+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /