aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java
blob: b716b6930112309fd8e189d2519c18155a24ba04 (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
73
74
75
76
77
78
79
80
81
82
83
// Copyright Yahoo. 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.io.Serializable;
import java.util.List;

/**
 * A set of argument expressions to a function or feature.
 * This is a value object.
 *
 * @author bratseth
 */
public final class Arguments implements Serializable {

    public static final Arguments EMPTY = new Arguments();

    private final List<ExpressionNode> expressions;

    public Arguments() {
        this(List.of());
    }

    public Arguments(ExpressionNode singleArgument) {
        this(List.of(singleArgument));
    }

    public Arguments(List<? extends ExpressionNode> expressions) {
        if (expressions == null) {
            this.expressions = List.of();
            return;
        }

        this.expressions = List.copyOf(expressions);
    }

    /** Returns an unmodifiable list of the expressions in this, never null */
    public List<ExpressionNode> expressions() { return expressions; }

    /** Returns the number of arguments in this */
    public int size() { return expressions.size(); }

    /** Evaluate all arguments in this */
    public Value[] evaluate(Context context) {
        Value[] values = new Value[expressions.size()];
        for (int i = 0; i < expressions.size(); i++)
            values[i] = expressions.get(i).evaluate(context);
        return values;
    }

    /** Evaluate the i'th argument */
    public Value evaluate(int i,Context context) {
        return expressions.get(i).evaluate(context);
    }

    public boolean isEmpty() { return expressions.isEmpty(); }

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

    @Override
    public boolean equals(Object other) {
        if (other == this) return true;
        return other instanceof Arguments && expressions.equals(((Arguments)other).expressions);
    }

    @Override
    public String toString() {
        StringBuilder b = new StringBuilder();
        b.append("(");
        for (ExpressionNode argument : expressions)
            b.append(argument).append(",");
        b.setLength(b.length()-1);
        if (b.length() > 0)
            b.append(")");
        return b.toString();
    }

}