/
Test.java
576 lines (483 loc) · 30.1 KB
/
Test.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package com.sitepoint.optional.example;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
public class Test {
public static void main(String[] args) {
misbehaving();
attemptedSolution();
attemptedSolutionOptionalOutputOnly();
alternativeSolutionNoOptional();
alternativeNonSolutionNoOptional();
alternativeSolutionOptionalOutputOnly();
alternativeSolutionOptionalInputOnly();
}
public static void misbehaving() {
Map<Long, Account> bank = new HashMap<>();
Function<Long, Account> findAccount = id -> bank.get(id);
Function<Account, Balance> extractBalance = account -> account != null ? account.getBalance() : new Balance(0., Currency.DOLLAR);
Function<Balance, Double> toDollars = balance -> {
if (balance == null) {
return 0.;
}
switch (balance.getCurrency()) {
case DOLLAR: return balance.getAmount();
case POUND: return balance.getAmount() * 1.3;
case EURO: return balance.getAmount() * 1.1;
default: return 0.;
}
};
Optional<Long> accountId1 = Optional.of(1L);
Optional<Long> accountId2 = Optional.ofNullable(null);
Optional<Long> accountId3 = Optional.of(3L);
Optional<Long> accountId4 = Optional.of(4L);
Optional<Long> accountId5 = Optional.of(5L);
bank.put(1L, new Account(1L, new Balance(100., Currency.EURO)));
bank.put(4L, null);
bank.put(5L, new Account(2L, null));
System.out.println("****************************************************************************");
System.out.println("Example 1 - Misbehaving naive solution\n");
System.out.println("Account 1"); // An account properly stored with a non-null balance
System.out.println(accountId1.map(findAccount).map(extractBalance).map(toDollars)); // 110.0
System.out.println(accountId1.map(id -> toDollars.apply(extractBalance.apply(findAccount.apply(id))))); // 110.0
System.out.println("Account 2"); // id == null => Optional.empty
System.out.println(accountId2.map(findAccount).map(extractBalance).map(toDollars)); // Optional.empty
System.out.println(accountId2.map(id -> toDollars.apply(extractBalance.apply(findAccount.apply(id))))); // Optional.empty
System.out.println("Account 3"); // Id not in map => findAccount returns null
System.out.println(accountId3.map(findAccount).map(extractBalance).map(toDollars)); // Optional.empty
System.out.println(accountId3.map(id -> toDollars.apply(extractBalance.apply(findAccount.apply(id))))); // 0.0
System.out.println("Account 4"); //Id associated to null in the map
System.out.println(accountId4.map(findAccount).map(extractBalance).map(toDollars)); // Optional.empty
System.out.println(accountId4.map(id -> toDollars.apply(extractBalance.apply(findAccount.apply(id))))); // 0.0
System.out.println("Account 5"); // Account's balance is null
System.out.println(accountId5.map(findAccount).map(extractBalance).map(toDollars)); // Optional.empty
System.out.println(accountId5.map(id -> toDollars.apply(extractBalance.apply(findAccount.apply(id))))); // 0.0
}
/**
* This is the first solution we came up with to cope with `Optional` breaking the Monad laws.
* We need to re-engineer the functions in the Optional-map-chain so that they won't treat `null` as a special case,
* and that they use `Optional` for both their inputs and outputs.
*/
public static void attemptedSolution() {
Map<Long, Account> bank = new HashMap<>();
Function<Long, Optional<Account>> findAccountOpt = id -> Optional.ofNullable(bank.get(id));
Function<Optional<Account>, Optional<Balance>> extractBalanceOpt = accountOpt -> {
Optional<Balance> balanceOpt = accountOpt.map(Account::getBalance);
return balanceOpt.isPresent()
? balanceOpt
: Optional.of(new Balance(0., Currency.DOLLAR));
};
Function<Optional<Balance>, Optional<Double>> toDollarsOpt = balanceOpt -> {
Function<Balance, Double> toDollars = balance -> {
switch (balance.getCurrency()){
case DOLLAR:
return balance.getAmount();
case POUND:
return balance.getAmount() * 1.3;
case EURO:
return balance.getAmount() * 1.1;
default:
return 0.;
}
};
Optional<Double> dollarsOpt = balanceOpt.map(toDollars);
return dollarsOpt.isPresent()
? dollarsOpt
: Optional.of(0.);
};
Optional<Long> accountId1 = Optional.of(1L);
Optional<Long> accountId2 = Optional.ofNullable(null);
Optional<Long> accountId3 = Optional.of(3L);
Optional<Long> accountId4 = Optional.of(4L);
Optional<Long> accountId5 = Optional.of(5L);
bank.put(1L, new Account(1L, new Balance(100., Currency.EURO)));
bank.put(4L, null);
bank.put(5L, new Account(2L, null));
System.out.println("****************************************************************************");
System.out.println("Example 2 - Working solution 1\n");
System.out.println("Account 1"); // An account properly stored with a non-null balance
System.out.println(accountId1.map(findAccountOpt).map(extractBalanceOpt).flatMap(toDollarsOpt)); // 110.0
System.out.println(accountId1.flatMap(findAccountOpt.andThen(extractBalanceOpt).andThen(toDollarsOpt))); // 110.0
System.out.println("Account 2"); // id == null => Optional.empty
System.out.println(accountId2.map(findAccountOpt).map(extractBalanceOpt).flatMap(toDollarsOpt)); // Optional.empty
System.out.println(accountId2.flatMap(findAccountOpt.andThen(extractBalanceOpt).andThen(toDollarsOpt))); // Optional.empty
System.out.println("Account 3"); // Id not in map => findAccountOpt returns null
System.out.println(accountId3.map(findAccountOpt).map(extractBalanceOpt).flatMap(toDollarsOpt)); // 0.0
System.out.println(accountId3.flatMap(findAccountOpt.andThen(extractBalanceOpt).andThen(toDollarsOpt))); // 0.0
System.out.println("Account 4"); //Id associated to null in the map
System.out.println(accountId4.map(findAccountOpt).map(extractBalanceOpt).flatMap(toDollarsOpt)); // 0.0
System.out.println(accountId4.flatMap(findAccountOpt.andThen(extractBalanceOpt).andThen(toDollarsOpt))); // 0.0
System.out.println("Account 5"); // Account's balance is null
System.out.println(accountId5.map(findAccountOpt).map(extractBalanceOpt).flatMap(toDollarsOpt)); // 0.0
System.out.println(accountId5.flatMap(findAccountOpt.andThen(extractBalanceOpt).andThen(toDollarsOpt))); // 0.0
}
/**
* One might ask: isn't avoiding any handling of `null` inside the functions enough?
* The answer is no. Even if we refrain from explicitly handling `null`, it doesn't mean it will never be handled.
* For instance, since we don't check for `null` as part of the deal, we can have `NullPointerException` thrown by
* the composed function, while mapping the individual functions in a chain would simply return `Optional.empty`.
* So unfortunately this breaks associativity law, because of exceptions being thrown - even if we don't explicitly
* add any side effects, we still get some because of `null` not being checked.
*/
public static void alternativeSolutionNoOptional() {
Map<Long, Account> bank = new HashMap<>();
Function<Long, Account> findAccount = id -> bank.get(id);
Function<Account, Balance> extractBalance = account -> account.getBalance();
Function<Balance, Double> toDollars = balance -> {
switch (balance.getCurrency()){
case DOLLAR:
return balance.getAmount();
case POUND:
return balance.getAmount() * 1.3;
case EURO:
return balance.getAmount() * 1.1;
default:
return 0.;
}
};
Optional<Long> accountId1 = Optional.of(1L);
Optional<Long> accountId2 = Optional.ofNullable(null);
Optional<Long> accountId3 = Optional.of(3L);
Optional<Long> accountId4 = Optional.of(4L);
Optional<Long> accountId5 = Optional.of(5L);
bank.put(1L, new Account(1L, new Balance(100., Currency.EURO)));
bank.put(4L, null);
bank.put(5L, new Account(2L, null));
System.out.println("****************************************************************************");
System.out.println("Example 3 - Misbehaving solution: Ignoring null\n");
System.out.println("Account 1"); // An account properly stored with a non-null balance
System.out.println(accountId1.map(findAccount).map(extractBalance).map(toDollars)); // 110.0
System.out.println(accountId1.map(findAccount.andThen(extractBalance).andThen(toDollars))); // 110.0
System.out.println("Account 2"); // id == null => Optional.empty
System.out.println(accountId2.map(findAccount).map(extractBalance).map(toDollars)); // Optional.empty
System.out.println(accountId2.map(findAccount.andThen(extractBalance).andThen(toDollars))); // Optional.empty
System.out.println("Account 3"); // Id not in map => findAccount returns null
System.out.println(accountId3.map(findAccount).map(extractBalance).map(toDollars)); // Optional.empty
// System.out.println(accountId3.map(findAccount.andThen(extractBalance).andThen(toDollars))); // NullPointerException
System.out.println("Account 4"); //Id associated to null in the map
System.out.println(accountId4.map(findAccount).map(extractBalance).map(toDollars)); // Optional.empty
// System.out.println(accountId4.map(findAccount.andThen(extractBalance).andThen(toDollars))); // NullPointerException
System.out.println("Account 5"); // Account's balance is null
System.out.println(accountId5.map(findAccount).map(extractBalance).map(toDollars)); // Optional.empty
// System.out.println(accountId5.map(findAccount.andThen(extractBalance).andThen(toDollars))); // NullPointerException
}
/**
* We could of course build up on the previous attempt, and just check for `null` and return it.
* This is not a meaningful solution. While, in fact, this would allow us to avoid breaking associativity and left
* identity, it would defy altogether the very purpose of `Optional`: we could as well just remove the overhead
* and keep dealing with `null` the way we were.
*/
public static void alternativeNonSolutionNoOptional() {
Map<Long, Account> bank = new HashMap<>();
Function<Long, Account> findAccount = id -> bank.get(id);
Function<Account, Balance> extractBalance = account -> account == null ? null : account.getBalance();
Function<Balance, Double> toDollars = balance -> {
if (balance == null) {
return null;
} else {
switch (balance.getCurrency()) {
case DOLLAR:
return balance.getAmount();
case POUND:
return balance.getAmount() * 1.3;
case EURO:
return balance.getAmount() * 1.1;
default:
return 0.;
}
}
};
Optional<Long> accountId1 = Optional.of(1L);
Optional<Long> accountId2 = Optional.ofNullable(null);
Optional<Long> accountId3 = Optional.of(3L);
Optional<Long> accountId4 = Optional.of(4L);
Optional<Long> accountId5 = Optional.of(5L);
bank.put(1L, new Account(1L, new Balance(100., Currency.EURO)));
bank.put(4L, null);
bank.put(5L, new Account(2L, null));
System.out.println("****************************************************************************");
System.out.println("Example 4 - Working non-Solution: null guards\n");
System.out.println("Account 1"); // An account properly stored with a non-null balance
System.out.println(accountId1.map(findAccount).map(extractBalance).map(toDollars)); // 110.0
System.out.println(accountId1.map(findAccount.andThen(extractBalance).andThen(toDollars))); // 110.0
System.out.println("Account 2"); // id == null => Optional.empty
System.out.println(accountId2.map(findAccount).map(extractBalance).map(toDollars)); // Optional.empty
System.out.println(accountId2.map(findAccount.andThen(extractBalance).andThen(toDollars))); // Optional.empty
System.out.println("Account 3"); // Id not in map => findAccount returns null
System.out.println(accountId3.map(findAccount).map(extractBalance).map(toDollars)); // Optional.empty
System.out.println(accountId3.map(findAccount.andThen(extractBalance).andThen(toDollars))); // NullPointerException
System.out.println("Account 4"); //Id associated to null in the map
System.out.println(accountId4.map(findAccount).map(extractBalance).map(toDollars)); // Optional.empty
System.out.println(accountId4.map(findAccount.andThen(extractBalance).andThen(toDollars))); // NullPointerException
System.out.println("Account 5"); // Account's balance is null
System.out.println(accountId5.map(findAccount).map(extractBalance).map(toDollars)); // Optional.empty
System.out.println(accountId5.map(findAccount.andThen(extractBalance).andThen(toDollars))); // NullPointerException
}
/**
* We might try to just use `Optional` for the return types. We'll have a type mismatch when we try to compose
* functions, but nothing we can't solve: we have two ways to compose all the functions:
* - add `Optional::get` in between any two composed functions.
* - add `Optional::orElse` with proper alternative parameters.
* - use `flatMap` in the composed function.
* The first solution is equivalent to the case `alternativeSolutionNoOptional`, and will throw `NullPointerException`s.
* The second one, instead, won't throw exceptions, but we would defer to users the choice of the appropriate default values.
* What's worse, it would make so easy to pass parameters to `orElse` that would break associativity law again:
* for example `new Balance(0., Currency.DOLLAR)` - see last example below.
* (NOTE: in this case we won't technically break the associativity law as the composition includes functions not
* used in the map chain, but still, the point is that we would fail the expected invariance between chaining and
* composing functions).
* We'll check the third one in the next method.
*/
public static void alternativeSolutionOptionalOutputOnly() {
Map<Long, Account> bank = new HashMap<>();
Function<Long, Optional<Account>> findAccount = id -> Optional.ofNullable(bank.get(id));
Function<Account, Optional<Balance>> extractBalance = account -> Optional.ofNullable(account.getBalance());
Function<Balance, Optional<Double>> toDollars = balance -> {
switch (balance.getCurrency()){
case DOLLAR:
return Optional.ofNullable(balance.getAmount());
case POUND:
return Optional.ofNullable(balance.getAmount() * 1.3);
case EURO:
return Optional.ofNullable(balance.getAmount() * 1.1);
default:
return Optional.ofNullable(0.);
}
};
Optional<Long> accountId1 = Optional.of(1L);
Optional<Long> accountId2 = Optional.ofNullable(null);
Optional<Long> accountId3 = Optional.of(3L);
Optional<Long> accountId4 = Optional.of(4L);
Optional<Long> accountId5 = Optional.of(5L);
bank.put(1L, new Account(1L, new Balance(100., Currency.EURO)));
bank.put(4L, null);
bank.put(5L, new Account(2L, null));
System.out.println("****************************************************************************");
System.out.println("Example 5 - Misbehaving solutions: Optional as a return type only\n");
System.out.println("Account 1"); // An account properly stored with a non-null balance
System.out.println(accountId1.flatMap(findAccount).flatMap(extractBalance).flatMap(toDollars)); // 110.0
System.out.println(accountId1.flatMap(findAccount.andThen(Optional::get).andThen(extractBalance).andThen(Optional::get).andThen(toDollars))); // 110.0
System.out.println(accountId1.flatMap(
findAccount
.andThen(x -> x.orElse(new Account(0L, new Balance(0., Currency.DOLLAR))))
.andThen(extractBalance)
.andThen(x -> x.orElse(new Balance(0., Currency.DOLLAR)))
.andThen(toDollars))); // 110.0
System.out.println("Account 2"); // id == null => Optional.empty
System.out.println(accountId2.flatMap(findAccount).flatMap(extractBalance).flatMap(toDollars)); // Optional.empty
System.out.println(accountId2.flatMap(
findAccount.andThen(Optional::get)
.andThen(extractBalance)
.andThen(Optional::get)
.andThen(toDollars))); // Optional.empty
System.out.println(accountId2.flatMap(
findAccount
.andThen(x -> x.orElse(new Account(0L, null)))
.andThen(extractBalance)
.andThen(x -> x.orElse(new Balance(null, Currency.DOLLAR)))
.andThen(toDollars))); // Optional.empty
System.out.println("Account 3"); // Id not in map => findAccount returns null
System.out.println(accountId3.flatMap(findAccount).flatMap(extractBalance).flatMap(toDollars)); // Optional.empty
// System.out.println(accountId3.flatMap(
// findAccount.andThen(Optional::get)
// .andThen(extractBalance)
// .andThen(Optional::get)
// .andThen(toDollars))); // NullPointerException
System.out.println(accountId3.flatMap(
findAccount
.andThen(x -> x.orElse(new Account(0L, null)))
.andThen(extractBalance)
.andThen(x -> x.orElse(new Balance(null, Currency.DOLLAR)))
.andThen(toDollars))); // Optional.empty
System.out.println("Account 4"); //Id associated to null in the map
System.out.println(accountId4.flatMap(findAccount).flatMap(extractBalance).flatMap(toDollars)); // Optional.empty
// System.out.println(accountId4.flatMap(
// findAccount.andThen(Optional::get)
// .andThen(extractBalance)
// .andThen(Optional::get)
// .andThen(toDollars))); // NullPointerException
System.out.println(accountId4.flatMap(
findAccount
.andThen(x -> x.orElse(new Account(0L, null)))
.andThen(extractBalance)
.andThen(x -> x.orElse(new Balance(null, Currency.DOLLAR)))
.andThen(toDollars))); // Optional.empty
System.out.println("Account 5"); // Account's balance is null
System.out.println(accountId5.flatMap(findAccount).flatMap(extractBalance).flatMap(toDollars)); // Optional.empty
// System.out.println(accountId5.flatMap(
// findAccount.andThen(Optional::get)
// .andThen(extractBalance)
// .andThen(Optional::get)
// .andThen(toDollars))); // NullPointerException
System.out.println(accountId5.flatMap(
findAccount
.andThen(x -> x.orElse(new Account(0L, null)))
.andThen(extractBalance)
.andThen(x -> x.orElse(new Balance(null, Currency.DOLLAR)))
.andThen(toDollars))); // Optional.empty
System.out.println(accountId5.flatMap(
findAccount
.andThen(x -> x.orElse(new Account(0L, null)))
.andThen(extractBalance)
.andThen(x -> x.orElse(new Balance(0., Currency.DOLLAR)))
.andThen(toDollars))); // Optional[0.]
}
/**
* Following up on the attempts in the previous method, we'll now show how using Optional as a return type only
* is the right solution.
* This is the simplest re-engineering that prevents any violation to the left identity law and the associativity law.
* In summary we need to:
* 1. Avoid handling `null` in any way inside the functions in the map-chain.
* 2. Use `Optional<?>` for the return types (only) of all the functions in the chain.
* 3. When composing them, be careful and make a clever use of `apply` and `flatMap`.
*
* The function definitions and the types of the intermediate results are simpler than the other solution.
* The only glitch is that these definitions makes impossible to compose the functions using `andThen` or just
* `apply` alone, which in turn might create maintainability concerns, in case new developers on the code are
* not familiar with this pattern and approach refactoring.
* However, this is not uncommon in functional programming with monads: check this post
* http://learnyouahaskell.com/a-fistful-of-monads#monad-laws
* where this is explained clearly for Haskell.
*/
public static void attemptedSolutionOptionalOutputOnly() {
Map<Long, Account> bank = new HashMap<>();
Function<Long, Optional<Account>> findAccount = id -> Optional.ofNullable(bank.get(id));
Function<Account, Optional<Balance>> extractBalance = account -> Optional.ofNullable(account.getBalance());
Function<Balance, Optional<Double>> toDollars = balance -> {
switch (balance.getCurrency()){
case DOLLAR:
return Optional.ofNullable(balance.getAmount());
case POUND:
return Optional.ofNullable(balance.getAmount() * 1.3);
case EURO:
return Optional.ofNullable(balance.getAmount() * 1.1);
default:
return Optional.ofNullable(0.);
}
};
Optional<Long> accountId1 = Optional.of(1L);
Optional<Long> accountId2 = Optional.ofNullable(null);
Optional<Long> accountId3 = Optional.of(3L);
Optional<Long> accountId4 = Optional.of(4L);
Optional<Long> accountId5 = Optional.of(5L);
bank.put(1L, new Account(1L, new Balance(100., Currency.EURO)));
bank.put(4L, null);
bank.put(5L, new Account(2L, null));
System.out.println("****************************************************************************");
System.out.println("Example 6 - Better working solution: Optional as a return type only\n");
System.out.println("Account 1"); // An account properly stored with a non-null balance
System.out.println(accountId1.flatMap(findAccount).flatMap(extractBalance).flatMap(toDollars)); // 110.0
System.out.println(accountId1.flatMap(id ->
findAccount.apply(id).flatMap(account ->
extractBalance.apply(account)).flatMap(toDollars))); // 110.0
System.out.println("Account 2"); // id == null => Optional.empty
System.out.println(accountId2.flatMap(findAccount).flatMap(extractBalance).flatMap(toDollars)); // Optional.empty
System.out.println(accountId2.flatMap(id ->
findAccount.apply(id).flatMap(account ->
extractBalance.apply(account)).flatMap(toDollars))); // Optional.empty
System.out.println("Account 3"); // Id not in map => findAccount returns null
System.out.println(accountId3.flatMap(findAccount).flatMap(extractBalance).flatMap(toDollars)); // Optional.empty
System.out.println(accountId3.flatMap(id ->
findAccount.apply(id).flatMap(account ->
extractBalance.apply(account)).flatMap(toDollars))); // Optional.empty
System.out.println("Account 4"); //Id associated to null in the map
System.out.println(accountId4.flatMap(findAccount).flatMap(extractBalance).flatMap(toDollars)); // Optional.empty
System.out.println(accountId4.flatMap(id ->
findAccount.apply(id).flatMap(account ->
extractBalance.apply(account)).flatMap(toDollars))); // Optional.empty
System.out.println("Account 5"); // Account's balance is null
System.out.println(accountId5.flatMap(findAccount).flatMap(extractBalance).flatMap(toDollars)); // Optional.empty
System.out.println(accountId5.flatMap(id ->
findAccount.apply(id).flatMap(account ->
extractBalance.apply(account)).flatMap(toDollars))); // Optional[0.]
}
/**
* Finally, we might try to use `Optional` only in the input types for the functions in the chain.
* We'd be forced to insert Optional::ofNullable at every step of the chain and of the composition to make the types work.
* And the final result would be: we'd break associativity law anyway.
*/
public static void alternativeSolutionOptionalInputOnly() {
Map<Long, Account> bank = new HashMap<>();
Function<Long, Account> findAccount = id -> bank.get(id);
Function<Optional<Account>, Balance> extractBalance = accountOpt -> {
Optional<Balance> balanceOpt = accountOpt.map(Account::getBalance);
return balanceOpt.orElse(new Balance(0., Currency.DOLLAR));
};
Function<Optional<Balance>, Double> toDollarsOpt = balanceOpt -> {
Function<Balance, Double> toDollars = balance -> {
switch (balance.getCurrency()){
case DOLLAR:
return balance.getAmount();
case POUND:
return balance.getAmount() * 1.3;
case EURO:
return balance.getAmount() * 1.1;
default:
return 0.;
}
};
Optional<Double> dollarsOpt = balanceOpt.map(toDollars);
return dollarsOpt.orElse(0.);
};
Optional<Long> accountId1 = Optional.of(1L);
Optional<Long> accountId2 = Optional.ofNullable(null);
Optional<Long> accountId3 = Optional.of(3L);
Optional<Long> accountId4 = Optional.of(4L);
Optional<Long> accountId5 = Optional.of(5L);
bank.put(1L, new Account(1L, new Balance(100., Currency.EURO)));
bank.put(4L, null);
bank.put(5L, new Account(2L, null));
System.out.println("****************************************************************************");
System.out.println("Example 7 - Misbehaving solution: Optional as Input only\n");
System.out.println("Account 1"); // An account properly stored with a non-null balance
System.out.println(accountId1.map(findAccount).map(Optional::ofNullable).map(extractBalance).map(Optional::ofNullable).map(toDollarsOpt)); // 110.0
System.out.println(accountId1.map(findAccount.andThen(Optional::ofNullable).andThen(extractBalance).andThen(Optional::ofNullable).andThen(toDollarsOpt))); // 110.0
System.out.println("Account 2"); // id == null => Optional.empty
System.out.println(accountId2.map(findAccount).map(Optional::ofNullable).map(extractBalance).map(Optional::ofNullable).map(toDollarsOpt)); // Optional.empty
System.out.println(accountId2.map(findAccount.andThen(Optional::ofNullable).andThen(extractBalance).andThen(Optional::ofNullable).andThen(toDollarsOpt))); // Optional.empty
System.out.println("Account 3"); // Id not in map => findAccount returns null
System.out.println(accountId3.map(findAccount).map(Optional::ofNullable).map(extractBalance).map(Optional::ofNullable).map(toDollarsOpt)); // Optional.empty
System.out.println(accountId3.map(findAccount.andThen(Optional::ofNullable).andThen(extractBalance).andThen(Optional::ofNullable).andThen(toDollarsOpt))); // 0.0
System.out.println("Account 4"); //Id associated to null in the map
System.out.println(accountId4.map(findAccount).map(Optional::ofNullable).map(extractBalance).map(Optional::ofNullable).map(toDollarsOpt)); // Optional.empty
System.out.println(accountId4.map(findAccount.andThen(Optional::ofNullable).andThen(extractBalance).andThen(Optional::ofNullable).andThen(toDollarsOpt))); // 0.0
System.out.println("Account 5"); // Account's balance is null
System.out.println(accountId5.map(findAccount).map(Optional::ofNullable).map(extractBalance).map(Optional::ofNullable).map(toDollarsOpt)); // 0.0
System.out.println(accountId5.map(findAccount.andThen(Optional::ofNullable).andThen(extractBalance).andThen(Optional::ofNullable).andThen(toDollarsOpt))); // 0.0
}
private static class Account {
private Long id;
Balance balance;
public Account(Long id, Balance balance) {
this.id = id;
this.balance = balance;
}
public Long getId() {
return id;
}
public Balance getBalance() {
return balance;
}
}
private enum Currency {
DOLLAR, POUND, EURO
}
private static class Balance {
private Double amount;
private Currency currency;
public Balance(Double amount, Currency currency) {
this.amount = amount;
this.currency = currency;
}
public Balance(Double amount) {
this(amount, Currency.DOLLAR);
}
public Double getAmount() {
return amount;
}
public Currency getCurrency() {
return currency;
}
}
}