All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Java.libraries.microprofile.enumClass.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{#withXml}}
@XmlType(name="{{datatypeWithEnum}}")
@XmlEnum({{dataType}}.class)
{{/withXml}}
{{^withXml}}
  @JsonbTypeSerializer({{datatypeWithEnum}}.Serializer.class)
  @JsonbTypeDeserializer({{datatypeWithEnum}}.Deserializer.class)
{{/withXml}}
  {{>additionalEnumTypeAnnotations}}public enum {{datatypeWithEnum}} {

    {{#allowableValues}}
    {{#withXml}}
    {{#enumVars}}@XmlEnumValue({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}) {{name}}({{dataType}}.valueOf({{{value}}})){{^-last}}, {{/-last}}{{#-last}};{{/-last}}{{/enumVars}}
    {{/withXml}}
    {{^withXml}}
    {{#enumVars}}{{name}}({{dataType}}.valueOf({{{value}}})){{^-last}}, {{/-last}}{{#-last}};{{/-last}}{{/enumVars}}
    {{/withXml}}
    {{/allowableValues}}


    {{dataType}} value;

    {{datatypeWithEnum}} ({{dataType}} v) {
        value = v;
    }

    public {{dataType}} value() {
        return value;
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }

    {{#withXml}}
    public static {{datatypeWithEnum}} fromValue(String v) {
        for ({{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
            if (String.valueOf(b.value).equals(v)) {
                return b;
            }
        }
        {{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + v + "'");{{/useNullForUnknownEnumValue}}
    }
    {{/withXml}}
    {{^withXml}}
    public static final class Deserializer implements JsonbDeserializer<{{datatypeWithEnum}}> {
        @Override
        public {{datatypeWithEnum}} deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) {
            for ({{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
                if (String.valueOf(b.value).equals(parser.getString())) {
                    return b;
                }
            }
            {{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + parser.getString() + "'");{{/useNullForUnknownEnumValue}}
        }
    }

    public static final class Serializer implements JsonbSerializer<{{datatypeWithEnum}}> {
        @Override
        public void serialize({{datatypeWithEnum}} obj, JsonGenerator generator, SerializationContext ctx) {
            generator.write(obj.value);
        }
    }
    {{/withXml}}
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy