-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimports.java
219 lines (176 loc) · 4.52 KB
/
imports.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import java.util.stream.*;
import static java.util.Map.*;
import static java.util.stream.Collectors.*;
import java.util.*;
import static java.util.stream.Collectors.groupingBy;
class Payment
{
private final int value;
public Payment(final int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
@Override
public String toString()
{
return "Payment{" +
"value=" + value +
'}';
}
}
class Setting
{
static Optional<Setting> lookupSettingByName(final String name)
{
return Optional.ofNullable(System.getProperty(name))
.map(Setting::new);
}
private final String value;
Setting(final String value)
{
this.value = value;
}
public String getValue()
{
return value;
}
public String toString()
{
return "[value="+value+"]";
}
}
class Booking
{
static Optional<Booking> lookupBooking(final String reference)
{
return reference.startsWith("LCY")
? Optional.of(new Booking(reference))
: Optional.empty();
}
private final String reference;
Booking(final String reference)
{
this.reference = reference;
}
public String getReference()
{
return reference;
}
public String toString()
{
return "Booking{"+reference+"}";
}
}
class Ui
{
static void displayCheckIn(final Booking booking)
{
System.out.println("Checking in ... " + booking);
}
static void displayMissingBookingPage()
{
System.out.println("Missing Booking");
}
}
class Client
{
private final String address;
Client(String address)
{
this.address = address;
}
public String toString()
{
return "Client{address="+address+"}";
}
}
public class Expense {
private final long amount;
private final int year;
private final List<Tag> tags;
public Expense(long amount, int year, List<Tag> tags) {
this.amount = amount;
this.year = year;
this.tags = tags;
}
public long getAmount() {
return amount;
}
public int getYear() {
return year;
}
public List<Tag> getTags() {
return tags;
}
@Override
public String toString() {
return "Expense{" +
"amount=" + amount +
", year=" + year +
", tags=" + tags +
'}';
}
}
public enum Tag {
FOOD, ENTERTAINMENT, TRAVEL, UTILITY
}
Optional<Client> findClient(final String clientId)
{
return "aClient".equals(clientId)
? Optional.of(new Client("London"))
: Optional.empty();
}
Optional<Client> lookupCompanyDetails(final String clientId)
{
return "anotherCompany".equals(clientId)
? Optional.of(new Client("Cambridge"))
: Optional.empty();
}
List<String> SETTING_NAMES = List.of("userHome", "user.home");
List<Payment> paymentsByValue =
Stream.of(900, 700, 500, 300, 100, 0).map(Payment::new).collect(toList());
String bookingReference = "LCY to ANR"
List<String> toc = new ArrayList<>(List.of(
" Introduction",
" Collection Factory Methods",
" motivation",
" of()",
" ofEntries()",
" Streams",
" ofNullable (properties)",
" takeWhile/dropWhile (paymentsByValue)",
" iterate (termination)",
" Collectors",
" filtering",
" flatMapping",
" Optional",
" stream (Setting)",
" ifPresentOrElse (Flight Bookings)",
" or (Client addresses)",
" Conclusions"));
int tocIndex = 0;
void printToc()
{
toc.forEach(System.out::println);
}
void next()
{
String current = toc.get(tocIndex);
current = "✓" + current.substring(1);
toc.set(tocIndex, current);
tocIndex++;
printToc();
}
String takeWhile = "paymentsByValue.stream().filter(transaction -> transaction.getValue() >= 500).collect(toList());";
String iterate = "Stream.iterate(1, n -> n+ 1).filter(n -> n < 5).forEach(System.out::println)";
String optionalStream = "Setting.lookupSettingByName";
String optionalPresentOrElse = "Booking.lookupBooking(), Ui.displayCheckIn(booking.get()), Ui.displayMissingBookingPage()"
String optionalOr = "findClient, lookupCompany"
List<Expense> purchases = List.of(
new Expense(500, 2016, List.of(Tag.FOOD, Tag.ENTERTAINMENT)),
new Expense(1_500, 2016, List.of(Tag.UTILITY)),
new Expense(700, 2015, List.of(Tag.TRAVEL, Tag.ENTERTAINMENT)));