All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.ydb.yoj.databind.expression.values.IntegerFieldValue Maven / Gradle / Ivy

Go to download

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.IntegerBadTimestamp;
import tech.ydb.yoj.databind.expression.IllegalExpressionException.FieldTypeError.IntegerFieldExpected;

import java.lang.reflect.Type;
import java.time.Instant;
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 IntegerFieldValue(long num) implements FieldValue {
    @Override
    public Optional> getComparableByType(Type fieldType, FieldValueType valueType) {
        return switch (valueType) {
            case INTEGER -> Optional.of(num);
            case REAL -> Optional.of((double) num);
            case TIMESTAMP -> Optional.of(Instant.ofEpochMilli(num));
            default -> Optional.empty();
        };
    }

    @Override
    public ValidationResult isValidValueOfType(Type fieldType, FieldValueType valueType) {
        return switch (valueType) {
            case INTEGER, REAL -> validFieldValue();
            case TIMESTAMP -> num >= 0
                    ? validFieldValue()
                    : invalidFieldValue(IntegerBadTimestamp::new, p -> format("Negative integer value for timestamp field \"%s\"", p));
            default -> invalidFieldValue(IntegerFieldExpected::new, p -> format("Specified an integer value for non-integer field \"%s\"", p));
        };
    }

    @Override
    public String toString() {
        return Long.toString(num);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy