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

import com.google.common.collect.ImmutableList;
import com.yahoo.searchlib.rankingexpression.evaluation.Context;
import com.yahoo.searchlib.rankingexpression.evaluation.Value;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

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

    private final ImmutableList<ExpressionNode> expressions;

    public Arguments() {
        this(null);
    }

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

        // Build in a roundabout way because java generics and lists
        ImmutableList.Builder<ExpressionNode> b = ImmutableList.builder();
        for (ExpressionNode node : expressions)
            b.add(node);
        this.expressions = b.build();
    }

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

    /** 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 rhs) {
        return rhs instanceof Arguments && expressions.equals(((Arguments)rhs).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();
    }

}