-
Notifications
You must be signed in to change notification settings - Fork 42
/
EnumExample.java
264 lines (221 loc) · 8.29 KB
/
EnumExample.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
package com.palantir.product;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.errorprone.annotations.Immutable;
import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.Safe;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.processing.Generated;
/**
* This enumerates the numbers 1:2 also 100.
*
* <p>This class is used instead of a native enum to support unknown values. Rather than throw an exception, the
* {@link EnumExample#valueOf} method defaults to a new instantiation of {@link EnumExample} where
* {@link EnumExample#get} will return {@link EnumExample.Value#UNKNOWN}.
*
* <p>For example, {@code EnumExample.valueOf("corrupted value").get()} will return {@link EnumExample.Value#UNKNOWN},
* but {@link EnumExample#toString} will return "corrupted value".
*
* <p>There is no method to access all instantiations of this class, since they cannot be known at compile time.
*/
@Generated("com.palantir.conjure.java.types.EnumGenerator")
@Safe
@Immutable
public final class EnumExample {
public static final EnumExample ONE = new EnumExample(Value.ONE, "ONE");
/** @deprecated Prefer <code>ONE</code> where possible. */
@Deprecated
public static final EnumExample TWO = new EnumExample(Value.TWO, "TWO");
/**
* Value of 100.
*
* @deprecated One is easier to manage.
*/
@Deprecated
public static final EnumExample ONE_HUNDRED = new EnumExample(Value.ONE_HUNDRED, "ONE_HUNDRED");
@SuppressWarnings("deprecation")
private static final List<EnumExample> values = Collections.unmodifiableList(Arrays.asList(ONE, TWO, ONE_HUNDRED));
private final Value value;
private final String string;
private EnumExample(Value value, String string) {
this.value = value;
this.string = string;
}
public Value get() {
return this.value;
}
@Override
@JsonValue
public String toString() {
return this.string;
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other)
|| (this.value == Value.UNKNOWN
&& other instanceof EnumExample
&& this.string.equals(((EnumExample) other).string));
}
@Override
public int hashCode() {
return this.string.hashCode();
}
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
@SuppressWarnings("deprecation")
public static EnumExample valueOf(@Nonnull @Safe String value) {
Preconditions.checkNotNull(value, "value cannot be null");
String upperCasedValue = value.toUpperCase(Locale.ROOT);
switch (upperCasedValue) {
case "ONE":
return ONE;
case "TWO":
return TWO;
case "ONE_HUNDRED":
return ONE_HUNDRED;
default:
return new EnumExample(Value.UNKNOWN, upperCasedValue);
}
}
@SuppressWarnings("deprecation")
public <T> T accept(Visitor<T> visitor) {
switch (value) {
case ONE:
return visitor.visitOne();
case TWO:
return visitor.visitTwo();
case ONE_HUNDRED:
return visitor.visitOneHundred();
default:
return visitor.visitUnknown(string);
}
}
public static List<EnumExample> values() {
return values;
}
@Generated("com.palantir.conjure.java.types.EnumGenerator")
public enum Value {
ONE,
/** @deprecated Prefer <code>ONE</code> where possible. */
@Deprecated
TWO,
/**
* Value of 100.
*
* @deprecated One is easier to manage.
*/
@Deprecated
ONE_HUNDRED,
UNKNOWN
}
@Generated("com.palantir.conjure.java.types.EnumGenerator")
public interface Visitor<T> {
T visitOne();
/** @deprecated Prefer <code>ONE</code> where possible. */
@Deprecated
T visitTwo();
/**
* Value of 100.
*
* @deprecated One is easier to manage.
*/
@Deprecated
T visitOneHundred();
T visitUnknown(String unknownValue);
static <T> OneStageVisitorBuilder<T> builder() {
return new VisitorBuilder<T>();
}
}
private static final class VisitorBuilder<T>
implements OneStageVisitorBuilder<T>,
TwoStageVisitorBuilder<T>,
OneHundredStageVisitorBuilder<T>,
UnknownStageVisitorBuilder<T>,
Completed_StageVisitorBuilder<T> {
private Supplier<T> oneVisitor;
private Supplier<T> twoVisitor;
private Supplier<T> oneHundredVisitor;
private Function<@Safe String, T> unknownVisitor;
@Override
public TwoStageVisitorBuilder<T> visitOne(@Nonnull Supplier<T> oneVisitor) {
Preconditions.checkNotNull(oneVisitor, "oneVisitor cannot be null");
this.oneVisitor = oneVisitor;
return this;
}
@Override
public OneHundredStageVisitorBuilder<T> visitTwo(@Nonnull Supplier<T> twoVisitor) {
Preconditions.checkNotNull(twoVisitor, "twoVisitor cannot be null");
this.twoVisitor = twoVisitor;
return this;
}
@Override
public UnknownStageVisitorBuilder<T> visitOneHundred(@Nonnull Supplier<T> oneHundredVisitor) {
Preconditions.checkNotNull(oneHundredVisitor, "oneHundredVisitor cannot be null");
this.oneHundredVisitor = oneHundredVisitor;
return this;
}
@Override
public Completed_StageVisitorBuilder<T> visitUnknown(@Nonnull Function<@Safe String, T> unknownVisitor) {
Preconditions.checkNotNull(unknownVisitor, "unknownVisitor cannot be null");
this.unknownVisitor = unknownType -> unknownVisitor.apply(unknownType);
return this;
}
@Override
public Completed_StageVisitorBuilder<T> throwOnUnknown() {
this.unknownVisitor = unknownType -> {
throw new SafeIllegalArgumentException(
"Unknown variant of the 'EnumExample' union", SafeArg.of("unknownType", unknownType));
};
return this;
}
@Override
public Visitor<T> build() {
final Supplier<T> oneVisitor = this.oneVisitor;
final Supplier<T> twoVisitor = this.twoVisitor;
final Supplier<T> oneHundredVisitor = this.oneHundredVisitor;
final Function<@Safe String, T> unknownVisitor = this.unknownVisitor;
return new Visitor<T>() {
@Override
public T visitOne() {
return oneVisitor.get();
}
@Override
public T visitTwo() {
return twoVisitor.get();
}
@Override
public T visitOneHundred() {
return oneHundredVisitor.get();
}
@Override
public T visitUnknown(String unknownType) {
return unknownVisitor.apply(unknownType);
}
};
}
}
public interface OneStageVisitorBuilder<T> {
TwoStageVisitorBuilder<T> visitOne(@Nonnull Supplier<T> oneVisitor);
}
public interface TwoStageVisitorBuilder<T> {
OneHundredStageVisitorBuilder<T> visitTwo(@Nonnull Supplier<T> twoVisitor);
}
public interface OneHundredStageVisitorBuilder<T> {
UnknownStageVisitorBuilder<T> visitOneHundred(@Nonnull Supplier<T> oneHundredVisitor);
}
public interface UnknownStageVisitorBuilder<T> {
Completed_StageVisitorBuilder<T> visitUnknown(@Nonnull Function<@Safe String, T> unknownVisitor);
Completed_StageVisitorBuilder<T> throwOnUnknown();
}
public interface Completed_StageVisitorBuilder<T> {
Visitor<T> build();
}
}