org.yangcentral.yangkit.model.impl.codec.Decimal64StringValueCodecImpl Maven / Gradle / Ivy
package org.yangcentral.yangkit.model.impl.codec;
import org.yangcentral.yangkit.base.ErrorCode;
import org.yangcentral.yangkit.model.api.codec.Decimal64StringValueCodec;
import org.yangcentral.yangkit.model.api.codec.YangCodecException;
import org.yangcentral.yangkit.model.api.restriction.Restriction;
import java.math.BigDecimal;
public class Decimal64StringValueCodecImpl extends StringValueCodecImpl implements Decimal64StringValueCodec {
public BigDecimal deserialize(Restriction restriction, String input) throws YangCodecException {
BigDecimal bigDecimal;
try {
bigDecimal = new BigDecimal(input);
} catch (NumberFormatException e) {
throw new YangCodecException(ErrorCode.INVALID_VALUE.getFieldName());
}
if (!restriction.evaluated(bigDecimal)) {
throw new YangCodecException(ErrorCode.INVALID_VALUE.getFieldName());
} else {
return bigDecimal;
}
}
public String serialize(Restriction restriction, BigDecimal output) throws YangCodecException {
if (!restriction.evaluated(output)) {
throw new YangCodecException(ErrorCode.INVALID_VALUE.getFieldName());
} else {
return output.toString();
}
}
}