1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.metapath.cst.logic;
7   
8   import dev.metaschema.core.metapath.IExpression;
9   import dev.metaschema.core.metapath.cst.AbstractBinaryExpression;
10  import dev.metaschema.core.metapath.function.ComparisonFunctions;
11  import dev.metaschema.core.util.ObjectUtils;
12  import edu.umd.cs.findbugs.annotations.NonNull;
13  
14  /**
15   * A common base class for all comparison nodes, which consist of two
16   * expressions representing the left and right sides of the comparison, and a
17   * comparison operator.
18   */
19  public abstract class AbstractComparison
20      extends AbstractBinaryExpression<IExpression, IExpression>
21      implements IBooleanLogicExpression {
22  
23    @NonNull
24    private final ComparisonFunctions.Operator operator;
25  
26    /**
27     * Construct an expression that compares the result of the {@code right}
28     * expression with the result of the {@code left} expression using the specified
29     * {@code operator}.
30     *
31     * @param text
32     *          the parsed text of the expression
33     * @param left
34     *          the expression to compare against
35     * @param operator
36     *          the comparison operator
37     * @param right
38     *          the expression to compare with
39     */
40    public AbstractComparison(
41        @NonNull String text,
42        @NonNull IExpression left,
43        @NonNull ComparisonFunctions.Operator operator,
44        @NonNull IExpression right) {
45      super(text, left, right);
46      this.operator = ObjectUtils.requireNonNull(operator, "operator");
47    }
48  
49    /**
50     * Get the comparison operator.
51     *
52     * @return the operator
53     */
54    @NonNull
55    public ComparisonFunctions.Operator getOperator() {
56      return operator;
57    }
58  
59    @SuppressWarnings("null")
60    @Override
61    public String toCSTString() {
62      return String.format("%s[operator=%s]", getClass().getName(), operator);
63    }
64  
65  }