tech.ydb.yoj.databind.expression.values.RealFieldValue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yoj-databind Show documentation
Show all versions of yoj-databind Show documentation
Core data-binding logic used by YOJ (YDB ORM for Java) to convert
between Java objects and database rows (or anything representable by
a Java Map, really).
The newest version!
package tech.ydb.yoj.databind.expression.values;
import tech.ydb.yoj.databind.FieldValueType;
import tech.ydb.yoj.databind.expression.IllegalExpressionException.FieldTypeError.IntegerToRealInexact;
import tech.ydb.yoj.databind.expression.IllegalExpressionException.FieldTypeError.RealFieldExpected;
import java.lang.reflect.Type;
import java.util.Optional;
import static java.lang.String.format;
import static tech.ydb.yoj.databind.expression.values.FieldValue.ValidationResult.invalidFieldValue;
import static tech.ydb.yoj.databind.expression.values.FieldValue.ValidationResult.validFieldValue;
public record RealFieldValue(double real) implements FieldValue {
// Maximum 64-bit integer value that is perfectly representable as a double-precision IEEE 754 value.
// That is 1L << 53L, @see https://stackoverflow.com/a/1848758
private static final long MIN_REPRESENTABLE_LONG = -9007199254740992L;
private static final long MAX_REPRESENTABLE_LONG = 9007199254740992L;
@Override
public Optional> getComparableByType(Type fieldType, FieldValueType valueType) {
return switch (valueType) {
case REAL -> Optional.of(real);
case INTEGER -> Optional.of((long) real);
default -> Optional.empty();
};
}
@Override
public ValidationResult isValidValueOfType(Type fieldType, FieldValueType valueType) {
return switch (valueType) {
case REAL -> validFieldValue();
case INTEGER -> (real >= -MIN_REPRESENTABLE_LONG && real <= MAX_REPRESENTABLE_LONG)
? validFieldValue()
: invalidFieldValue(IntegerToRealInexact::new, p -> format("Integer value magnitude is too large for real field \"%s\"", p));
default -> invalidFieldValue(RealFieldExpected::new, p -> format("Specified a real value for non-real field \"%s\"", p));
};
}
@Override
public String toString() {
return Double.toString(real);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy