Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fe0433f

Browse files
author
Taras
committedApr 16, 2018
Implemented FunctionFactory exercise
0 parents  commit fe0433f

File tree

7 files changed

+112
-0
lines changed

7 files changed

+112
-0
lines changed
 

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
**/*.iml
3+
**/target

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Java 8 exercises

‎math-functions-factory/pom.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
5+
<parent>
6+
<artifactId>java-8-exercises</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
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>
21+
22+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.bobocode;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.function.Function;
6+
7+
/**
8+
* FunctionFactory is an API that allows you to store and retrieve functions by name. Functions are stored in a HashMap,
9+
* where the key is a function name, and the value is a Function<T,R> instance. Methods addFunction(), and getFunction()
10+
* are already implemented.
11+
* <p>
12+
* The task is to create different types of functions and manage them using FunctionFactory. The instruction is placed
13+
* to the main method.
14+
*/
15+
public class FunctionFactory<T, R> {
16+
private Map<String, Function<T, R>> functionMap;
17+
18+
private FunctionFactory() {
19+
functionMap = new HashMap<>();
20+
}
21+
22+
public static FunctionFactory<Integer, Integer> integerFunctionFactory(){
23+
return new FunctionFactory<>();
24+
}
25+
26+
public void addFunction(String name, Function<T, R> function) {
27+
functionMap.put(name, function);
28+
}
29+
30+
public Function<T, R> getFunction(String name) {
31+
if (functionMap.containsKey(name)) {
32+
return functionMap.get(name);
33+
} else {
34+
throw new InvalidFunctionNameException(name);
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.bobocode;
2+
3+
public class InvalidFunctionNameException extends RuntimeException {
4+
public InvalidFunctionNameException(String functionName) {
5+
super("Function " + functionName + " doesn't exist.");
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.bobocode;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.JUnit4;
6+
7+
import java.util.function.Function;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
@RunWith(JUnit4.class)
12+
public class FunctionFactoryTest {
13+
private FunctionFactory<Integer, Integer> integerFunctionFactory = FunctionFactory.integerFunctionFactory();
14+
15+
@Test
16+
public void testAddSquareFunction(){
17+
integerFunctionFactory.addFunction("square", n -> n * n);
18+
19+
Function<Integer, Integer> square = integerFunctionFactory.getFunction("square");
20+
assertEquals(25, square.apply(5).intValue());
21+
}
22+
}

‎pom.xml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.bobocode</groupId>
8+
<artifactId>java-8-exercises</artifactId>
9+
<packaging>pom</packaging>
10+
<version>1.0-SNAPSHOT</version>
11+
<modules>
12+
<module>math-functions-factory</module>
13+
</modules>
14+
15+
<properties>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<maven.compiler.target>1.8</maven.compiler.target>
18+
</properties>
19+
20+
</project>

0 commit comments

Comments
 (0)
This repository has been archived.