Skip to content

Commit b5ad150

Browse files
authored
Merge pull request #81 from fingerprintjs/fix-enums-generation
Fix generation of enum classes
2 parents 3d14a98 + 423e922 commit b5ad150

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import com.fasterxml.jackson.annotation.JsonCreator;
2+
import com.fasterxml.jackson.annotation.JsonValue;
3+
4+
/**
5+
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}}
6+
*/
7+
public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} {
8+
{{#allowableValues}}{{#enumVars}}
9+
{{{name}}}({{{value}}}){{^-last}},
10+
{{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}}
11+
12+
private {{{dataType}}} value;
13+
14+
{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}({{{dataType}}} value) {
15+
this.value = value;
16+
}
17+
18+
@JsonValue
19+
public {{{dataType}}} getValue() {
20+
return value;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return String.valueOf(value);
26+
}
27+
28+
@JsonCreator
29+
public static {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue({{{dataType}}} value) {
30+
for ({{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
31+
if (b.value.equals(value)) {
32+
return b;
33+
}
34+
}
35+
{{^errorOnUnknownEnum}}return null;{{/errorOnUnknownEnum}}{{#errorOnUnknownEnum}}throw new IllegalArgumentException("Unexpected value '" + value + "' for '{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}' enum.");{{/errorOnUnknownEnum}}
36+
}
37+
}

0 commit comments

Comments
 (0)