11package org .adoptopenjdk .lambda .tutorial ;
22
3-
43import org .adoptopenjdk .lambda .tutorial .exercise1 .Color ;
54import org .adoptopenjdk .lambda .tutorial .exercise1 .Shape ;
65import org .adoptopenjdk .lambda .tutorial .exercise1 .Shapes ;
2221import static org .hamcrest .Matchers .everyItem ;
2322import static org .hamcrest .Matchers .hasSize ;
2423
25-
2624/**
2725 * Exercise 1 - Internal vs External iteration.
28- *
26+ * <p>
2927 * As described in Brian Goetz's State of the Lambda - Libraries Edition[0], Java's collection classes provide a way for
3028 * clients to enumerate the members of a collection. Currently, this is iteration is "External" - that is, the
3129 * collection can be iterated in sequence, by the client code.
32- *
30+ * </p>
31+ * <p>
3332 * This refers to the trusty "for loop":
34- *
35- * for (Shape s: shapes) {
33+ * <pre>
34+ * {@code
35+ * for (Shape s: shapes) {
3636 * s.setColor(RED)
37- * }
38- *
37+ * }
38+ * }
39+ * </pre>
40+ * </p>
41+ * <p>
3942 * JDK 8, with lambdas and an updated Collections library, will allow "Internal" iteration. In this case, the collection
4043 * receives some code, and decides how to apply that to its elements. This has several benefits, including:
4144 * - allowing the collection to decide how to handle executing given code, including opening the door to parallelism and laziness
4245 * - leads to a style where operations can be pipelined, into a more fluent, readable style.
43- *
46+ * </p>
47+ * <p>
4448 * Internal iteration, using lambda expression syntax, turns the above for loop into:
45- *
46- * shapes.forEach(s -> s.setColor(RED))
47- *
48- * Where "s -> s.setColor(RED)" is a lambda expression, which is passed into forEach, and invoked inside forEach. Lambda
49+ * <pre>
50+ * {@code
51+ * shapes.forEach(s -> s.setColor(RED))
52+ * }
53+ * </pre>
54+ * Where <code>s -> s.setColor(RED)</code> is a lambda expression, which is passed into forEach, and invoked inside forEach. Lambda
4955 * expressions have a type, called a "Functional Interface". In this case the lambda is of type Consumer. Consumer declares
50- * a single method, "void accept(T t)". This is all hidden by the Java compiler, and unless you extract the lambda to a
51- * variable, or write a method which accepts a lambda, you don't really need to know about it.
52- *
53- * The below tests can be made to pass using for loops. Try to make them pass without using a for loop.
54- *
56+ * a single method, <code>void accept(T t)</code>. This is all hidden by the Java compiler, and unless you extract the lambda
57+ * to a variable, or write a method which accepts a lambda, you don't really need to know about it.
58+ * </p>
59+ * <p>
60+ * The tests below can be made to pass using for loops. Try to make them pass without using a for loop.
61+ * </p>
5562 * [0] http://cr.openjdk.java.net/~briangoetz/lambda/sotc3.html
56- *
63+ *
5764 * @see java.lang.Iterable#forEach
5865 * @see Shape
5966 * @see Shapes
6067 * @see Color
6168 *
62- * Lambda Tutorial -- Adopt Open JDK
6369 * @author Graham Allan grundlefleck at gmail dot com
6470 */
6571@ SuppressWarnings ("unchecked" )
@@ -110,7 +116,6 @@ public void buildStringRepresentingAllShapes() {
110116 assertThat (builder .toString (), equalTo ("[a RED shape][a BLACK shape][a YELLOW shape]" ));
111117 }
112118
113-
114119 /**
115120 * Use forEach to change the color, and build a string showing the old colors of each shape.
116121 *
@@ -131,17 +136,12 @@ public void changeColorOfAllShapes_AND_buildStringShowingAllTheOldColors() {
131136
132137 assertThat (myShapes , hasSize (3 ));
133138 assertThat (myShapes , everyItem (hasColor (GREEN )));
134- assertThat (builder .toString (), equalTo ("[BLUE][ BLACK][ YELLOW]" ));
139+ assertThat (builder .toString (), equalTo ("[a BLUE shape][a BLACK shape][a YELLOW shape ]" ));
135140 }
136141
137-
138-
139- // Test helpers
142+ // ----- Test helpers -----
140143
141144 private static Matcher <Shape > hasColor (Color color ) {
142145 return FeatureMatchers .from (Matchers .is (color ), "has color" , "color" , Shape ::getColor );
143146 }
144147}
145-
146-
147-
0 commit comments