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;
}
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);
}
}
}
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;
}
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);
}
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;
}
Aggregations