|
1 | 1 | package com.iluwatar.fluentinterface; |
2 | 2 |
|
| 3 | +import com.iluwatar.fluentinterface.fluentiterable.FluentIterable; |
3 | 4 | import com.iluwatar.fluentinterface.fluentiterable.lazy.LazyFluentIterable; |
4 | 5 | import com.iluwatar.fluentinterface.fluentiterable.simple.SimpleFluentIterable; |
5 | 6 |
|
|
9 | 10 |
|
10 | 11 | import static java.lang.String.valueOf; |
11 | 12 |
|
| 13 | +/** |
| 14 | + * Fluent interface pattern is useful when you want to provide an easy readable, flowing API. Those |
| 15 | + * interfaces tend to mimic domain specific languages, so they can nearly be read as human |
| 16 | + * languages. |
| 17 | + * <p> |
| 18 | + * In this example two implementations of a {@link FluentIterable} interface are given. The |
| 19 | + * SimpleFluentIterable evaluates eagerly and would be too costly for real world applications. The |
| 20 | + * LazyFluentIterable is evaluated on termination. Their usage is demonstrated with a simple number |
| 21 | + * list that is filtered, transformed and collected. The result is printed afterwards. |
| 22 | + * <p> |
| 23 | + */ |
12 | 24 | public class App { |
13 | 25 |
|
14 | | - public static void main(String[] args) { |
15 | | - |
16 | | - List<Integer> integerList = new ArrayList<Integer>() {{ |
17 | | - add(1); |
18 | | - add(-61); |
19 | | - add(14); |
20 | | - add(-22); |
21 | | - add(18); |
22 | | - add(-87); |
23 | | - add(6); |
24 | | - add(64); |
25 | | - add(-82); |
26 | | - add(26); |
27 | | - add(-98); |
28 | | - add(97); |
29 | | - add(45); |
30 | | - add(23); |
31 | | - add(2); |
32 | | - add(-68); |
33 | | - add(45); |
34 | | - }}; |
35 | | - prettyPrint("The initial list contains: ", integerList); |
36 | | - |
37 | | - List<Integer> firstFiveNegatives = SimpleFluentIterable.from(integerList) |
38 | | - .filter(negatives()) |
39 | | - .first(3) |
40 | | - .asList(); |
41 | | - prettyPrint("The first three negative values are: ", firstFiveNegatives); |
42 | | - |
43 | | - |
44 | | - List<Integer> lastTwoPositives = SimpleFluentIterable.from(integerList) |
45 | | - .filter(positives()) |
46 | | - .last(2) |
47 | | - .asList(); |
48 | | - prettyPrint("The last two positive values are: ", lastTwoPositives); |
49 | | - |
50 | | - SimpleFluentIterable.from(integerList) |
51 | | - .filter(number -> number%2 == 0) |
52 | | - .first() |
53 | | - .ifPresent(evenNumber -> System.out.println(String.format("The first even number is: %d", evenNumber))); |
54 | | - |
55 | | - |
56 | | - List<String> transformedList = SimpleFluentIterable.from(integerList) |
57 | | - .filter(negatives()) |
58 | | - .map(transformToString()) |
59 | | - .asList(); |
60 | | - prettyPrint("A string-mapped list of negative numbers contains: ", transformedList); |
61 | | - |
62 | | - |
63 | | - List<String> lastTwoOfFirstFourStringMapped = LazyFluentIterable.from(integerList) |
64 | | - .filter(positives()) |
65 | | - .first(4) |
66 | | - .last(2) |
67 | | - .map(number -> "String[" + String.valueOf(number) + "]") |
68 | | - .asList(); |
69 | | - prettyPrint("The lazy list contains the last two of the first four positive numbers mapped to Strings: ", lastTwoOfFirstFourStringMapped); |
70 | | - |
71 | | - LazyFluentIterable.from(integerList) |
72 | | - .filter(negatives()) |
73 | | - .first(2) |
74 | | - .last() |
75 | | - .ifPresent(lastOfFirstTwo -> System.out.println(String.format("The last of the first two negatives is: %d", lastOfFirstTwo))); |
| 26 | + public static void main(String[] args) { |
| 27 | + |
| 28 | + List<Integer> integerList = new ArrayList<Integer>() { |
| 29 | + { |
| 30 | + add(1); |
| 31 | + add(-61); |
| 32 | + add(14); |
| 33 | + add(-22); |
| 34 | + add(18); |
| 35 | + add(-87); |
| 36 | + add(6); |
| 37 | + add(64); |
| 38 | + add(-82); |
| 39 | + add(26); |
| 40 | + add(-98); |
| 41 | + add(97); |
| 42 | + add(45); |
| 43 | + add(23); |
| 44 | + add(2); |
| 45 | + add(-68); |
| 46 | + add(45); |
| 47 | + } |
| 48 | + }; |
| 49 | + prettyPrint("The initial list contains: ", integerList); |
| 50 | + |
| 51 | + List<Integer> firstFiveNegatives = |
| 52 | + SimpleFluentIterable.fromCopyOf(integerList).filter(negatives()).first(3).asList(); |
| 53 | + prettyPrint("The first three negative values are: ", firstFiveNegatives); |
| 54 | + |
| 55 | + |
| 56 | + List<Integer> lastTwoPositives = |
| 57 | + SimpleFluentIterable.fromCopyOf(integerList).filter(positives()).last(2).asList(); |
| 58 | + prettyPrint("The last two positive values are: ", lastTwoPositives); |
| 59 | + |
| 60 | + SimpleFluentIterable |
| 61 | + .fromCopyOf(integerList) |
| 62 | + .filter(number -> number % 2 == 0) |
| 63 | + .first() |
| 64 | + .ifPresent( |
| 65 | + evenNumber -> System.out.println(String.format("The first even number is: %d", |
| 66 | + evenNumber))); |
| 67 | + |
| 68 | + |
| 69 | + List<String> transformedList = |
| 70 | + SimpleFluentIterable.fromCopyOf(integerList).filter(negatives()).map(transformToString()) |
| 71 | + .asList(); |
| 72 | + prettyPrint("A string-mapped list of negative numbers contains: ", transformedList); |
| 73 | + |
| 74 | + |
| 75 | + List<String> lastTwoOfFirstFourStringMapped = |
| 76 | + LazyFluentIterable.from(integerList).filter(positives()).first(4).last(2) |
| 77 | + .map(number -> "String[" + String.valueOf(number) + "]").asList(); |
| 78 | + prettyPrint( |
| 79 | + "The lazy list contains the last two of the first four positive numbers mapped to Strings: ", |
| 80 | + lastTwoOfFirstFourStringMapped); |
| 81 | + |
| 82 | + LazyFluentIterable |
| 83 | + .from(integerList) |
| 84 | + .filter(negatives()) |
| 85 | + .first(2) |
| 86 | + .last() |
| 87 | + .ifPresent( |
| 88 | + lastOfFirstTwo -> System.out.println(String.format( |
| 89 | + "The last of the first two negatives is: %d", lastOfFirstTwo))); |
| 90 | + } |
| 91 | + |
| 92 | + private static Function<Integer, String> transformToString() { |
| 93 | + return integer -> "String[" + valueOf(integer) + "]"; |
| 94 | + } |
| 95 | + |
| 96 | + private static Predicate<? super Integer> negatives() { |
| 97 | + return integer -> (integer < 0); |
| 98 | + } |
| 99 | + |
| 100 | + private static Predicate<? super Integer> positives() { |
| 101 | + return integer -> (integer > 0); |
| 102 | + } |
| 103 | + |
| 104 | + private static <TYPE> void prettyPrint(String prefix, Iterable<TYPE> iterable) { |
| 105 | + prettyPrint(", ", prefix, ".", iterable); |
| 106 | + } |
| 107 | + |
| 108 | + private static <TYPE> void prettyPrint(String prefix, String suffix, Iterable<TYPE> iterable) { |
| 109 | + prettyPrint(", ", prefix, suffix, iterable); |
| 110 | + } |
| 111 | + |
| 112 | + private static <TYPE> void prettyPrint(String delimiter, String prefix, String suffix, |
| 113 | + Iterable<TYPE> iterable) { |
| 114 | + StringJoiner joiner = new StringJoiner(delimiter, prefix, "."); |
| 115 | + Iterator<TYPE> iterator = iterable.iterator(); |
| 116 | + while (iterator.hasNext()) { |
| 117 | + joiner.add(iterator.next().toString()); |
76 | 118 | } |
77 | 119 |
|
78 | | - private static Function<Integer, String> transformToString() { |
79 | | - return integer -> "String[" + valueOf(integer) + "]"; |
80 | | - } |
81 | | - private static Predicate<? super Integer> negatives() { |
82 | | - return integer -> (integer < 0); |
83 | | - } |
84 | | - private static Predicate<? super Integer> positives() { |
85 | | - return integer -> (integer > 0); |
86 | | - } |
87 | | - |
88 | | - private static <TYPE> void prettyPrint(String prefix, Iterable<TYPE> iterable) { |
89 | | - prettyPrint(", ", prefix, ".", iterable); |
90 | | - } |
91 | | - private static <TYPE> void prettyPrint(String prefix, String suffix, Iterable<TYPE> iterable) { |
92 | | - prettyPrint(", ", prefix, suffix, iterable); |
93 | | - } |
94 | | - |
95 | | - private static <TYPE> void prettyPrint(String delimiter, String prefix, String suffix, Iterable<TYPE> iterable) { |
96 | | - StringJoiner joiner = new StringJoiner(delimiter, prefix, "."); |
97 | | - Iterator<TYPE> iterator = iterable.iterator(); |
98 | | - while (iterator.hasNext()) { |
99 | | - joiner.add(iterator.next().toString()); |
100 | | - } |
101 | | - |
102 | | - System.out.println(joiner); |
103 | | - } |
| 120 | + System.out.println(joiner); |
| 121 | + } |
104 | 122 | } |
0 commit comments