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.solver.ExecutionPath;
22import android.databinding.tool.writer.KCode;
23
24import java.util.ArrayList;
25import java.util.List;
26
27public class SymbolExpr extends Expr {
28    String mText;
29    Class mType;
30
31    SymbolExpr(String text, Class type) {
32        super();
33        mText = text;
34        mType = type;
35    }
36
37    public String getText() {
38        return mText;
39    }
40
41    @Override
42    protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) {
43        return modelAnalyzer.findClass(mType);
44    }
45
46    @Override
47    protected String computeUniqueKey() {
48        return mType.getSimpleName() + mText;
49    }
50
51    @Override
52    public String getInvertibleError() {
53        return "Symbol '" + mText + "' cannot be the target of a two-way binding expression";
54    }
55
56    @Override
57    protected KCode generateCode() {
58        return new KCode(getText());
59    }
60
61    @Override
62    public Expr cloneToModel(ExprModel model) {
63        return model.symbol(mText, mType);
64    }
65
66    @Override
67    protected List<Dependency> constructDependencies() {
68        return new ArrayList<Dependency>();
69    }
70
71    @Override
72    public boolean canBeEvaluatedToAVariable() {
73        return !void.class.equals(mType);
74    }
75
76    @Override
77    public List<ExecutionPath> toExecutionPath(List<ExecutionPath> paths) {
78        if (void.class.equals(mType)) {
79            return paths;
80        }
81        return super.toExecutionPath(paths);
82    }
83
84    @Override
85    public String toString() {
86        return mText;
87    }
88}
89