-
Notifications
You must be signed in to change notification settings - Fork 25
/
TradJavaStreamComparisonTest.java
281 lines (241 loc) · 12.7 KB
/
TradJavaStreamComparisonTest.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
package org.organicdesign.fp;
import org.junit.Test;
import org.organicdesign.fp.collections.ImList;
import org.organicdesign.fp.collections.ImMap;
import org.organicdesign.fp.collections.RangeOfInt;
import org.organicdesign.fp.tuple.Tuple3;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.*;
import static org.organicdesign.fp.StaticImports.*;
import static org.organicdesign.fp.TradJavaStreamComparisonTest.ColorVal.*;
public class TradJavaStreamComparisonTest {
// Declare a simple enum with a character mapping (eg. for storing in a database or passing
// over a network). We'll calculate and store the reverse-mapping three different ways,
// Once with Paguro (3 lines), once using "Traditional" Java (6 lines plus closing braces),
// and once using the new Java8 streams (9 lines, plus closing braces).
public enum ColorVal {
RED('R'),
GREEN('G'),
BLUE('B');
private final Character ch;
ColorVal(Character c) { ch = c; }
public Character ch() { return ch; }
// The Paguro way (3 lines of code):
static final ImMap<Character,ColorVal> charMapP =
xformArray(values())
.toImMap(v -> tup(v.ch(), v));
// Same thing in "traditional" Java (6 lines, plus closing braces):
static final Map<Character,ColorVal> charMapT;
static {
Map<Character,ColorVal> tempMap = new HashMap<>();
for (ColorVal v : values()) {
tempMap.put(v.ch(), v);
}
charMapT = Collections.unmodifiableMap(tempMap);
}
// Same thing with Java 8's streams (3 lines thanks to @codepoetics on reddit)
static final Map<Character,ColorVal> charMap8 = Collections.unmodifiableMap(
Stream.of(values())
.collect(Collectors.toMap(ColorVal::ch, Function.identity())));
// If you were using a mutable map, you'd want to protect it with a method.
// It's still a good idea to do that on public classes to defend against having to change
// your public interface over time.
public static ColorVal fromCharP(Character c) { return charMapP.get(c); }
public static ColorVal fromCharT(Character c) { return charMapT.get(c); }
public static ColorVal fromChar8(Character c) { return charMap8.get(c); }
}
// Prove that all three reverse-mappings work.
@Test public void testReverseMapping() {
assertEquals(RED, ColorVal.fromCharP('R'));
assertEquals(RED, ColorVal.fromCharT('R'));
assertEquals(RED, ColorVal.fromChar8('R'));
assertEquals(GREEN, ColorVal.fromCharP('G'));
assertEquals(GREEN, ColorVal.fromCharT('G'));
assertEquals(GREEN, ColorVal.fromChar8('G'));
assertEquals(BLUE, ColorVal.fromCharP('B'));
assertEquals(BLUE, ColorVal.fromCharT('B'));
assertEquals(BLUE, ColorVal.fromChar8('B'));
assertNull(ColorVal.fromCharP('x'));
assertNull(ColorVal.fromCharT('x'));
assertNull(ColorVal.fromChar8('x'));
}
// Prove that all three reverse-mappings cannot be changed accidentally
// Notice that Paguro is the only one to give us a deprecation warning on this mutator
// method because it always throws an exception. It's not deprecated in the sense that the
// method is ever going away. But the warning indicates the coding error of ever trying to
// call this method.
@SuppressWarnings("deprecation")
@Test(expected = UnsupportedOperationException.class)
public void revMapExJ() { charMapP.put('Z', RED); }
// Traditional Java doesn't indicate that the put method always throws an exception.
@Test(expected = UnsupportedOperationException.class)
public void revMapExT() { charMapT.put('Z', RED); }
// Java8 doesn't indicate that the put method always throws an exception.
@SuppressWarnings("ConstantConditions")
@Test(expected = UnsupportedOperationException.class)
public void revMapEx8() { charMap8.put('Z', RED); }
// Maybe your code has to inter-operate with a legacy system that instead of letters, uses
// numbers to indicate the three primary colors:
@Test public void testExtension() {
// Paguro has methods that return new lightweight copies of the original map,
// with the new value added. This makes it a snap to extend existing immutable data
// structures: 3 loc.
final ImMap<Character,ColorVal> extendedMapP = charMapP.assoc('1', RED)
.assoc('2', GREEN)
.assoc('3', BLUE);
// To make the construction private, we declare a block in traditional Java. 7 loc.
final Map<Character,ColorVal> extendedMapT;
{
Map<Character, ColorVal> tempMap = new HashMap<>(charMapT);
tempMap.put('1', RED);
tempMap.put('2', GREEN);
tempMap.put('3', BLUE);
extendedMapT = Collections.unmodifiableMap(tempMap);
}
// In Java8 we can use a lambda to make construction private. 8 loc, including a cast.
// I had to look up the name of this functional class in the API docs because I thought it
// was Producer.
Map<Character, ColorVal> tempMap = new HashMap<>(charMap8);
tempMap.put('1', RED);
tempMap.put('2', GREEN);
tempMap.put('3', BLUE);
final Map<Character,ColorVal> extendedMap8 = Collections.unmodifiableMap(tempMap);
// All three methods produce the same correct results. The question is which code would
// you rather write to achieve these results?
assertEquals(RED, extendedMapP.get('R'));
assertEquals(RED, extendedMapP.get('1'));
assertEquals(RED, extendedMapT.get('R'));
assertEquals(RED, extendedMapT.get('1'));
assertEquals(RED, extendedMap8.get('R'));
assertEquals(RED, extendedMap8.get('1'));
assertEquals(GREEN, extendedMapP.get('G'));
assertEquals(GREEN, extendedMapP.get('2'));
assertEquals(GREEN, extendedMapT.get('G'));
assertEquals(GREEN, extendedMapT.get('2'));
assertEquals(GREEN, extendedMap8.get('G'));
assertEquals(GREEN, extendedMap8.get('2'));
assertEquals(BLUE, extendedMapP.get('B'));
assertEquals(BLUE, extendedMapP.get('3'));
assertEquals(BLUE, extendedMapT.get('B'));
assertEquals(BLUE, extendedMapT.get('3'));
assertEquals(BLUE, extendedMap8.get('B'));
assertEquals(BLUE, extendedMap8.get('3'));
assertNull(extendedMapP.get('x'));
assertNull(extendedMapP.get('4'));
assertNull(extendedMapT.get('x'));
assertNull(extendedMapT.get('4'));
assertNull(extendedMap8.get('x'));
assertNull(extendedMap8.get('4'));
}
// ====================================== Second Example =====================================
// java.awt.Color is outside of profile compact1, so I'm defining a simpler, but similar class
static class Color extends Tuple3<Integer,Integer,Integer> {
Color(int r, int g, int b) { super(r, g, b); }
// Yes, throwing an Exception is contrived here. Exceptions happen in real Java code, but
// usually for more complex reasons.
@SuppressWarnings("RedundantThrows")
public int green() throws Exception { return _2; }
}
// The Paguro way: 3 loc, + 3 loc = 6 loc
@Test
public void colorSquareP() {
ImList<Color> imgData = RangeOfInt.of(0, 256)
.flatMap(i -> RangeOfInt.of(0, 256).map(j -> new Color(i, (i + j) / 2, 255)))
.toImList();
assertTrue(imgData.toString()
.startsWith("PersistentVector(" +
"Color(0,0,255)," +
"Color(0,0,255)," +
"Color(0,1,255)," +
"Color(0,1,255)," +
"Color(0,2,255),"));
ImMap<Integer,Integer> greenCounts = imgData
.fold(map(),
(accum, color) -> accum.assoc(color.green(),
accum.getOrElse(color.green(), 0) + 1));
assertEquals(256, greenCounts.size());
assertEquals(Integer.valueOf(3), greenCounts.get(0));
assertEquals(Integer.valueOf(7), greenCounts.get(1));
assertEquals(Integer.valueOf(11), greenCounts.get(2));
assertEquals(Integer.valueOf(15), greenCounts.get(3));
}
// Same thing in "Traditional" Java: 4 loc + 2 brackets, 8 loc + 4 brackets =
// 12 loc + 6 brackets
// Note that colorSquareT() throws an Exception (because Color.green() throws it).
@Test public void colorSquareT() throws Exception {
java.util.List<Color> imgData = new ArrayList<>();
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
imgData.add(new Color(i, (i + j) / 2, 255));
}
}
assertTrue(imgData.toString()
.startsWith("[" +
"Color(0,0,255), " +
"Color(0,0,255), " +
"Color(0,1,255), " +
"Color(0,1,255), " +
"Color(0,2,255), "));
Map<Integer,Integer> greenCounts = new HashMap<>();
for (Color color : imgData) {
greenCounts.put(color.green(), greenCounts.getOrDefault(color.green(), 0) + 1);
}
assertEquals(256, greenCounts.size());
assertEquals(Integer.valueOf(3), greenCounts.get(0));
assertEquals(Integer.valueOf(7), greenCounts.get(1));
assertEquals(Integer.valueOf(11), greenCounts.get(2));
assertEquals(Integer.valueOf(15), greenCounts.get(3));
}
// Same thing with Java 8's Streams - I just used traditional Java to populate the list.
// (4 loc + 2 brackets) + (8 loc + 1 bracket) = 12 loc + 3 brackets
@Test public void colorSquare8() {
java.util.List<Color> imgData = new ArrayList<>();
for (int i = 0; i < 256; i++) {
for (int j = 0; j < 256; j++) {
imgData.add(new Color(i, (i + j) / 2, 255));
}
}
assertTrue(imgData.toString()
.startsWith("[" +
"Color(0,0,255), " +
"Color(0,0,255), " +
"Color(0,1,255), " +
"Color(0,1,255), " +
"Color(0,2,255), "));
// If this didn't throw an exception, we could use (thanks to @codepoetics on reddit)
// Map<Integer,Long> greenCounts = Collections.unmodifiableMap(
// imgData.stream()
// .collect(Collectors.groupingBy(color -> color.green(),
// Collectors.counting())));
// But green does throw an exception. There are various ways of handling this, but I think
// this is probably typical, and basically identical to what Paguro does for you.
Map<Integer,Long> greenCounts = Collections.unmodifiableMap(
imgData.stream()
.collect(Collectors.groupingBy(color -> {
try {
return color.green();
} catch (Exception e) {
throw new RuntimeException(e);
}
}, Collectors.counting())));
// Conclusion:
// Wrapping exceptions in lambdas is not the end of the world, but it's unnecessarily
// distracting. Also, why learn a whole Collectors library when you could fold into
// a map and get exception wrapping for free? Finally, what's returned here is just
// another Collections.unmodifiableMap - you have to make a deep copy in order to change it.
// If you really want to write code this way, at least use Paguro's
// FunctionUtils.unmodMap() instead so that your IDE and compiler can warn you if you try to
// call a deprecated method.
assertEquals(256, greenCounts.size());
assertEquals(Long.valueOf(3), greenCounts.get(0));
assertEquals(Long.valueOf(7), greenCounts.get(1));
assertEquals(Long.valueOf(11), greenCounts.get(2));
assertEquals(Long.valueOf(15), greenCounts.get(3));
}
}