|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +package dev.vortex.api.expressions; |
| 5 | + |
| 6 | +import dev.vortex.api.Expression; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Objects; |
| 9 | +import java.util.Optional; |
| 10 | + |
| 11 | +/** |
| 12 | + * Represents an IS NOT NULL expression that checks whether values are non-null. |
| 13 | + * This expression returns true for non-null values and false for null values. |
| 14 | + */ |
| 15 | +public final class IsNotNull implements Expression { |
| 16 | + private final Expression child; |
| 17 | + |
| 18 | + private IsNotNull(Expression child) { |
| 19 | + this.child = child; |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Parses an IsNotNull expression from serialized metadata and child expressions. |
| 24 | + * This method is used during deserialization of Vortex expressions. |
| 25 | + * |
| 26 | + * @param metadata the serialized metadata, must be empty for IsNotNull expressions |
| 27 | + * @param children the child expressions, must contain exactly one element |
| 28 | + * @return a new IsNotNull expression parsed from the provided data |
| 29 | + * @throws IllegalArgumentException if the number of children is not exactly one, |
| 30 | + * or if metadata is not empty |
| 31 | + */ |
| 32 | + public static IsNotNull parse(byte[] metadata, List<Expression> children) { |
| 33 | + if (children.size() != 1) { |
| 34 | + throw new IllegalArgumentException( |
| 35 | + "IsNotNull expression must have exactly one child, found: " + children.size()); |
| 36 | + } |
| 37 | + if (metadata.length > 0) { |
| 38 | + throw new IllegalArgumentException( |
| 39 | + "IsNotNull expression must not have metadata, found: " + metadata.length); |
| 40 | + } |
| 41 | + return new IsNotNull(children.get(0)); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Creates a new IsNotNull expression that checks non-nullity of the given child expression. |
| 46 | + * |
| 47 | + * @param child the expression to check for non-null values |
| 48 | + * @return a new IsNotNull expression |
| 49 | + */ |
| 50 | + public static IsNotNull of(Expression child) { |
| 51 | + return new IsNotNull(child); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean equals(Object o) { |
| 56 | + if (o == null || getClass() != o.getClass()) return false; |
| 57 | + IsNotNull other = (IsNotNull) o; |
| 58 | + return Objects.equals(child, other.child); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public int hashCode() { |
| 63 | + return Objects.hash(child); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String id() { |
| 68 | + return "vortex.is_not_null"; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public List<Expression> children() { |
| 73 | + return List.of(child); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Optional<byte[]> metadata() { |
| 78 | + return Optional.of(new byte[] {}); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public String toString() { |
| 83 | + return "vortex.is_not_null(" + child + ")"; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns the child expression that will be checked for non-null values. |
| 88 | + * |
| 89 | + * @return the child expression |
| 90 | + */ |
| 91 | + public Expression getChild() { |
| 92 | + return child; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public <T> T accept(Visitor<T> visitor) { |
| 97 | + return visitor.visitIsNotNull(this); |
| 98 | + } |
| 99 | +} |
0 commit comments