- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeSafe.java
More file actions
Latest commit
172 lines (149 loc) · 4.9 KB
/
Copy pathTypeSafe.java
File metadata and controls
172 lines (149 loc) · 4.9 KB
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
packageplayground;
importjava.text.SimpleDateFormat;
importjava.util.ArrayList;
importjava.util.Collection;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.HashSet;
importjava.util.Map;
importjava.util.UUID;
importjavax.annotation.CheckReturnValue;
importorg.eclipse.jdt.annotation.NonNull;
/**
* Description: Helps avoiding common Java typing pitfalls. <br>
* Keywords csv: typing, type safety, compile, compiler
*
* <p>
* NOTE: Use <code>Ctrl+Shift+G</code> on the pitfall method you want to find occurences (scroll down to your packages).
* <p>
* NOTE: Findbugs may also find some of these bugs.
*
* @author croussy
*/
@CheckReturnValue
publicfinalclassTypeSafe<T> {
privatefinalTelement;
privateTypeSafe(finalTelement) {
// NOTE: assert not null is used to avoid late NullPointerExceptions.
assertelement != null;
this.element = element;
}
/**
* NEVER USE THIS METHOD, use {@link #equalsTyped(Object)} instead (shows Object due to type erasure) !
*
* Overriding of equals for this class is meant to enforce the use of equalsTyped in case equals was used by mistake.
*/
@Override
@Deprecated
publicbooleanequals(finalObjectother) {
assertfalse;
// Always break
thrownewIllegalArgumentException("Use equalsTyped instead of equals !");
}
/**
* <p>
* <code>new TypeSafe<String>(first).equalsTyped(second)</code>
* <p>
* NOTE: for Strings you can use the simpler {@link #equalsString(String, String)} method.
* <p>
* NOTE: Cannot be named equals because of type erasure.
*/
@SuppressWarnings("null")
publicbooleanequalsTyped(final@NonNullTother) {
returnelement.equals(other);
}
@Override
publicinthashCode() {
// Should not be used.
assertfalse;
return -1;
}
publicstatic <T> TypeSafe<T> build(finalTelement) {
returnnewTypeSafe<>(element);
}
publicstatic <T> booleancollectionContains(finalCollection<T> collection, finalTelement) {
returncollection.contains(element);
}
publicstatic <T> booleancollectionContainsAll(finalCollection<T> collection, finalCollection<T> elements) {
assertelements != null;
returncollection.containsAll(elements);
}
publicstatic <T> booleancollectionIntersects(finalCollection<T> collection, finalCollection<T> elements) {
assertcollection != null;
assertelements != null;
// Make a copy to avoid side-effects.
finalHashSet<T> copy = newHashSet<T>(collection);
copy.retainAll(elements);
return !copy.isEmpty();
}
publicstatic <T> booleancollectionRemove(finalCollection<T> collection, finalTelement) {
returncollection.remove(element);
}
publicstatic <T> booleancollectionRemoveAll(finalCollection<T> collection, finalCollection<T> elements) {
assertelements != null;
returncollection.removeAll(elements);
}
publicstatic <T> booleancollectionRetainAll(finalCollection<T> collection, finalCollection<T> elements) {
assertelements != null;
returncollection.retainAll(elements);
}
/**
* For types different from String use {@link #equalsTyped(String, String)}.
*/
publicstaticbooleanequalsString(finalStrings1, finalStrings2) {
returns1.equals(s2);
}
/**
* Type-safe equals check for UUID for convenience.
*/
publicstaticbooleanequalsUuid(finalUUIDu1, finalUUIDu2) {
returnu1.equals(u2);
}
publicstatic <K, V> booleanmapContainsKey(finalMap<K, V> map, finalKkey) {
assertkey != null;
returnmap.containsKey(key);
}
publicstatic <K, V> VmapGet(finalMap<K, V> map, finalKkey) {
assertkey != null;
returnmap.get(key);
}
publicstatic <K, V> VmapGetExpectValueNotNull(finalMap<K, V> map, finalKkey) {
assertkey != null;
finalVvalue = map.get(key);
assertvalue != null;
returnvalue;
}
publicstatic <K, V> VmapRemove(finalMap<K, V> map, finalKkey) {
assertkey != null;
returnmap.remove(key);
}
publicstaticStringsimpleDateFormatDoFormat(finalSimpleDateFormatsdf, finalDatedate) {
assertsdf != null;
assertdate != null;
// Incredible, but this can take and Object, thus needs to be protected.
returnsdf.format(date);
}
// Some examples:
publicstaticvoidmain(finalString[] args) {
{
finalStringbob = "Bob";
finalStringtom = "Tom";
bob.equals(tom);
// Equals breaks typing by accepting an Integer !
// bob.equals(1);
TypeSafe.build(bob).equalsTyped(tom);
// TypeSafe.build(bob).equalsTyped(1);
// TypeSafe.build(bob).equals(1);
TypeSafe.equalsString(bob, tom);
}
{
finalCollection<String> collection = newArrayList<>();
TypeSafe.collectionContains(collection, "bob");
}
{
finalMap<String, Integer> map = newHashMap<>();
TypeSafe.mapGet(map, "");
TypeSafe.mapContainsKey(map, "");
}
}
}