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

pl.jalokim.propertiestojson.resolvers.primitives.string.TextToNumberResolver Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
package pl.jalokim.propertiestojson.resolvers.primitives.string;

import pl.jalokim.propertiestojson.resolvers.PrimitiveJsonTypesResolver;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Optional;

public class TextToNumberResolver implements TextToConcreteObjectResolver {

    @Override
    public Optional returnObjectWhenCanBeResolved(PrimitiveJsonTypesResolver primitiveJsonTypesResolver, String propertyValue, String propertyKey) {
        return Optional.ofNullable(convertToNumber(propertyValue));
    }

    public static Number convertToNumber(String propertyValue) {
        Number number = convertToNumberFromText(propertyValue);
        if (number!= null && number.toString().equals(propertyValue)) {
            return number;
        }
        return null;
    }

    private static Number convertToNumberFromText(String propertyValue) {

        try {
            return getIntegerNumber(propertyValue);
        } catch (NumberFormatException exc) {
        }
        try {
            return getDoubleNumber(propertyValue);
        } catch (NumberFormatException exc) {
        }
        return null;
    }


    private static BigInteger getIntegerNumber(String toParse) {
        return new BigInteger(toParse);
    }

    private static BigDecimal getDoubleNumber(String toParse) {
        return new BigDecimal(toParse);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy