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

templates.java-micronaut.common.model.enum.mustache Maven / Gradle / Ivy

/**
 * {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{.}}}{{/description}}
{{#isDeprecated}}
 *
 * @deprecated{{#vendorExtensions.x-deprecated-message}} {{{.}}}{{/vendorExtensions.x-deprecated-message}}
{{/isDeprecated}}
 */
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
{{#withXml}}
@XmlType(name="{{datatypeWithEnum}}")
@XmlEnum({{vendorExtensions.baseType}}.class)
{{/withXml}}
{{#lombok}}
@RequiredArgsConstructor
@Getter(onMethod = @__(@JsonValue))
{{/lombok}}
{{#micronaut_serde_jackson}}
@Serdeable
{{/micronaut_serde_jackson}}
{{#generatedAnnotation}}
{{>common/generatedAnnotation}}
{{/generatedAnnotation}}
{{#additionalEnumTypeAnnotations}}
{{{.}}}
{{/additionalEnumTypeAnnotations}}
{{#formatSingleLine}}public enum {{>common/model/enumName}}{{/formatSingleLine}} {

    {{#allowableValues}}
        {{#enumVars}}
            {{#enumDescription}}
    /**
     * {{enumDescription}}
     */
            {{/enumDescription}}
            {{#withXml}}
    @XmlEnumValue({{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}}{{{value}}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isLong}}"{{/isLong}}{{#isFloat}}"{{/isFloat}})
            {{/withXml}}
            {{#deprecated}}
    @Deprecated
            {{/deprecated}}
    @JsonProperty({{{strValue}}})
    {{{name}}}({{{value}}}),{{#-last}}
    ;{{/-last}}
        {{/enumVars}}
    {{/allowableValues}}

    {{#formatSingleLine}}public static final Map<{{{dataType}}}, {{>common/model/enumName}}> VALUE_MAPPING = Map.copyOf(Arrays.stream(values()){{/formatSingleLine}}
        .collect(Collectors.toMap(v -> v.value{{#isString}}{{#useEnumCaseInsensitive}}.toLowerCase(){{/useEnumCaseInsensitive}}{{/isString}}, Function.identity())));

    private final {{{vendorExtensions.baseType}}} value;
{{^lombok}}

    {{#formatSingleLine}}{{>common/model/enumName}}{{/formatSingleLine}}({{{vendorExtensions.baseType}}} value) {
        this.value = value;
    }

    /**
     * @return The value represented by this enum
     */
    {{#jackson}}
    @JsonValue
    {{/jackson}}
    public {{{vendorExtensions.baseType}}} getValue() {
        return value;
    }
{{/lombok}}

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

    /**
     * Create this enum from a value.
     *
     * @param value The value
     *
     * @return The enum
     */
    {{#jackson}}
    @JsonCreator
    {{/jackson}}
    {{#formatSingleLine}}public static {{>common/model/enumName}} fromValue({{{dataType}}} value){{/formatSingleLine}} {{openbrace}}{{#formatNoEmptyLines}}
        {{#isString}}{{#useEnumCaseInsensitive}}
        var key = value.toLowerCase();
        {{/useEnumCaseInsensitive}}{{/isString}}
        {{^isNullable}}
            {{#isString}}
                {{#useEnumCaseInsensitive}}
        if (!VALUE_MAPPING.containsKey(key)) {
            throw new IllegalArgumentException("Unexpected value '" + key + "'");
        }
                {{/useEnumCaseInsensitive}}
                {{^useEnumCaseInsensitive}}
        if (!VALUE_MAPPING.containsKey(value)) {
            throw new IllegalArgumentException("Unexpected value '" + value + "'");
        }
                {{/useEnumCaseInsensitive}}
            {{/isString}}
            {{^isString}}
        if (!VALUE_MAPPING.containsKey(value)) {
            throw new IllegalArgumentException("Unexpected value '" + value + "'");
        }
            {{/isString}}
        {{/isNullable}}
        return VALUE_MAPPING.get({{#isString}}{{#useEnumCaseInsensitive}}key{{/useEnumCaseInsensitive}}{{^useEnumCaseInsensitive}}value{{/useEnumCaseInsensitive}}{{/isString}}{{^isString}}value{{/isString}});
{{/formatNoEmptyLines}}
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy