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}}
  {{#jsonb}}
  @JsonbTypeSerializer({{datatypeWithEnum}}.Serializer.class)
  @JsonbTypeDeserializer({{datatypeWithEnum}}.Deserializer.class)
  {{/jsonb}}
{{/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;
    }

    {{#jackson}}
    @JsonValue
    {{/jackson}}
    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}}
    {{#jsonb}}
    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);
        }
    }
    {{/jsonb}}
    {{#jackson}}
    @JsonCreator
        public static {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue({{{dataType}}} value) {
            for ({{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) {
                if (b.value.equals(value)) {
                    return b;
                }
            }
            {{#isNullable}}return null;{{/isNullable}}{{^isNullable}}throw new IllegalArgumentException("Unexpected value '" + value + "'");{{/isNullable}}
        }
    {{/jackson}}
    {{/withXml}}
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy