-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathoneof_interface.mustache
More file actions
87 lines (80 loc) · 2.67 KB
/
oneof_interface.mustache
File metadata and controls
87 lines (80 loc) · 2.67 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
{{>additionalOneOfTypeAnnotations}}
{{#withXml}}
{{>xmlAnnotation}}
{{/withXml}}
{{#discriminator}}
{{>typeInfoAnnotation}}
{{/discriminator}}
{{>generatedAnnotation}}
{{#vendorExtensions.x-extensible-enum}}
@JsonDeserialize(using = {{classname}}.EnumDeserializer.class)
{{/vendorExtensions.x-extensible-enum}}
public {{>sealed}}interface {{classname}}{{#vendorExtensions.x-implements}}{{#-first}} extends {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} {{>permits}}{
{{#discriminator}}
public {{propertyType}} {{propertyGetter}}();
{{/discriminator}}
{{^discriminator}}
{{#vendorExtensions.x-extensible-enum}}
{{#useBeanValidation}}@Valid{{/useBeanValidation}}
@JsonValue
String getValue();
static {{classname}} fromValue(String value) {
{{#interfaceModels}}
try {
return {{classname}}.fromValue(value);
} catch (IllegalArgumentException e) {
// continue
}
{{/interfaceModels}}
{{#useString}}
return new {{stringClassName}}(value);
{{/useString}}
{{^useString}}
throw new IllegalArgumentException(value + " not supported in classes " + Arrays.asList({{#interfaceModels}}"{{classname}}"{{^-last}}, {{/-last}}{{/interfaceModels}}));
{{/useString}}
}
// custom jackson deserializer
class EnumDeserializer extends StdDeserializer<{{classname}}> {
public EnumDeserializer() {
super({{classname}}.class);
}
@Override
public {{classname}} deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String value = p.readValueAs(String.class);
return {{classname}}.fromValue(value);
}
}
{{#useString}}
{{#property.description}}
/**
* {{{.}}}
*/
{{/property.description}}
class {{stringClassName}} implements {{classname}} {
private final String value;
public {{stringClassName}}(String value) {
this.value = value;
}
{{#useBeanValidation}}@NotNull {{#property}}{{>beanValidationCore}} {{/property}}{{/useBeanValidation}}
@Override
public String getValue() {
return value;
}
@Override
public String toString() {
return "{{stringClassName}}:" + value;
}
@Override
public final boolean equals(Object o) {
if (!(o instanceof {{stringClassName}} )) return false;
return value.equals((({{stringClassName}})o).value);
}
@Override
public int hashCode() {
return value.hashCode();
}
}
{{/useString}}
{{/vendorExtensions.x-extensible-enum}}
{{/discriminator}}
}