Skip to content
This repository was archived by the owner on Oct 28, 2020. It is now read-only.

Commit 31eeeef

Browse files
author
Nicolai Parlog
committed
Experiment with ad-hoc fields
1 parent c62f01a commit 31eeeef

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package org.codefx.demo.java10.lang.var;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Map.Entry;
6+
import java.util.Optional;
7+
import java.util.stream.Stream;
8+
9+
import static java.util.Map.entry;
10+
11+
public class AdHocFields {
12+
13+
/*
14+
* DOWNSIDES
15+
*
16+
* - verbose
17+
* - combination of non-trivial language features (anonymous types & type inference)
18+
* - falls apart when extracting methods
19+
*
20+
* ALTERNATIVES
21+
*
22+
* - Map.Entry
23+
* - a library offering tuples
24+
* - wait for records / data classes
25+
*/
26+
27+
private static void process(User user, Map<User, SimpleAddress> addresses) {
28+
// with var it is possible to directly declare anonymous types
29+
// and refence their ad-hoc fields later
30+
var userWithAddress = new Object() {
31+
User _user = user;
32+
Optional<SimpleAddress> _address = Optional.ofNullable(addresses.get(user));
33+
};
34+
35+
// further work with `userwithAddress`
36+
String asString = userWithAddress._user + " at " + userWithAddress._address;
37+
System.out.println(asString);
38+
}
39+
40+
private static void processWithAnonymousType(List<User> users, Map<User, SimpleAddress> addresses) {
41+
// the stream pipeline works exactly the same in Java 8, but `userWithAddress`
42+
// would have to be `Optional<? extends Object>`, thus loosing the ad-hoc fields
43+
var userWithAddress = users.stream()
44+
.map(user -> new Object() {
45+
User _user = user;
46+
Optional<SimpleAddress> _address = Optional.ofNullable(addresses.get(user));
47+
})
48+
.filter(o -> o._address.isPresent())
49+
.filter(o -> isValid(o._address.get()))
50+
.map(ua -> new Object() {
51+
User _user = ua._user;
52+
FullAddress _address = canonicalize(ua._address.get());
53+
})
54+
.findAny();
55+
56+
// further work with `userwithAddress`
57+
String asString = userWithAddress
58+
.map(ua -> ua._user + " at " + ua._address)
59+
.orElse("NONE");
60+
System.out.println(asString);
61+
}
62+
63+
private static void processWithEntries(List<User> users, Map<User, SimpleAddress> addresses) {
64+
// with `Entry` it is no longer necessary to rely on `var` - it is also shorter
65+
Optional<Entry<User, FullAddress>> userWithAddress = users.stream()
66+
.map(user -> entry(user, Optional.ofNullable(addresses.get(user))))
67+
.filter(o -> o.getValue().isPresent())
68+
.filter(o -> isValid(o.getValue().get()))
69+
.map(ua -> entry(ua.getKey(), canonicalize(ua.getValue().get())))
70+
.findAny();
71+
72+
// further work with `userwithAddress`
73+
String asString = userWithAddress
74+
.map(ua -> ua.getKey() + " at " + ua.getValue())
75+
.orElse("NONE");
76+
System.out.println(asString);
77+
}
78+
79+
private static boolean isValid(SimpleAddress address) {
80+
// fancy validation of `address`
81+
return true;
82+
}
83+
84+
private static FullAddress canonicalize(SimpleAddress address) {
85+
// fancy processing of `address`
86+
return new FullAddress(address.address);
87+
}
88+
89+
private static class User {
90+
91+
private final String name;
92+
93+
public User(String name) {
94+
this.name = name;
95+
}
96+
97+
}
98+
99+
private static class SimpleAddress {
100+
101+
private final String address;
102+
103+
public SimpleAddress(String address) {
104+
this.address = address;
105+
}
106+
}
107+
108+
private static class FullAddress {
109+
110+
private final String address;
111+
112+
public FullAddress(String address) {
113+
this.address = address;
114+
}
115+
}
116+
117+
}

0 commit comments

Comments
 (0)