summaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/NegativeNode.java
blob: 11feddb919ee43b854a86578715a307152ca03d8 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression.rule;

import com.yahoo.searchlib.rankingexpression.evaluation.Context;
import com.yahoo.searchlib.rankingexpression.evaluation.Value;

import java.util.Collections;
import java.util.Deque;
import java.util.List;

/**
 * A node which flips the sign of the value produced from the nested expression
 *
 * @author <a href="mailto:bratseth@yahoo-inc.com">Jon Bratseth</a>
 */
public class NegativeNode extends CompositeNode {

    private final ExpressionNode value;

    /** Constructs a new negative node */
    public NegativeNode(ExpressionNode value) {
        this.value = value;
    }

    /** Returns the node creating the value negated by this */
    public ExpressionNode getValue() { return value; }

    @Override
    public List<ExpressionNode> children() {
        return Collections.singletonList(value);
    }

    @Override
    public String toString(SerializationContext context, Deque<String> path, CompositeNode parent) {
        return "-" + value.toString(context, path, parent);
    }

    @Override
    public Value evaluate(Context context) {
        return value.evaluate(context).negate();
    }

    @Override
    public NegativeNode setChildren(List<ExpressionNode> children) {
        if (children.size() != 1) throw new IllegalArgumentException("Expected 1 children but got " + children.size());
        return new NegativeNode(children.get(0));
    }

}