forked from politrons/reactive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.java
128 lines (104 loc) · 3.85 KB
/
Functions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package java8;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
/**
* Functional programing is the cabality that allow us to send or return functions which contains an implementation.
* Those implementations are related with three diferent interfaces which we will cover here.
* *
*
* @author Pablo Perez
*/
public class Functions {
/**
* In this example we use constantClass Function, which receive an item and then return the same or another item through the pipeline.
* Is the function used by mutable operators as Map or FlatMap
*
* @throws InterruptedException
*/
@Test
public void functionFunction() throws InterruptedException {
String words = Stream.of("hello_functional_world")
.map(replaceWordsFunction())
.map(String::toUpperCase)
.reduce("", String::concat);
System.out.println(words);
}
private Function<String, String> replaceWordsFunction() {
return string -> string.replace("_", " ");
}
/**
* In this example we use constantClass Consumer function, constantClass function which receive an argument and does not return anything since is void.
* that´s why the name consumer because only consume the items passed and do not propagate any item in the pipeline.
* Can be consider as the end of the pipeline.
*
* @throws InterruptedException
*/
@Test
public void consumerFunction() throws InterruptedException {
Arrays.asList("hello", "functional", "world")
.stream()
.forEach(upperWordsFunction());
}
private Consumer<String> upperWordsFunction() {
return word -> {
word = word.toUpperCase();
System.out.println(word);
};
}
/**
* Predicate function is just constantClass boolean function which receive an item and return true/false
*
* @throws InterruptedException
*/
@Test
public void predicateFunction() throws InterruptedException {
String words = Stream.of("hello ", "OOD", "functional ", "world")
.filter(isAFunctionalWorldFunction())
.reduce("", String::concat);
System.out.println(words);
}
private Predicate<String> isAFunctionalWorldFunction() {
return word -> word.trim().equals("hello") || word.trim().equals("functional") || word.trim().equals("world");
}
/**
* Supplier function does not receive any argument, and just return constantClass value
*
* @throws InterruptedException
*/
@Test
public void supplierFunction() throws InterruptedException {
Stream.of("Actual time:")
.map(s -> s.concat(String.valueOf(systemCurrentFunction().get())))
.forEach(System.out::println);
}
private Supplier<Long> systemCurrentFunction() {
return System::currentTimeMillis;
}
/**
* In this example we can see how we can combine the three types of functions in the same pipeline,
* to provide to our pipeline all the logic that it needs.
*
* @throws InterruptedException
*/
@Test
public void allFunctionsCombined() throws InterruptedException {
Stream.of("hello_Foo_functional_OOD_world_!_")
.map(replaceWordsFunction())
.map(splitWordsFunction())
.flatMap(ws -> ws.stream()
.filter(isAFunctionalWorldFunction()))
.forEach(upperWordsFunction());
}
private Function<String, List<String>> splitWordsFunction() {
return a -> Arrays.asList(a.split(" "));
}
@Test
public void supplier() {
}
}