|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.sql.expression.predicate.nulls; |
| 7 | + |
| 8 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 9 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 10 | +import org.elasticsearch.xpack.sql.expression.gen.processor.Processor; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.Objects; |
| 14 | +import java.util.function.Function; |
| 15 | + |
| 16 | +public class CheckNullProcessor implements Processor { |
| 17 | + |
| 18 | + public enum CheckNullOperation implements Function<Object, Boolean> { |
| 19 | + |
| 20 | + IS_NULL(Objects::isNull, "IS NULL"), |
| 21 | + IS_NOT_NULL(Objects::nonNull, "IS NOT NULL"); |
| 22 | + |
| 23 | + private final Function<Object, Boolean> process; |
| 24 | + private final String symbol; |
| 25 | + |
| 26 | + CheckNullOperation(Function<Object, Boolean> process, String symbol) { |
| 27 | + this.process = process; |
| 28 | + this.symbol = symbol; |
| 29 | + } |
| 30 | + |
| 31 | + public String symbol() { |
| 32 | + return symbol; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public String toString() { |
| 37 | + return symbol; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Boolean apply(Object o) { |
| 42 | + return process.apply(o); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public static final String NAME = "nckn"; |
| 47 | + |
| 48 | + private final CheckNullOperation operation; |
| 49 | + |
| 50 | + CheckNullProcessor(CheckNullOperation operation) { |
| 51 | + this.operation = operation; |
| 52 | + } |
| 53 | + |
| 54 | + public CheckNullProcessor(StreamInput in) throws IOException { |
| 55 | + this(in.readEnum(CheckNullOperation.class)); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getWriteableName() { |
| 60 | + return NAME; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void writeTo(StreamOutput out) throws IOException { |
| 65 | + out.writeEnum(operation); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Object process(Object input) { |
| 70 | + return operation.apply(input); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean equals(Object o) { |
| 75 | + if (this == o) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + if (o == null || getClass() != o.getClass()) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + CheckNullProcessor that = (CheckNullProcessor) o; |
| 82 | + return operation == that.operation; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public int hashCode() { |
| 87 | + return Objects.hash(operation); |
| 88 | + } |
| 89 | +} |
0 commit comments