From ddcae93b2d60a75647ee5371221ab52d70a7edbd Mon Sep 17 00:00:00 2001 From: Nikolai Amelichev Date: Tue, 3 Sep 2024 13:25:12 +0200 Subject: [PATCH] Restore old-package `FieldValue` for maximum compatibility --- .../yoj/databind/expression/FieldValue.java | 247 ++++++++++++++++++ .../expression/values/FieldValue.java | 207 +-------------- 2 files changed, 251 insertions(+), 203 deletions(-) create mode 100644 databind/src/main/java/tech/ydb/yoj/databind/expression/FieldValue.java diff --git a/databind/src/main/java/tech/ydb/yoj/databind/expression/FieldValue.java b/databind/src/main/java/tech/ydb/yoj/databind/expression/FieldValue.java new file mode 100644 index 00000000..f1bedfce --- /dev/null +++ b/databind/src/main/java/tech/ydb/yoj/databind/expression/FieldValue.java @@ -0,0 +1,247 @@ +package tech.ydb.yoj.databind.expression; + +import lombok.NonNull; +import tech.ydb.yoj.DeprecationWarnings; +import tech.ydb.yoj.databind.ByteArray; +import tech.ydb.yoj.databind.expression.values.BooleanFieldValue; +import tech.ydb.yoj.databind.expression.values.ByteArrayFieldValue; +import tech.ydb.yoj.databind.expression.values.IntegerFieldValue; +import tech.ydb.yoj.databind.expression.values.RealFieldValue; +import tech.ydb.yoj.databind.expression.values.StringFieldValue; +import tech.ydb.yoj.databind.expression.values.TimestampFieldValue; +import tech.ydb.yoj.databind.expression.values.Tuple; +import tech.ydb.yoj.databind.expression.values.TupleFieldValue; +import tech.ydb.yoj.databind.expression.values.UuidFieldValue; +import tech.ydb.yoj.databind.schema.Schema; + +import javax.annotation.Nullable; +import java.time.Instant; +import java.util.Map; +import java.util.UUID; + +/** + * @deprecated This class is kept for backward compatibility and will be removed in YOJ 3.0.0. + * Please use {@link tech.ydb.yoj.databind.expression.values.FieldValue} instead. + */ +@Deprecated(forRemoval = true) +public interface FieldValue { + Object getRaw(@NonNull Schema.JavaField field); + + Comparable getComparable(@NonNull Schema.JavaField field); + + /////////////////////////////////////////////// + /// COMPATIBILITY QUERIES AND STATIC FACTORIES + + @Nullable + static Comparable getComparable(@NonNull Map values, + @NonNull Schema.JavaField field) { + DeprecationWarnings.warnOnce("FieldValue.getComparable", + "Please use new tech.ydb.yoj.databind.expression.values.FieldValue.getComparable()"); + return tech.ydb.yoj.databind.expression.values.FieldValue.getComparable(values, field); + } + + static FieldValue ofObj(@NonNull Object obj, @NonNull Schema.JavaField schemaField) { + DeprecationWarnings.warnOnce("FieldValue.ofObj", + "Please use new tech.ydb.yoj.databind.expression.values.FieldValue.ofObj()"); + return tech.ydb.yoj.databind.expression.values.FieldValue.ofObj(obj, schemaField); + } + + /** + * @return {@code true} if this field value is an integer; {@code false} otherwise + * @see IntegerFieldValue + * @deprecated We recommend using + * Pattern Matching for {@code instanceof} + * (Java 17+) or Pattern + * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. + */ + @Deprecated + default boolean isNumber() { + return this instanceof IntegerFieldValue; + } + + /** + * @return {@code true} if this field value is an floating-point number; {@code false} otherwise + * @see RealFieldValue + * @deprecated We recommend using + * Pattern Matching for {@code instanceof} + * (Java 17+) or Pattern + * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. + */ + @Deprecated + default boolean isReal() { + return this instanceof RealFieldValue; + } + + /** + * @return {@code true} if this field value is a String; {@code false} otherwise + * @see StringFieldValue + * @deprecated We recommend using + * Pattern Matching for {@code instanceof} + * (Java 17+) or Pattern + * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. + */ + @Deprecated + default boolean isString() { + return this instanceof StringFieldValue; + } + + /** + * @return {@code true} if this field value is an boolean; {@code false} otherwise + * @see BooleanFieldValue + * @deprecated We recommend using + * Pattern Matching for {@code instanceof} + * (Java 17+) or Pattern + * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. + */ + @Deprecated + default boolean isBool() { + return this instanceof BooleanFieldValue; + } + + /** + * @return {@code true} if this field value is a timestamp; {@code false} otherwise + * @see TimestampFieldValue + * @deprecated We recommend using + * Pattern Matching for {@code instanceof} + * (Java 17+) or Pattern + * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. + */ + @Deprecated + default boolean isTimestamp() { + return this instanceof TimestampFieldValue; + } + + /** + * @return {@code true} if this field value is a {@link Tuple tuple}; {@code false} otherwise + * @see TupleFieldValue + * @deprecated We recommend using + * Pattern Matching for {@code instanceof} + * (Java 17+) or Pattern + * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. + */ + @Deprecated + default boolean isTuple() { + return this instanceof TupleFieldValue; + } + + /** + * @return {@code true} if this field value is a {@link ByteArray byte array}; {@code false} otherwise + * @see ByteArrayFieldValue + * @deprecated We recommend using + * Pattern Matching for {@code instanceof} + * (Java 17+) or Pattern + * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. + */ + @Deprecated + default boolean isByteArray() { + return this instanceof ByteArrayFieldValue; + } + + /** + * @return {@code true} if this field value is an {@link UUID}; {@code false} otherwise + * @see UuidFieldValue + * @deprecated We recommend using + * Pattern Matching for {@code instanceof} + * (Java 17+) or Pattern + * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. + */ + @Deprecated + default boolean isUuid() { + return this instanceof UuidFieldValue; + } + + /** + * Constructs a new field value that is definitely a String. (Unlike {@link #ofObj(Object, Schema.JavaField)} which may perform implicit conversions from + * a enum or a custom value type.) + * + * @param str string value + * @return field value that holds a specified string value + */ + @NonNull + static FieldValue ofStr(@NonNull String str) { + return new StringFieldValue(str); + } + + /** + * Constructs a new field value that is definitely an integer. (Unlike {@link #ofObj(Object, Schema.JavaField)} which may perform implicit conversions + * from a custom value type.) + * + * @param num integer value + * @return field value that holds the specified integer value + */ + @NonNull + static FieldValue ofNum(long num) { + return new IntegerFieldValue(num); + } + + /** + * Constructs a new field value that is definitely a floating-point value. (Unlike {@link #ofObj(Object, Schema.JavaField)} which may perform implicit + * conversions from a custom value type.) + * + * @param real floating-point value + * @return field value that holds the specified floating-point value + */ + @NonNull + static FieldValue ofReal(double real) { + return new RealFieldValue(real); + } + + /** + * Constructs a new field value that is definitely a boolean value. (Unlike {@link #ofObj(Object, Schema.JavaField)} which may perform implicit + * conversions from a custom value type.) + * + * @param bool boolean value + * @return field value that holds the specified boolean value + */ + @NonNull + static FieldValue ofBool(boolean bool) { + return new BooleanFieldValue(bool); + } + + /** + * Constructs a new field value that is definitely a timestamp value. (Unlike {@link #ofObj(Object, Schema.JavaField)} which may perform implicit + * conversions from a custom value type.) + * + * @param timestamp timestamp value + * @return field value that holds the specified timestamp value + */ + @NonNull + static FieldValue ofTimestamp(@NonNull Instant timestamp) { + return new TimestampFieldValue(timestamp); + } + + /** + * Constructs a new field value that is a tuple. + * + * @param tuple tuple value + * @return field value that holds the specified timestamp value + */ + @NonNull + static FieldValue ofTuple(@NonNull Tuple tuple) { + return new TupleFieldValue(tuple); + } + + /** + * Constructs a new field value that is definitely a byte array value. (Unlike {@link #ofObj(Object, Schema.JavaField)} which may perform implicit + * conversions from a custom value type.) + * + * @param byteArray byte array value + * @return field value that holds the specified byte array value + */ + @NonNull + static FieldValue ofByteArray(@NonNull ByteArray byteArray) { + return new ByteArrayFieldValue(byteArray); + } + + /** + * Constructs a new field value that is definitely an UUID value. (Unlike {@link #ofObj(Object, Schema.JavaField)} which may perform implicit + * conversions from a custom value type.) + * + * @param uuid UUID value + * @return field value that holds the specified UUID value + */ + @NonNull + static FieldValue ofUuid(@NonNull UUID uuid) { + return new UuidFieldValue(uuid); + } +} diff --git a/databind/src/main/java/tech/ydb/yoj/databind/expression/values/FieldValue.java b/databind/src/main/java/tech/ydb/yoj/databind/expression/values/FieldValue.java index 7bca6307..237f2cc8 100644 --- a/databind/src/main/java/tech/ydb/yoj/databind/expression/values/FieldValue.java +++ b/databind/src/main/java/tech/ydb/yoj/databind/expression/values/FieldValue.java @@ -21,16 +21,19 @@ import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toCollection; -public sealed interface FieldValue permits BooleanFieldValue, ByteArrayFieldValue, IntegerFieldValue, +public sealed interface FieldValue extends tech.ydb.yoj.databind.expression.FieldValue + permits BooleanFieldValue, ByteArrayFieldValue, IntegerFieldValue, RealFieldValue, StringFieldValue, TimestampFieldValue, TupleFieldValue, UuidFieldValue { Optional> getComparableByType(Type fieldType, FieldValueType valueType); + @Override default Object getRaw(@NonNull JavaField field) { Comparable cmp = getComparable(field); return CustomValueTypes.postconvert(field, cmp); } + @Override default Comparable getComparable(@NonNull JavaField field) { field = field.isFlat() ? field.toFlatField() : field; FieldValueType valueType = FieldValueType.forSchemaField(field); @@ -90,206 +93,4 @@ static Comparable getComparable(@NonNull Map values, @NonNull return new Tuple(null, tupleValues(field.flatten().toList(), values)); } } - - /////////////////////////////////////////////// - /// COMPATIBILITY QUERIES AND STATIC FACTORIES - - /** - * @return {@code true} if this field value is an integer; {@code false} otherwise - * @see IntegerFieldValue - * @deprecated We recommend using - * Pattern Matching for {@code instanceof} - * (Java 17+) or Pattern - * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. - */ - @Deprecated - default boolean isNumber() { - return this instanceof IntegerFieldValue; - } - - /** - * @return {@code true} if this field value is an floating-point number; {@code false} otherwise - * @see RealFieldValue - * @deprecated We recommend using - * Pattern Matching for {@code instanceof} - * (Java 17+) or Pattern - * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. - */ - @Deprecated - default boolean isReal() { - return this instanceof RealFieldValue; - } - - /** - * @return {@code true} if this field value is a String; {@code false} otherwise - * @see StringFieldValue - * @deprecated We recommend using - * Pattern Matching for {@code instanceof} - * (Java 17+) or Pattern - * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. - */ - @Deprecated - default boolean isString() { - return this instanceof StringFieldValue; - } - - /** - * @return {@code true} if this field value is an boolean; {@code false} otherwise - * @see BooleanFieldValue - * @deprecated We recommend using - * Pattern Matching for {@code instanceof} - * (Java 17+) or Pattern - * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. - */ - @Deprecated - default boolean isBool() { - return this instanceof BooleanFieldValue; - } - - /** - * @return {@code true} if this field value is a timestamp; {@code false} otherwise - * @see TimestampFieldValue - * @deprecated We recommend using - * Pattern Matching for {@code instanceof} - * (Java 17+) or Pattern - * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. - */ - @Deprecated - default boolean isTimestamp() { - return this instanceof TimestampFieldValue; - } - - /** - * @return {@code true} if this field value is a {@link Tuple tuple}; {@code false} otherwise - * @see TupleFieldValue - * @deprecated We recommend using - * Pattern Matching for {@code instanceof} - * (Java 17+) or Pattern - * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. - */ - @Deprecated - default boolean isTuple() { - return this instanceof TupleFieldValue; - } - - /** - * @return {@code true} if this field value is a {@link ByteArray byte array}; {@code false} otherwise - * @see ByteArrayFieldValue - * @deprecated We recommend using - * Pattern Matching for {@code instanceof} - * (Java 17+) or Pattern - * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. - */ - @Deprecated - default boolean isByteArray() { - return this instanceof ByteArrayFieldValue; - } - - /** - * @return {@code true} if this field value is an {@link UUID}; {@code false} otherwise - * @see UuidFieldValue - * @deprecated We recommend using - * Pattern Matching for {@code instanceof} - * (Java 17+) or Pattern - * Matching for {@code switch} Expressions and Statements (Java 21+), because {@code FieldValue} is a {@code sealed} interface. - */ - @Deprecated - default boolean isUuid() { - return this instanceof UuidFieldValue; - } - - /** - * Constructs a new field value that is definitely a String. (Unlike {@link #ofObj(Object, JavaField)} which may perform implicit conversions from - * a enum or a custom value type.) - * - * @param str string value - * @return field value that holds a specified string value - */ - @NonNull - static FieldValue ofStr(@NonNull String str) { - return new StringFieldValue(str); - } - - /** - * Constructs a new field value that is definitely an integer. (Unlike {@link #ofObj(Object, JavaField)} which may perform implicit conversions - * from a custom value type.) - * - * @param num integer value - * @return field value that holds the specified integer value - */ - @NonNull - static FieldValue ofNum(long num) { - return new IntegerFieldValue(num); - } - - /** - * Constructs a new field value that is definitely a floating-point value. (Unlike {@link #ofObj(Object, JavaField)} which may perform implicit - * conversions from a custom value type.) - * - * @param real floating-point value - * @return field value that holds the specified floating-point value - */ - @NonNull - static FieldValue ofReal(double real) { - return new RealFieldValue(real); - } - - /** - * Constructs a new field value that is definitely a boolean value. (Unlike {@link #ofObj(Object, JavaField)} which may perform implicit - * conversions from a custom value type.) - * - * @param bool boolean value - * @return field value that holds the specified boolean value - */ - @NonNull - static FieldValue ofBool(boolean bool) { - return new BooleanFieldValue(bool); - } - - /** - * Constructs a new field value that is definitely a timestamp value. (Unlike {@link #ofObj(Object, JavaField)} which may perform implicit - * conversions from a custom value type.) - * - * @param timestamp timestamp value - * @return field value that holds the specified timestamp value - */ - @NonNull - static FieldValue ofTimestamp(@NonNull Instant timestamp) { - return new TimestampFieldValue(timestamp); - } - - /** - * Constructs a new field value that is a tuple. - * - * @param tuple tuple value - * @return field value that holds the specified timestamp value - */ - @NonNull - static FieldValue ofTuple(@NonNull Tuple tuple) { - return new TupleFieldValue(tuple); - } - - /** - * Constructs a new field value that is definitely a byte array value. (Unlike {@link #ofObj(Object, JavaField)} which may perform implicit - * conversions from a custom value type.) - * - * @param byteArray byte array value - * @return field value that holds the specified byte array value - */ - @NonNull - static FieldValue ofByteArray(@NonNull ByteArray byteArray) { - return new ByteArrayFieldValue(byteArray); - } - - /** - * Constructs a new field value that is definitely an UUID value. (Unlike {@link #ofObj(Object, JavaField)} which may perform implicit - * conversions from a custom value type.) - * - * @param uuid UUID value - * @return field value that holds the specified UUID value - */ - @NonNull - static FieldValue ofUuid(@NonNull UUID uuid) { - return new UuidFieldValue(uuid); - } }