1
1
package org .inferred .freebuilder .processor ;
2
2
3
3
import static com .google .common .base .Preconditions .checkState ;
4
+ import static java .util .stream .Collectors .toList ;
4
5
5
6
import com .google .common .collect .ImmutableList ;
6
7
import com .google .common .collect .Ordering ;
7
8
9
+ import org .inferred .freebuilder .processor .testtype .AbstractNonComparable ;
10
+ import org .inferred .freebuilder .processor .testtype .NonComparable ;
11
+ import org .inferred .freebuilder .processor .testtype .OtherNonComparable ;
8
12
import org .inferred .freebuilder .processor .util .NameImpl ;
9
13
14
+ import java .util .Collection ;
15
+ import java .util .Comparator ;
16
+ import java .util .List ;
17
+ import java .util .Optional ;
10
18
import java .util .stream .Collectors ;
11
19
import java .util .stream .IntStream ;
12
20
21
+ import javax .annotation .Nullable ;
22
+
13
23
public enum ElementFactory {
14
24
STRINGS (
25
+ String .class ,
15
26
"String" ,
16
- "String" ,
27
+ null ,
17
28
CharSequence .class ,
18
29
"!element.isEmpty()" ,
19
30
"!element.toString().isEmpty()" ,
@@ -28,8 +39,9 @@ public enum ElementFactory {
28
39
"foxtrot" ,
29
40
"golf" ),
30
41
INTEGERS (
31
- " Integer" ,
42
+ Integer . class ,
32
43
"int" ,
44
+ null ,
33
45
Number .class ,
34
46
"element >= 0" ,
35
47
"element.intValue() >= 0" ,
@@ -42,49 +54,72 @@ public enum ElementFactory {
42
54
10 ,
43
55
15 ,
44
56
21 ,
45
- 28 );
46
-
47
- private final String type ;
57
+ 28 ),
58
+ NON_COMPARABLES (
59
+ NonComparable .class ,
60
+ NonComparable .class .getName (),
61
+ AbstractNonComparable .ReverseIdComparator .class ,
62
+ AbstractNonComparable .class ,
63
+ "element.id() >= 0" ,
64
+ "element.id() >= 0" ,
65
+ "ID must be non-negative" ,
66
+ new NonComparable (-2 , "broken" ),
67
+ new OtherNonComparable (88 , "other" ),
68
+ new NonComparable (10 , "alpha" ),
69
+ new NonComparable (9 , "cappa" ),
70
+ new NonComparable (8 , "echo" ),
71
+ new NonComparable (7 , "golf" ),
72
+ new NonComparable (6 , "beta" ),
73
+ new NonComparable (5 , "delta" ),
74
+ new NonComparable (4 , "foxtrot" ));
75
+
76
+ private final Class <?> type ;
48
77
private final String unwrappedType ;
78
+ @ Nullable private final Class <?> comparator ;
49
79
private Class <?> supertype ;
50
80
private final String validation ;
51
81
private final String supertypeValidation ;
52
82
private final String errorMessage ;
53
- private final Comparable <?> invalidExample ;
83
+ private final Object invalidExample ;
54
84
private final Object supertypeExample ;
55
- private final ImmutableList <Comparable <?> > examples ;
85
+ private final ImmutableList <Object > examples ;
56
86
57
87
ElementFactory (
58
- String type ,
88
+ Class <?> type ,
59
89
String unwrappedType ,
90
+ @ Nullable Class <? extends Comparator <?>> comparator ,
60
91
Class <?> supertype ,
61
92
String validation ,
62
93
String supertypeValidation ,
63
94
String errorMessage ,
64
- Comparable <?> invalidExample ,
95
+ Object invalidExample ,
65
96
Object supertypeExample ,
66
- Comparable <?> ... examples ) {
97
+ Object ... examples ) {
67
98
this .type = type ;
68
99
this .unwrappedType = unwrappedType ;
100
+ this .comparator = comparator ;
69
101
this .supertype = supertype ;
70
102
this .validation = validation ;
71
103
this .supertypeValidation = supertypeValidation ;
72
104
this .errorMessage = errorMessage ;
73
105
this .invalidExample = invalidExample ;
74
106
this .supertypeExample = supertypeExample ;
75
107
this .examples = ImmutableList .copyOf (examples );
76
- checkState (Ordering .natural ().isOrdered (this .examples ),
77
- "Examples must be in natural order (got %s)" , this .examples );
108
+ checkExamplesOrdered (comparator , this .examples );
78
109
}
79
110
80
- public String type () {
111
+ public Class <?> type () {
81
112
return type ;
82
113
}
83
114
84
115
public String unwrappedType () {
85
116
return unwrappedType ;
86
117
}
87
118
119
+ public Optional <Class <?>> comparator () {
120
+ return Optional .ofNullable (comparator );
121
+ }
122
+
88
123
public Class <?> supertype () {
89
124
return supertype ;
90
125
}
@@ -121,18 +156,47 @@ public String examples(int... ids) {
121
156
return IntStream .of (ids ).mapToObj (this ::example ).collect (Collectors .joining (", " ));
122
157
}
123
158
159
+ @ Override
160
+ public String toString () {
161
+ return type .getSimpleName ();
162
+ }
163
+
124
164
private static String toSource (Object example ) {
125
165
if (example instanceof String ) {
126
166
return "\" " + example + "\" " ;
127
167
} else if (example instanceof NameImpl ) {
128
168
return "new " + NameImpl .class .getName () + "(\" " + example + "\" )" ;
169
+ } else if (example instanceof NonComparable ) {
170
+ NonComparable nonComparable = (NonComparable ) example ;
171
+ return "new " + NonComparable .class .getName () + "(" + nonComparable .id () + ", \" "
172
+ + nonComparable .name () + "\" )" ;
173
+ } else if (example instanceof OtherNonComparable ) {
174
+ OtherNonComparable otherNonComparable = (OtherNonComparable ) example ;
175
+ return "new " + OtherNonComparable .class .getName () + "(" + otherNonComparable .id () + ", \" "
176
+ + otherNonComparable .name () + "\" )" ;
129
177
} else {
130
178
return example .toString ();
131
179
}
132
180
}
133
181
134
- @ Override
135
- public String toString () {
136
- return type ;
182
+ private static void checkExamplesOrdered (
183
+ @ Nullable Class <? extends Comparator <?>> comparator ,
184
+ Collection <?> examples ) {
185
+ if (comparator == null ) {
186
+ List <Comparable <?>> comparables =
187
+ examples .stream ().map (v -> (Comparable <?>) v ).collect (toList ());
188
+ checkState (Ordering .natural ().isOrdered (comparables ),
189
+ "Examples must be in natural order (got %s)" , examples );
190
+ } else {
191
+ try {
192
+ @ SuppressWarnings ("rawtypes" )
193
+ Ordering ordering = Ordering .from (comparator .newInstance ());
194
+ @ SuppressWarnings ("unchecked" )
195
+ boolean isOrdered = ordering .isOrdered (examples );
196
+ checkState (isOrdered );
197
+ } catch (ReflectiveOperationException e ) {
198
+ throw new RuntimeException (e );
199
+ }
200
+ }
137
201
}
138
202
}
0 commit comments