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

com.nhl.link.rest.parser.converter.EnumConverter Maven / Gradle / Ivy

package com.nhl.link.rest.parser.converter;

import com.fasterxml.jackson.databind.JsonNode;
import com.nhl.link.rest.LinkRestException;

import javax.ws.rs.core.Response;

/**
 * @since 2.10
 */
public class EnumConverter> extends AbstractConverter {

    private Class enumType;

    public EnumConverter(Class enumType) {
        this.enumType = enumType;
    }

    @Override
    protected T valueNonNull(JsonNode node) {
        String value = node.asText();
        return value == null || value.length() == 0 ? null : fromString(node.asText());
    }

    protected T fromString(String string) {
        try {
            return Enum.valueOf(enumType, string);
        } catch (IllegalArgumentException e) {
            throw new LinkRestException(Response.Status.BAD_REQUEST,
                    "Invalid enum value: " + string);
        }
    }

    public Class getEnumType() {
        return enumType;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy