tech.ydb.yoj.databind.expression.values.UuidFieldValue 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 lombok.NonNull;
import tech.ydb.yoj.databind.FieldValueType;
import tech.ydb.yoj.databind.expression.IllegalExpressionException.FieldTypeError.UuidFieldExpected;
import java.lang.reflect.Type;
import java.util.Optional;
import java.util.UUID;
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 UuidFieldValue(@NonNull UUID uuid) implements FieldValue {
@Override
public Optional> getComparableByType(Type fieldType, FieldValueType valueType) {
return switch (valueType) {
case UUID, STRING -> Optional.of(uuid.toString());
default -> Optional.empty();
};
}
@Override
public ValidationResult isValidValueOfType(Type fieldType, FieldValueType valueType) {
return valueType == FieldValueType.UUID || valueType == FieldValueType.STRING
? validFieldValue() // All UUIDs are representable as both UUID and String
: invalidFieldValue(UuidFieldExpected::new, p -> format("Specified an UUID value for non-UUID/non-String field \"%s\"", p));
}
@Override
public String toString() {
return uuid.toString();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy