aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search-core/src/main/java/com/yahoo/document/predicate/Negation.java
blob: 62ad522e655f0b4335ed789a4ffcbbf77eeb962e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.predicate;

import java.util.List;
import java.util.Objects;

/**
 * @author Simon Thoresen Hult
 */
public class Negation extends PredicateOperator {

    private Predicate operand;

    public Negation(Predicate operand) {
        Objects.requireNonNull(operand, "operand");
        this.operand = operand;
    }

    public Negation setOperand(Predicate operand) {
        Objects.requireNonNull(operand, "operand");
        this.operand = operand;
        return this;
    }

    public Predicate getOperand() {
        return operand;
    }

    @Override
    public List<Predicate> getOperands() {
        return java.util.Arrays.asList(operand);
    }

    @Override
    public Negation clone() throws CloneNotSupportedException {
        Negation obj = (Negation)super.clone();
        obj.operand = operand.clone();
        return obj;
    }

    @Override
    public int hashCode() {
        return operand.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof Negation)) {
            return false;
        }
        Negation rhs = (Negation)obj;
        if (!operand.equals(rhs.operand)) {
            return false;
        }
        return true;
    }

    @Override
    protected void appendTo(StringBuilder out) {
        if (operand instanceof FeatureSet) {
            ((FeatureSet)operand).appendNegatedTo(out);
        } else {
            out.append("not (");
            operand.appendTo(out);
            out.append(')');
        }
    }

}