Search in sources :

Example 1 with AtlasConversionInfo

use of io.atlasmap.spi.AtlasConversionInfo in project atlasmap by atlasmap.

the class StringConverter method toInteger.

@AtlasConversionInfo(sourceType = FieldType.STRING, targetType = FieldType.INTEGER, concerns = { AtlasConversionConcern.FORMAT, AtlasConversionConcern.RANGE })
public Integer toInteger(String value) throws AtlasConversionException {
    if (value == null) {
        return null;
    }
    BigDecimal bd = null;
    Integer i = null;
    try {
        i = Integer.parseInt(value);
    } catch (NumberFormatException nfe) {
        try {
            bd = new BigDecimal(value);
            i = bd.intValue();
        } catch (NumberFormatException nfe2) {
            throw new AtlasConversionException(nfe);
        }
    }
    if (bd != null && bd.compareTo(BigDecimal.valueOf(i)) != 0) {
        throw new AtlasConversionException(String.format("String %s is greater than Integer.MAX_VALUE  or less than Integer.MIN_VALUE", value));
    }
    return i;
}
Also used : BigInteger(java.math.BigInteger) AtlasConversionException(io.atlasmap.api.AtlasConversionException) BigDecimal(java.math.BigDecimal) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo)

Example 2 with AtlasConversionInfo

use of io.atlasmap.spi.AtlasConversionInfo in project atlasmap by atlasmap.

the class JavaValidationService method validateClassConversion.

private void validateClassConversion(String mappingId, JavaField inputField, JavaField outField, List<Validation> validations) {
    Optional<AtlasConverter<?>> atlasConverter = getConversionService().findMatchingConverter(inputField.getClassName(), outField.getClassName());
    if (!atlasConverter.isPresent()) {
        Validation validation = new Validation();
        validation.setScope(ValidationScope.MAPPING);
        validation.setId(mappingId);
        validation.setMessage(String.format("Conversion from '%s' to '%s' is required but no converter is available", inputField.getClassName(), outField.getClassName()));
        validation.setStatus(ValidationStatus.WARN);
        validations.add(validation);
    } else {
        AtlasConversionInfo conversionInfo;
        // find the method that does the conversion
        Method[] methods = atlasConverter.get().getClass().getMethods();
        conversionInfo = Arrays.stream(methods).map(method -> method.getAnnotation(AtlasConversionInfo.class)).filter(atlasConversionInfo -> atlasConversionInfo != null).filter(atlasConversionInfo -> atlasConversionInfo.sourceType().compareTo(inputField.getFieldType()) == 0 && atlasConversionInfo.targetType().compareTo(outField.getFieldType()) == 0).findFirst().orElse(null);
        if (conversionInfo != null) {
            populateConversionConcerns(mappingId, conversionInfo, getFieldName(inputField), getFieldName(outField), validations);
        }
    }
}
Also used : Validation(io.atlasmap.v2.Validation) DataInputStream(java.io.DataInputStream) Arrays(java.util.Arrays) FieldDirection(io.atlasmap.spi.FieldDirection) ValidationScope(io.atlasmap.v2.ValidationScope) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) AtlasValidator(io.atlasmap.spi.AtlasValidator) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) BaseModuleValidationService(io.atlasmap.core.BaseModuleValidationService) NonNullValidator(io.atlasmap.validators.NonNullValidator) FieldType(io.atlasmap.v2.FieldType) Validation(io.atlasmap.v2.Validation) Field(io.atlasmap.v2.Field) Map(java.util.Map) AtlasConversionService(io.atlasmap.api.AtlasConversionService) Method(java.lang.reflect.Method) AtlasConverter(io.atlasmap.api.AtlasConverter) Logger(org.slf4j.Logger) IOException(java.io.IOException) ValidationStatus(io.atlasmap.v2.ValidationStatus) List(java.util.List) AtlasModuleDetail(io.atlasmap.spi.AtlasModuleDetail) JavaField(io.atlasmap.java.v2.JavaField) Optional(java.util.Optional) InputStream(java.io.InputStream) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) AtlasConverter(io.atlasmap.api.AtlasConverter) Method(java.lang.reflect.Method)

Example 3 with AtlasConversionInfo

use of io.atlasmap.spi.AtlasConversionInfo in project atlasmap by atlasmap.

the class CharSequenceConverter method toLong.

/**
 * Converts to {@link Long}.
 * @param value value
 * @return converted
 * @throws AtlasConversionException out of range or not a number
 */
@AtlasConversionInfo(sourceType = FieldType.STRING, targetType = FieldType.LONG, concerns = { AtlasConversionConcern.FORMAT, AtlasConversionConcern.RANGE, AtlasConversionConcern.FRACTIONAL_PART })
public Long toLong(CharSequence value) throws AtlasConversionException {
    if (value == null) {
        return null;
    }
    String str = value.toString();
    Long l = null;
    try {
        l = Long.parseLong(str);
    } catch (NumberFormatException nfe) {
        try {
            BigDecimal bd = new BigDecimal(str);
            if (bd.compareTo(new BigDecimal(Long.MIN_VALUE)) < 0 || bd.compareTo(new BigDecimal(Long.MAX_VALUE)) > 0) {
                throw new AtlasConversionException(String.format("String %s is greater than Long.MAX_VALUE  or less than Long.MIN_VALUE", value));
            }
            l = bd.longValue();
        } catch (NumberFormatException nfe2) {
            throw new AtlasConversionException(nfe);
        }
    }
    return l;
}
Also used : AtlasConversionException(io.atlasmap.api.AtlasConversionException) BigDecimal(java.math.BigDecimal) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo)

Example 4 with AtlasConversionInfo

use of io.atlasmap.spi.AtlasConversionInfo in project atlasmap by atlasmap.

the class CharSequenceConverter method toFloat.

/**
 * Converts to {@link Float}.
 * @param value value
 * @return converted
 * @throws AtlasConversionException out of range or not a number
 */
@AtlasConversionInfo(sourceType = FieldType.STRING, targetType = FieldType.FLOAT, concerns = { AtlasConversionConcern.FORMAT, AtlasConversionConcern.RANGE })
public Float toFloat(CharSequence value) throws AtlasConversionException {
    if (value == null) {
        return null;
    }
    String str = value.toString();
    float parsedFloat = 0.0f;
    try {
        parsedFloat = Float.parseFloat(str);
    } catch (NumberFormatException nfe) {
        throw new AtlasConversionException(nfe);
    }
    float absParsedFloat = Math.abs(parsedFloat);
    if (absParsedFloat == 0.0f) {
        return parsedFloat;
    }
    if (absParsedFloat < Float.MIN_VALUE || absParsedFloat > Float.MAX_VALUE) {
        throw new AtlasConversionException(String.format("String %s is greater than Float.MAX_VALUE  or less than Float.MIN_VALUE", str));
    }
    return Float.valueOf(str);
}
Also used : AtlasConversionException(io.atlasmap.api.AtlasConversionException) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo)

Example 5 with AtlasConversionInfo

use of io.atlasmap.spi.AtlasConversionInfo in project atlasmap by atlasmap.

the class CharSequenceConverter method toInteger.

/**
 * Converts to {@link Integer}.
 * @param value value
 * @return converted
 * @throws AtlasConversionException out of range or not a number
 */
@AtlasConversionInfo(sourceType = FieldType.STRING, targetType = FieldType.INTEGER, concerns = { AtlasConversionConcern.FORMAT, AtlasConversionConcern.RANGE, AtlasConversionConcern.FRACTIONAL_PART })
public Integer toInteger(CharSequence value) throws AtlasConversionException {
    if (value == null) {
        return null;
    }
    String str = value.toString();
    Integer i = null;
    try {
        i = Integer.parseInt(str);
    } catch (NumberFormatException nfe) {
        try {
            BigDecimal bd = new BigDecimal(str);
            if (bd.compareTo(new BigDecimal(Integer.MIN_VALUE)) < 0 || bd.compareTo(new BigDecimal(Integer.MAX_VALUE)) > 0) {
                throw new AtlasConversionException(String.format("String %s is greater than Integer.MAX_VALUE  or less than Integer.MIN_VALUE", str));
            }
            i = bd.intValue();
        } catch (NumberFormatException nfe2) {
            throw new AtlasConversionException(nfe);
        }
    }
    return i;
}
Also used : BigInteger(java.math.BigInteger) AtlasConversionException(io.atlasmap.api.AtlasConversionException) BigDecimal(java.math.BigDecimal) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo)

Aggregations

AtlasConversionInfo (io.atlasmap.spi.AtlasConversionInfo)22 Method (java.lang.reflect.Method)14 AtlasConversionConcern (io.atlasmap.spi.AtlasConversionConcern)12 Annotation (java.lang.annotation.Annotation)10 Test (org.junit.jupiter.api.Test)9 AtlasConversionException (io.atlasmap.api.AtlasConversionException)7 BigDecimal (java.math.BigDecimal)5 Field (io.atlasmap.v2.Field)4 FieldType (io.atlasmap.v2.FieldType)4 Validation (io.atlasmap.v2.Validation)4 ValidationScope (io.atlasmap.v2.ValidationScope)4 ValidationStatus (io.atlasmap.v2.ValidationStatus)4 Arrays (java.util.Arrays)4 List (java.util.List)4 Optional (java.util.Optional)4 AtlasConversionService (io.atlasmap.api.AtlasConversionService)2 AtlasConverter (io.atlasmap.api.AtlasConverter)2 JavaField (io.atlasmap.java.v2.JavaField)2 AtlasConverter (io.atlasmap.spi.AtlasConverter)2 AtlasModuleDetail (io.atlasmap.spi.AtlasModuleDetail)2