io.atlasmap.converters.FloatConverter Maven / Gradle / Ivy
/**
* Copyright (C) 2017 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.atlasmap.converters;
import io.atlasmap.api.AtlasConversionException;
import io.atlasmap.api.AtlasUnsupportedException;
import io.atlasmap.spi.AtlasConversionConcern;
import io.atlasmap.spi.AtlasConversionInfo;
import io.atlasmap.spi.AtlasPrimitiveConverter;
import io.atlasmap.v2.FieldType;
public class FloatConverter implements AtlasPrimitiveConverter {
/**
* @param value
* @return
* @throws AtlasConversionException
*/
@Override
@AtlasConversionInfo(sourceType = FieldType.FLOAT, targetType = FieldType.BOOLEAN, concerns = {
AtlasConversionConcern.CONVENTION })
public Boolean convertToBoolean(Float value, String sourceFormat, String targetFormat)
throws AtlasConversionException {
if (value == null) {
return null;
}
if (value == 0.0) {
return Boolean.FALSE;
} else {
return Boolean.TRUE;
}
}
/**
* @param value
* @return
* @throws AtlasConversionException
*/
@Override
@AtlasConversionInfo(sourceType = FieldType.FLOAT, targetType = FieldType.BYTE, concerns = {
AtlasConversionConcern.RANGE })
public Byte convertToByte(Float value) throws AtlasConversionException {
if (value == null) {
return null;
}
if (value % 1 == 0 && value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
return value.byteValue();
} else {
throw new AtlasConversionException(new AtlasUnsupportedException(String.format(
"Float %s is greater than Byte.MAX_VALUE or less than Byte.MIN_VALUE or is not a whole number",
value)));
}
}
/**
* @param value
* @return
* @throws AtlasConversionException
*/
@Override
@AtlasConversionInfo(sourceType = FieldType.FLOAT, targetType = FieldType.CHAR, concerns = {
AtlasConversionConcern.RANGE, AtlasConversionConcern.CONVENTION })
public Character convertToCharacter(Float value) throws AtlasConversionException {
if (value == null) {
return null;
}
if (value < Character.MIN_VALUE || value > Character.MAX_VALUE) {
throw new AtlasConversionException(String
.format("Float %s is greater than Character.MAX_VALUE or is less than Character.MIN_VALUE", value));
}
return Character.valueOf((char) value.intValue());
}
/**
* @param value
* @return
* @throws AtlasConversionException
*/
@Override
@AtlasConversionInfo(sourceType = FieldType.FLOAT, targetType = FieldType.DOUBLE)
public Double convertToDouble(Float value) throws AtlasConversionException {
if (value == null) {
return null;
}
return value.doubleValue();
}
/**
*
* @param value
* @return
* @throws AtlasConversionException
*/
@Override
@AtlasConversionInfo(sourceType = FieldType.FLOAT, targetType = FieldType.FLOAT)
public Float convertToFloat(Float value) throws AtlasConversionException {
if (value == null) {
return null;
}
// we want a copy of value
return value.floatValue();
}
/**
*
* @param value
* @return
* @throws AtlasConversionException
*/
@Override
@AtlasConversionInfo(sourceType = FieldType.FLOAT, targetType = FieldType.INTEGER, concerns = AtlasConversionConcern.RANGE)
public Integer convertToInteger(Float value) throws AtlasConversionException {
if (value == null) {
return null;
}
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
throw new AtlasConversionException(String
.format("Float %s is greater than Integer.MAX_VALUE or is less than Integer.MIN_VALUE", value));
}
return value.intValue();
}
/**
*
* @param value
* @return
* @throws AtlasConversionException
*/
@Override
@AtlasConversionInfo(sourceType = FieldType.FLOAT, targetType = FieldType.LONG, concerns = AtlasConversionConcern.RANGE)
public Long convertToLong(Float value) throws AtlasConversionException {
if (value == null) {
return null;
}
if (value > Long.MAX_VALUE || value < Long.MIN_VALUE) {
throw new AtlasConversionException(
String.format("Float %s is greater than Long.MAX_VALUE or is less than Long.MIN_VALUE", value));
}
return value.longValue();
}
/**
*
* @param value
* @return
* @throws AtlasConversionException
*/
@Override
@AtlasConversionInfo(sourceType = FieldType.FLOAT, targetType = FieldType.SHORT, concerns = AtlasConversionConcern.RANGE)
public Short convertToShort(Float value) throws AtlasConversionException {
if (value == null) {
return null;
}
if (value > Short.MAX_VALUE || value < Short.MIN_VALUE) {
throw new AtlasConversionException(
String.format("Float %s is greater than Short.MAX_VALUE or is less than Short.MIN_VALUE", value));
}
return value.shortValue();
}
/**
*
* @param value
* @return
* @throws AtlasConversionException
*/
@Override
@AtlasConversionInfo(sourceType = FieldType.FLOAT, targetType = FieldType.STRING)
public String convertToString(Float value, String sourceFormat, String targetFormat)
throws AtlasConversionException {
if (value == null) {
return null;
}
return String.valueOf(value);
}
}