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 17 package android.databinding.tool.expr; 18 19 import android.databinding.tool.reflection.ModelAnalyzer; 20 import android.databinding.tool.reflection.ModelClass; 21 import android.databinding.tool.writer.KCode; 22 23 import java.util.List; 24 25 public 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() { 66 return new KCode() 67 .app("(", getLeft().toCode()) 68 .app(") ") 69 .app(getOp()) 70 .app(" (", getRight().toCode()) 71 .app(")"); 72 } 73 74 @Override 75 public Expr cloneToModel(ExprModel model) { 76 return model.comparison(mOp, getLeft().cloneToModel(model), getRight().cloneToModel(model)); 77 } 78 79 @Override 80 public String getInvertibleError() { 81 return "Comparison operators are not valid as targets of two-way binding"; 82 } 83 84 @Override 85 public String toString() { 86 return getLeft().toString() + ' ' + mOp + ' ' + getRight(); 87 } 88 } 89