-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChapter2Lambdas.java
72 lines (59 loc) · 1.8 KB
/
Chapter2Lambdas.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
package dev.boiarshinov.book;
import org.testng.annotations.Test;
import javax.swing.*;
import javax.swing.text.DateFormatter;
import java.util.Random;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* Chapter 2 "Lambda-expressions".
*/
public class Chapter2Lambdas {
/**
* Exercise 1.
*/
@Test
public void functions() {
//Task A.
// T -> Function -> R
//Task B.
//For trigonometrical, convert functions etc.:
Function<Double, Double> sin = Math::sin;
Function<Double, Double> radToDegree = x -> x * 180 / Math.PI;
//Task C.
//First example can be a Long to Long Function, two others not.
final Function<Long, Long> function = x -> x + 1;
// final Function<Long, Long> function = (x, y) -> x + 1;
// final Function<Long, Long> function = x -> x == 1;
}
/**
* Exercise 2.
*/
@Test
public void threadLocal() {
final ThreadLocal<DateFormatter> localDateFormatter = ThreadLocal.withInitial(DateFormatter::new);
}
/**
* Exercise 3.
*/
@Test
public void lambdaTypeDetermination() {
//variant a - true
final Runnable helloWorld = () -> System.out.println("HelloWorld");
//variant b - true
JButton button = new JButton();
button.addActionListener(event -> System.out.println(event.getActionCommand()));
//variant c - false
//final boolean isOk = check(x -> x > 5);
}
private static boolean check(Predicate<Integer> predicate) {
return new Random().nextBoolean();
}
private static boolean check(IntPred predicate) {
return new Random().nextBoolean();
}
@FunctionalInterface
interface IntPred {
boolean test(Integer value);
}
}