aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/SwitchExpression.java
blob: 3bf67ff9c5dc6883be48ac2f85830dd2d7b294f8 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage.expressions;

import com.yahoo.document.DataType;
import com.yahoo.document.DocumentType;
import com.yahoo.document.Field;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.text.StringUtilities;
import com.yahoo.vespa.indexinglanguage.ExpressionConverter;
import com.yahoo.vespa.objects.ObjectOperation;
import com.yahoo.vespa.objects.ObjectPredicate;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Simon Thoresen Hult
 */
public final class SwitchExpression extends CompositeExpression {

    private final Map<String, Expression> cases = new LinkedHashMap<>();
    private final Expression defaultExp;

    public <T extends Expression> SwitchExpression(Map<String, T> cases) {
        this(cases, null);
    }

    public <T extends Expression> SwitchExpression(Map<String, T> cases, Expression defaultExp) {
        super(null);
        this.defaultExp = defaultExp;
        this.cases.putAll(cases);
    }

    @Override
    public SwitchExpression convertChildren(ExpressionConverter converter) {
        var convertedCases = new LinkedHashMap<String, Expression>();
        for (var entry : cases.entrySet()) {
            var converted = converter.branch().convert(entry.getValue());
            if (converted != null)
                convertedCases.put(entry.getKey(), converted);
        }
        return new SwitchExpression(convertedCases, converter.branch().convert(defaultExp));
    }

    public boolean isEmpty() {
        return defaultExp == null && cases.isEmpty();
    }

    public Map<String, Expression> getCases() {
        return Collections.unmodifiableMap(cases);
    }

    public Expression getDefaultExpression() {
        return defaultExp;
    }

    @Override
    public void setStatementOutput(DocumentType documentType, Field field) {
        defaultExp.setStatementOutput(documentType, field);
        for (var expression : cases.values())
            expression.setStatementOutput(documentType, field);
    }

    @Override
    protected void doExecute(ExecutionContext context) {
        FieldValue input = context.getValue();
        Expression exp = null;
        if (input != null) {
            if (!(input instanceof StringFieldValue)) {
                throw new IllegalArgumentException("Expected " + DataType.STRING.getName() + " input, got " +
                                                   input.getDataType().getName());
            }
            exp = cases.get(String.valueOf(input));
        }
        if (exp == null) {
            exp = defaultExp;
        }
        if (exp != null) {
            exp.execute(context);
        }
        context.setValue(input);
    }

    @Override
    public void selectMembers(ObjectPredicate predicate, ObjectOperation operation) {
        select(defaultExp, predicate, operation);
        for (Expression exp : cases.values()) {
            select(exp, predicate, operation);
        }
    }

    @Override
    protected void doVerify(VerificationContext context) {
        DataType input = context.getValueType();
        if (input == null) {
            throw new VerificationException(this, "Expected " + DataType.STRING.getName() + " input, but no input is specified");
        }
        if (input != DataType.STRING) {
            throw new VerificationException(this, "Expected " + DataType.STRING.getName() + " input, got " +
                                                  input.getName());
        }
        for (Expression exp : cases.values()) {
            context.setValueType(input).execute(exp);
        }
        context.setValueType(input).execute(defaultExp);
        context.setValueType(input);
    }

    @Override
    public DataType createdOutputType() {
        return null;
    }

    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder();
        ret.append("switch { ");
        for (Map.Entry<String, Expression> entry : cases.entrySet()) {
            ret.append("case \"").append(StringUtilities.escape(entry.getKey(), '"')).append("\": ");
            Expression exp = entry.getValue();
            ret.append(exp).append("; ");
        }
        if (defaultExp != null) {
            ret.append("default: ").append(defaultExp).append("; ");
        }
        ret.append("}");
        return ret.toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof SwitchExpression rhs)) return false;
        if (!cases.equals(rhs.cases)) return false;
        if (!equals(defaultExp, rhs.defaultExp)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        return getClass().hashCode() + (defaultExp != null ? defaultExp.hashCode() : 0);
    }

}