1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.databinding.tool.expr;
18
19import android.databinding.tool.reflection.ModelAnalyzer;
20import android.databinding.tool.reflection.ModelClass;
21import android.databinding.tool.writer.KCode;
22
23import java.util.List;
24
25public class ComparisonExpr extends Expr {
26    final String mOp;
27    ComparisonExpr(String op, Expr left, Expr right) {
28        super(left, right);
29        mOp = op;
30    }
31
32    @Override
33    protected String computeUniqueKey() {
34        return join(mOp, super.computeUniqueKey());
35    }
36
37    @Override
38    protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
39        return modelAnalyzer.loadPrimitive("boolean");
40    }
41
42    @Override
43    protected List<Dependency> constructDependencies() {
44        return constructDynamicChildrenDependencies();
45    }
46
47    public String getOp() {
48        return mOp;
49    }
50
51    public Expr getLeft() {
52        return getChildren().get(0);
53    }
54
55    public Expr getRight() {
56        return getChildren().get(1);
57    }
58
59    @Override
60    public boolean isEqualityCheck() {
61        return "==".equals(mOp.trim());
62    }
63
64    @Override
65    protected KCode generateCode(boolean expand) {
66        return new KCode().app("", getLeft().toCode(expand))
67        .app(" ").app(getOp()).app(" ")
68        .app("", getRight().toCode(expand));
69    }
70
71    @Override
72    public String getInvertibleError() {
73        return "Comparison operators are not valid as targets of two-way binding";
74    }
75}
76