Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.

Commit 45085c0

Browse files
author
Taras
committed
Restructured the exercise
- added Functions.java - refactored FunctionMap.java - implemented FunctionsTest.java
1 parent fe0433f commit 45085c0

File tree

8 files changed

+145
-69
lines changed

8 files changed

+145
-69
lines changed

math-functions-factory/src/main/java/com.bobocode/FunctionFactory.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

math-functions-factory/src/test/java/com/bobocode/FunctionFactoryTest.java

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

12-
<artifactId>math-functions-factory</artifactId>
13-
14-
<dependencies>
15-
<dependency>
16-
<groupId>junit</groupId>
17-
<artifactId>junit</artifactId>
18-
<version>4.12</version>
19-
</dependency>
20-
</dependencies>
12+
<artifactId>math-functions</artifactId>
2113

2214
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.bobocode;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.function.Function;
6+
7+
/**
8+
* FunctionMap is an API that allows you to store and retrieve functions by string name. FunctionMap are stored in a
9+
* HashMap, where the key is a function name, and the value is a Function<T,R> instance
10+
*/
11+
public class FunctionMap<T, R> {
12+
private Map<String, Function<T, R>> functionMap;
13+
14+
FunctionMap() {
15+
functionMap = new HashMap<>();
16+
}
17+
18+
public void addFunction(String name, Function<T, R> function) {
19+
functionMap.put(name, function);
20+
}
21+
22+
public Function<T, R> getFunction(String name) {
23+
if (functionMap.containsKey(name)) {
24+
return functionMap.get(name);
25+
} else {
26+
throw new InvalidFunctionNameException(name);
27+
}
28+
}
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.bobocode;
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, sng, increment, decrement, square)
18+
19+
return intFunctionMap;
20+
}
21+
}

math-functions-factory/src/main/java/com.bobocode/InvalidFunctionNameException.java renamed to math-functions/src/main/java/com.bobocode/InvalidFunctionNameException.java

File renamed without changes.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.bobocode;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.function.Function;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class FunctionsTest {
11+
private FunctionMap<Integer, Integer> integerFunctionMap;
12+
13+
@Before
14+
public void init(){
15+
integerFunctionMap = Functions.intFunctionMap();
16+
}
17+
18+
@Test
19+
public void testSquareFunction() {
20+
Function<Integer, Integer> squareFunction = integerFunctionMap.getFunction("square");
21+
22+
int actualResult = squareFunction.apply(5);
23+
24+
assertEquals(25, actualResult);
25+
}
26+
27+
@Test
28+
public void testAbsFunction(){
29+
Function<Integer, Integer> absFunction = integerFunctionMap.getFunction("abs");
30+
31+
int actualResult = absFunction.apply(-192);
32+
33+
assertEquals(192, actualResult);
34+
}
35+
36+
@Test
37+
public void testIncrementFunction(){
38+
Function<Integer, Integer> incrementFunction = integerFunctionMap.getFunction("increment");
39+
40+
int actualResult = incrementFunction.apply(399);
41+
42+
assertEquals(400, actualResult);
43+
}
44+
45+
@Test
46+
public void testDecrementFunction(){
47+
Function<Integer, Integer> decrementFunction = integerFunctionMap.getFunction("decrement");
48+
49+
int actualResult = decrementFunction.apply(800);
50+
51+
assertEquals(799, actualResult);
52+
}
53+
54+
@Test
55+
public void testSignFunctionOnNegativeValue(){
56+
Function<Integer, Integer> signFunction = integerFunctionMap.getFunction("sgn");
57+
58+
int actualResult = signFunction.apply(-123);
59+
60+
assertEquals(-1, actualResult);
61+
}
62+
63+
@Test
64+
public void testSignFunctionOnPositiveValue(){
65+
Function<Integer, Integer> signFunction = integerFunctionMap.getFunction("sgn");
66+
67+
int actualResult = signFunction.apply(23);
68+
69+
assertEquals(1, actualResult);
70+
}
71+
72+
@Test
73+
public void testSignFunctionOnZero(){
74+
Function<Integer, Integer> signFunction = integerFunctionMap.getFunction("sgn");
75+
76+
int actualResult = signFunction.apply(0);
77+
78+
assertEquals(0, actualResult);
79+
}
80+
81+
@Test(expected = InvalidFunctionNameException.class)
82+
public void testSqrtFunction(){
83+
integerFunctionMap.getFunction("sqrt");
84+
}
85+
}

pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,20 @@
99
<packaging>pom</packaging>
1010
<version>1.0-SNAPSHOT</version>
1111
<modules>
12-
<module>math-functions-factory</module>
12+
<module>math-functions</module>
1313
</modules>
1414

1515
<properties>
1616
<maven.compiler.source>1.8</maven.compiler.source>
1717
<maven.compiler.target>1.8</maven.compiler.target>
1818
</properties>
1919

20+
<dependencies>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>4.12</version>
25+
</dependency>
26+
</dependencies>
27+
2028
</project>

0 commit comments

Comments
 (0)