UnaryExpr.java revision d3f2b9229472c9dae9bf4ae8b3e2d653b5653b01
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 UnaryExpr extends Expr {
26    final String mOp;
27    UnaryExpr(String op, Expr expr) {
28        super(expr);
29        mOp = op;
30    }
31
32    @Override
33    public String getInvertibleError() {
34        return getExpr().getInvertibleError();
35    }
36
37    @Override
38    protected String computeUniqueKey() {
39        return addTwoWay(join(getOpStr(), getExpr().getUniqueKey()));
40    }
41
42    @Override
43    public KCode toInverseCode(KCode value) {
44        return getExpr().toInverseCode(new KCode().app(mOp, value));
45    }
46
47    @Override
48    protected KCode generateCode(boolean expand) {
49        return new KCode().app(getOp(), getExpr().toCode(expand));
50    }
51
52    @Override
53    protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
54        return getExpr().getResolvedType();
55    }
56
57    @Override
58    protected List<Dependency> constructDependencies() {
59        return constructDynamicChildrenDependencies();
60    }
61
62    private String getOpStr() {
63        switch (mOp.charAt(0)) {
64            case '~' : return "bitNot";
65            case '!' : return "logicalNot";
66            case '-' : return "unaryMinus";
67            case '+' : return "unaryPlus";
68        }
69        return mOp;
70    }
71
72    public String getOp() {
73        return mOp;
74    }
75
76    public Expr getExpr() {
77        return getChildren().get(0);
78    }
79}
80