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

cz.jirutka.rsql.parser.ast.ComparisonNode Maven / Gradle / Ivy

The newest version!
/*
 * The MIT License
 *
 * Copyright 2013-2014 Jakub Jirutka .
 * Copyright 2024 Edgar Asatryan .
 * Copyright 2024 Jens Borch Christiansen .
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package cz.jirutka.rsql.parser.ast;

import java.util.ArrayList;
import java.util.List;
import net.jcip.annotations.Immutable;

/**
 * This node represents a comparison with operator, selector and arguments,
 * e.g. name=in=(Jimmy,James).
 */
@Immutable
public final class ComparisonNode extends AbstractNode {

    private final ComparisonOperator operator;

    private final String selector;

    private final ComparisonArguments arguments;

    /**
     * @param operator  Must not be null.
     * @param selector  Must not be null or blank.
     * @param arguments Must not be null or empty.
     * @throws IllegalArgumentException If one of the conditions specified above it not met.
     * @since 2.4
     */
    public ComparisonNode(ComparisonOperator operator, String selector, ComparisonArguments arguments) {
        Assert.notNull(operator, "operator must not be null");
        Assert.notBlank(selector, "selector must not be blank");
        Assert.notNull(arguments, "arguments must not be null");
        validate(operator, arguments.asStringList().size());
        if (operator.getType() == ComparisonOperator.UNARY_TYPE) {
            Assert.isTrue(arguments.asStringList().size() == 1,
                    "operator %s expects single argument, but multiple values given", operator);
        } else if (operator.getType() == ComparisonOperator.NESTED_TYPE) {
            Assert.isTrue(arguments instanceof NestedArguments,
                "operator %s expects nested argument, but string value(s) was given", operator);
        }

        this.operator = operator;
        this.selector = selector;
        this.arguments = arguments;
    }

        /**
     * @param operator  Must not be null.
     * @param selector  Must not be null or blank.
     * @param arguments Must not be null or empty. If the operator is not
     *                  {@link ComparisonOperator#isMultiValue() multiValue}, then it must contain exactly
     *                  one argument.
     * @throws IllegalArgumentException If one of the conditions specified above it not met.
     * @deprecated Use {@link #ComparisonNode(ComparisonOperator, String, ComparisonArguments)}
     */
    @Deprecated
    public ComparisonNode(ComparisonOperator operator, String selector, List arguments) {
        this(operator, selector, new StringArguments(arguments));
    }

    public  R accept(RSQLVisitor visitor, A param) {
        return visitor.visit(this, param);
    }

    public ComparisonOperator getOperator() {
        return operator;
    }

    /**
     * Returns a copy of this node with the specified operator.
     *
     * @param newOperator Must not be null.
     * @return a copy of this node with the specified operator.
     */
    public ComparisonNode withOperator(ComparisonOperator newOperator) {
        return new ComparisonNode(newOperator, selector, arguments);
    }

    public String getSelector() {
        return selector;
    }

    /**
     * Returns a copy of this node with the specified selector.
     *
     * @param newSelector Must not be null or blank.
     * @return a copy of this node with the specified selector.
     */
    public ComparisonNode withSelector(String newSelector) {
        return selector.equals(newSelector)
        ? this
        : new ComparisonNode(operator, newSelector, arguments);
    }

    /**
     * Return arguments. It's guaranteed that string arguments contains at least one item.
     * When the operator is not {@link ComparisonOperator#isMultiValue() multiValue}, then it
     * contains exactly one argument.
     *
     * @return the arguments.
     */
    public ComparisonArguments getArgumentsObject() {
        return arguments;
    }

    /**
     * Returns a copy of the arguments list. It's guaranteed that it contains at least one item.
     * When the operator is not {@link ComparisonOperator#isMultiValue() multiValue}, then it
     * contains exactly one argument.
     *
     * @return a copy of the arguments list.
     * @deprecated Use {@link #getArgumentsObject()}
     */
    @Deprecated
    public List getArguments() {
        return new ArrayList<>(arguments.asStringList());
    }

    /**
     * Returns a copy of this node with the specified arguments.
     *
     * @param newArguments Must not be null or empty. If the operator is not
     *                     {@link ComparisonOperator#isMultiValue() multiValue}, then it must contain exactly
     *                     one argument.
     * @return a copy of this node with the specified arguments.
     */
    public ComparisonNode withArguments(ComparisonArguments newArguments) {
        return new ComparisonNode(operator, selector, newArguments);
    }

    /**
     * Returns a copy of this node with the specified arguments.
     *
     * @param newArguments Must not be null or empty. If the operator is not
     *                     {@link ComparisonOperator#isMultiValue() multiValue}, then it must contain exactly
     *                     one argument.
     * @return a copy of this node with the specified arguments.
     */
    public ComparisonNode withArguments(List newArguments) {
        return new ComparisonNode(operator, selector, newArguments);
    }

    private static void validate(ComparisonOperator operator, int argc) {
        Arity arity = operator.getArity();
        int min = arity.min();
        int max = arity.max();

        if (argc < min || argc > max) {
            final String message;
            if (min == max) {
                message = String.format("operator '%s' can have exactly %d argument(s), but got %d",
                    operator.getSymbol(), max, argc);
            } else {
                message = String.format("operator '%s' can have from %d to %d argument(s), but got %d",
                    operator.getSymbol(), min, max, argc);
            }

            throw new IllegalArgumentException(message);
        }
    }

    @Override
    public String toString() {
        if (operator.getType().equals(ComparisonOperator.NULLARY_TYPE)) {
            return selector + operator;
        } else {
            return selector + operator + (arguments.asStringList().size() <= 1 && operator.getArity().max() == 1 ? arguments : "(" + arguments + ")");
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof ComparisonNode)) return false;
        ComparisonNode that = (ComparisonNode) o;

        return arguments.equals(that.arguments)
            && operator.equals(that.operator)
            && selector.equals(that.selector);
    }

    @Override
    public int hashCode() {
        int result = selector.hashCode();
        result = 31 * result + arguments.hashCode();
        result = 31 * result + operator.hashCode();
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy