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

se.fortnox.reactivewizard.jaxrs.params.deserializing.NumberDeserializer Maven / Gradle / Ivy

There is a newer version: 24.6.0
Show newest version
package se.fortnox.reactivewizard.jaxrs.params.deserializing;

import java.util.function.Function;

/**
 * Generic number deserializer.
 */
public abstract class NumberDeserializer implements Deserializer {

    protected String              errorCode;
    private   Function numberParser;

    public NumberDeserializer(Function numberParser, String errorCode) {
        this.numberParser = numberParser;
        this.errorCode = errorCode;
    }

    @Override
    public T deserialize(String value) throws DeserializerException {
        if (value == null) {
            return getNullValue();
        }
        try {
            return numberParser.apply(value);
        } catch (NumberFormatException e) {
            throw new DeserializerException(errorCode);
        }
    }

    protected T getNullValue() throws DeserializerException {
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy