-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathFunctions.java
164 lines (137 loc) · 4.87 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package java8;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
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() {
System.out.println(splitWordsFunction().apply("hello world"));
}
@Test
public void ifReturnFunction() {
String result = ifFunction.apply(true);
System.out.println(result);
String result1 = ifMethod(false);
System.out.println(result1);
}
@Test
public void optionalFunction() throws InterruptedException {
Optional<Object> o = Optional.ofNullable(null);
System.out.println(o.isPresent());
}
/**
* In Java since if statement is not a function we have to create one, in order to being able
* to return a value from an if and assign into a variable.
* Of course the classic way is to create a simple method
*/
Function<Boolean, String> ifFunction = a -> {
if (a) {
return "Value 1";
} else {
return "Value 2";
}
};
public String ifMethod(Boolean a) {
if (a) {
return "Value 1";
} else {
return "Value 2";
}
}
}