Skip to content

Commit

Permalink
Bring back FieldValue.of(...) factories and FieldValue.is...() te…
Browse files Browse the repository at this point in the history
…st methods. Rename: `BoolFieldValue` -> `BooleanFieldValue` to reflect `FieldValueType`'s name 1:1.
  • Loading branch information
nvamelichev committed Sep 2, 2024
1 parent f6bf096 commit 0f8578f
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import tech.ydb.yoj.databind.expression.IllegalExpressionException.FieldTypeError.RealFieldExpected;
import tech.ydb.yoj.databind.expression.IllegalExpressionException.FieldTypeError.StringFieldExpected;
import tech.ydb.yoj.databind.expression.IllegalExpressionException.FieldTypeError.UnknownEnumConstant;
import tech.ydb.yoj.databind.expression.values.BoolFieldValue;
import tech.ydb.yoj.databind.expression.values.BooleanFieldValue;
import tech.ydb.yoj.databind.expression.values.ByteArrayFieldValue;
import tech.ydb.yoj.databind.expression.values.FieldValue;
import tech.ydb.yoj.databind.expression.values.IntegerFieldValue;
Expand Down Expand Up @@ -119,7 +119,7 @@ public FieldValue validateValue(@NonNull FieldValue value) {
fieldValueType == FieldValueType.REAL,
RealFieldExpected::new,
p -> format("Specified a real value for non-real field \"%s\"", p));
} else if (value instanceof BoolFieldValue) {
} else if (value instanceof BooleanFieldValue) {
checkArgument(fieldValueType == FieldValueType.BOOLEAN,
BooleanFieldExpected::new,
p -> format("Specified a boolean value for non-boolean field \"%s\"", p));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.lang.reflect.Type;
import java.util.Optional;

public record BoolFieldValue(boolean bool) implements FieldValue {
public record BooleanFieldValue(boolean bool) implements FieldValue {
@Override
public Optional<Comparable<?>> getComparableByType(Type fieldType, FieldValueType valueType) {
if (valueType != FieldValueType.BOOLEAN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,8 @@
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

public sealed interface FieldValue permits
BoolFieldValue,
ByteArrayFieldValue,
IntegerFieldValue,
RealFieldValue,
StringFieldValue,
TimestampFieldValue,
TupleFieldValue,
UuidFieldValue {
public sealed interface FieldValue permits BooleanFieldValue, ByteArrayFieldValue, IntegerFieldValue,
RealFieldValue, StringFieldValue, TimestampFieldValue, TupleFieldValue, UuidFieldValue {

Optional<Comparable<?>> getComparableByType(Type fieldType, FieldValueType valueType);

Expand Down Expand Up @@ -57,7 +50,7 @@ static FieldValue ofObj(@NonNull Object obj, @NonNull JavaField schemaField) {
case ENUM -> new StringFieldValue(((Enum<?>) obj).name());
case INTEGER -> new IntegerFieldValue(((Number) obj).longValue());
case REAL -> new RealFieldValue(((Number) obj).doubleValue());
case BOOLEAN -> new BoolFieldValue((Boolean) obj);
case BOOLEAN -> new BooleanFieldValue((Boolean) obj);
case BYTE_ARRAY -> new ByteArrayFieldValue((ByteArray) obj);
case TIMESTAMP -> new TimestampFieldValue((Instant) obj);
case UUID -> new UuidFieldValue((UUID) obj);
Expand Down Expand Up @@ -97,4 +90,206 @@ static Comparable<?> getComparable(@NonNull Map<String, Object> 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
* <a href="https://docs.oracle.com/en/java/javase/17/language/pattern-matching-instanceof-operator.html">Pattern Matching for {@code instanceof}
* </a> (Java 17+) or <a href="https://docs.oracle.com/en/java/javase/21/language/pattern-matching-switch-expressions-and-statements.html">Pattern
* Matching for {@code switch} Expressions and Statements</a> (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
* <a href="https://docs.oracle.com/en/java/javase/17/language/pattern-matching-instanceof-operator.html">Pattern Matching for {@code instanceof}
* </a> (Java 17+) or <a href="https://docs.oracle.com/en/java/javase/21/language/pattern-matching-switch-expressions-and-statements.html">Pattern
* Matching for {@code switch} Expressions and Statements</a> (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
* <a href="https://docs.oracle.com/en/java/javase/17/language/pattern-matching-instanceof-operator.html">Pattern Matching for {@code instanceof}
* </a> (Java 17+) or <a href="https://docs.oracle.com/en/java/javase/21/language/pattern-matching-switch-expressions-and-statements.html">Pattern
* Matching for {@code switch} Expressions and Statements</a> (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
* <a href="https://docs.oracle.com/en/java/javase/17/language/pattern-matching-instanceof-operator.html">Pattern Matching for {@code instanceof}
* </a> (Java 17+) or <a href="https://docs.oracle.com/en/java/javase/21/language/pattern-matching-switch-expressions-and-statements.html">Pattern
* Matching for {@code switch} Expressions and Statements</a> (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
* <a href="https://docs.oracle.com/en/java/javase/17/language/pattern-matching-instanceof-operator.html">Pattern Matching for {@code instanceof}
* </a> (Java 17+) or <a href="https://docs.oracle.com/en/java/javase/21/language/pattern-matching-switch-expressions-and-statements.html">Pattern
* Matching for {@code switch} Expressions and Statements</a> (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
* <a href="https://docs.oracle.com/en/java/javase/17/language/pattern-matching-instanceof-operator.html">Pattern Matching for {@code instanceof}
* </a> (Java 17+) or <a href="https://docs.oracle.com/en/java/javase/21/language/pattern-matching-switch-expressions-and-statements.html">Pattern
* Matching for {@code switch} Expressions and Statements</a> (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
* <a href="https://docs.oracle.com/en/java/javase/17/language/pattern-matching-instanceof-operator.html">Pattern Matching for {@code instanceof}
* </a> (Java 17+) or <a href="https://docs.oracle.com/en/java/javase/21/language/pattern-matching-switch-expressions-and-statements.html">Pattern
* Matching for {@code switch} Expressions and Statements</a> (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
* <a href="https://docs.oracle.com/en/java/javase/17/language/pattern-matching-instanceof-operator.html">Pattern Matching for {@code instanceof}
* </a> (Java 17+) or <a href="https://docs.oracle.com/en/java/javase/21/language/pattern-matching-switch-expressions-and-statements.html">Pattern
* Matching for {@code switch} Expressions and Statements</a> (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);
}
}

0 comments on commit 0f8578f

Please sign in to comment.