BracketExpr.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 BracketExpr extends Expr {
26
27    public static enum BracketAccessor {
28        ARRAY,
29        LIST,
30        MAP,
31    }
32
33    private BracketAccessor mAccessor;
34
35    BracketExpr(Expr target, Expr arg) {
36        super(target, arg);
37    }
38
39    @Override
40    protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
41        ModelClass targetType = getTarget().getResolvedType();
42        if (targetType.isArray()) {
43            mAccessor = BracketAccessor.ARRAY;
44        } else if (targetType.isList()) {
45            mAccessor = BracketAccessor.LIST;
46        } else if (targetType.isMap()) {
47            mAccessor = BracketAccessor.MAP;
48        } else {
49            throw new IllegalArgumentException("Cannot determine variable type used in [] " +
50                    "expression. Cast the value to List, Map, " +
51                    "or array. Type detected: " + targetType.toJavaCode());
52        }
53        return targetType.getComponentType();
54    }
55
56    @Override
57    protected List<Dependency> constructDependencies() {
58        return constructDynamicChildrenDependencies();
59    }
60
61    protected String computeUniqueKey() {
62        return join(getTarget().computeUniqueKey(), "$", getArg().computeUniqueKey(), "$");
63    }
64
65    public Expr getTarget() {
66        return getChildren().get(0);
67    }
68
69    public Expr getArg() {
70        return getChildren().get(1);
71    }
72
73    public BracketAccessor getAccessor() {
74        return mAccessor;
75    }
76
77    public boolean argCastsInteger() {
78        return Object.class.equals(getArg().getResolvedType());
79    }
80
81    @Override
82    protected KCode generateCode() {
83        KCode code = new KCode()
84                .app("", getTarget().toCode());
85        switch (getAccessor()) {
86            case ARRAY:
87                return code.app("[", getArg().toCode()).app("]");
88            case LIST:
89                code.app(".get(");
90                if (argCastsInteger()) {
91                    code.app("(Integer)");
92                }
93                return code.app("", getArg().toCode()).app(")");
94            case MAP:
95                return code.app(".get(", getArg().toCode()).app(")");
96        }
97        throw new IllegalStateException("Invalid BracketAccessor type");
98    }
99}
100