aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/SetMembershipCondition.java
blob: a1fdbbdf0fecfbb9e32fc06d998659b85ef85cfe (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.treenet.rule;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Represents a set membership test on the form <code>feature IN (integer1, integer2 ...)</code>
 *
 * @author bratseth
 * @since   5.1.21
 */
public class SetMembershipCondition extends Condition {

    private final List<Object> setValues;

    /**
     * Constructs a new instance of this class.
     *
     * @param testValue the name of the feature to test
     * @param setValues the set of values to compare to
     * @param trueLabel the label to jump to if the value is in the set
     * @param falseLabel the label to jumt to if the value is not in the set
     */
    public SetMembershipCondition(String testValue, List<Object> setValues, String trueLabel, String falseLabel) {
        super(testValue, trueLabel, falseLabel);
        this.setValues = Collections.unmodifiableList(new ArrayList<>(setValues));
    }

    /** Returns the unmodifiable set of values to check */
    public List<Object> getSetValues() { return setValues; }

    @Override
    protected String conditionToRankingExpression() {
        StringBuilder b = new StringBuilder("in [");
        for (Iterator<Object> i = setValues.iterator(); i.hasNext(); ) {
            Object value = i.next();
            if (value instanceof String)
                b.append("\"").append(value).append("\"");
            else if (value instanceof Integer)
                b.append(value);
            else
                throw new RuntimeException("Excepted a string or integer in a set membership test, not a " +
                                           value.getClass() + ": " + value);

            if (i.hasNext())
                b.append(",");
        }
        b.append("]");
        return b.toString();
    }

}