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

tech.ydb.yoj.databind.expression.NullExpr 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;

import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.Value;
import tech.ydb.yoj.databind.schema.Schema;

import java.util.Map;
import java.util.function.UnaryOperator;

import static java.lang.String.format;
import static lombok.AccessLevel.PRIVATE;

@Value
@AllArgsConstructor(access = PRIVATE)
public class NullExpr extends LeafExpression {
    Schema schema;

    boolean generated;

    Operator operator;

    Schema.JavaField field;

    public NullExpr(@NonNull Schema schema, boolean generated,
                    @NonNull ModelField field, @NonNull Operator operator) {
        this(schema, generated, operator, field.getJavaField());
    }

    @Override
    public Type getType() {
        return Type.NULL;
    }

    public boolean isActualValueNull(@NonNull T obj) {
        Map flattened = schema.flatten(obj);
        return field.flatten().allMatch(f -> flattened.get(field.getName()) == null);
    }

    @Override
    public  V visit(@NonNull Visitor visitor) {
        return visitor.visitNullExpr(this);
    }

    @Override
    public FilterExpression negate() {
        return new NullExpr<>(schema, generated, operator.negate(), field);
    }

    @Override
    public  NullExpr forSchema(@NonNull Schema dstSchema, @NonNull UnaryOperator pathTransformer) {
        Schema.JavaField newField = this.field.forSchema(dstSchema, pathTransformer);
        return new NullExpr<>(dstSchema, this.generated, this.operator, newField);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        NullExpr that = (NullExpr) o;

        if (!schema.equals(that.schema)) {
            return false;
        }
        if (!operator.name().equals(that.operator.name())) {
            return false;
        }
        return field.equals(that.field);
    }

    @Override
    public int hashCode() {
        int result = schema.hashCode();
        result = 31 * result + operator.name().hashCode();
        result = 31 * result + field.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return format("%s %s", field.getPath(), operator);
    }

    public enum Operator {
        /**
         * The value is null.
         */
        IS_NULL {
            @Override
            public Operator negate() {
                return IS_NOT_NULL;
            }

            @Override
            public String toString() {
                return "IS NULL";
            }
        },
        /**
         * The value is not null.
         */
        IS_NOT_NULL {
            @Override
            public Operator negate() {
                return IS_NULL;
            }

            @Override
            public String toString() {
                return "IS NOT NULL";
            }
        };

        public abstract Operator negate();
    }
}