MathExpr.java revision e52882df6130221462bf07f5f2b52de5c4b0f8de
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 MathExpr extends Expr {
26    final String mOp;
27    MathExpr(Expr left, String op, Expr right) {
28        super(left, right);
29        mOp = op;
30    }
31
32    @Override
33    protected String computeUniqueKey() {
34        return join(getLeft().getUniqueKey(), mOp, getRight().getUniqueKey());
35    }
36
37    @Override
38    protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
39        if ("+".equals(mOp)) {
40            // TODO we need upper casting etc.
41            if (getLeft().getResolvedType().isString()
42                    || getRight().getResolvedType().isString()) {
43                return modelAnalyzer.findClass(String.class);
44            }
45        }
46        return modelAnalyzer.findCommonParentOf(getLeft().getResolvedType(),
47                getRight().getResolvedType());
48    }
49
50    @Override
51    protected List<Dependency> constructDependencies() {
52        return constructDynamicChildrenDependencies();
53    }
54
55    public String getOp() {
56        return mOp;
57    }
58
59    public Expr getLeft() {
60        return getChildren().get(0);
61    }
62
63    public Expr getRight() {
64        return getChildren().get(1);
65    }
66
67    @Override
68    protected KCode generateCode() {
69        return new KCode().app("", getLeft().toCode()).app(mOp, getRight().toCode());
70    }
71}
72