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 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 join(getOpStr(), getExpr().getUniqueKey()); 40 } 41 42 @Override 43 public Expr generateInverse(ExprModel model, Expr value, String bindingClassName) { 44 return model.unary(mOp, getExpr().generateInverse(model, value, bindingClassName)); 45 } 46 47 @Override 48 public Expr cloneToModel(ExprModel model) { 49 return model.unary(mOp, getExpr().cloneToModel(model)); 50 } 51 52 @Override 53 protected KCode generateCode() { 54 return new KCode().app(getOp(), getExpr().toCode()); 55 } 56 57 @Override 58 protected ModelClass resolveType(ModelAnalyzer modelAnalyzer) { 59 return getExpr().getResolvedType(); 60 } 61 62 @Override 63 protected List<Dependency> constructDependencies() { 64 return constructDynamicChildrenDependencies(); 65 } 66 67 private String getOpStr() { 68 switch (mOp.charAt(0)) { 69 case '~' : return "bitNot"; 70 case '!' : return "logicalNot"; 71 case '-' : return "unaryMinus"; 72 case '+' : return "unaryPlus"; 73 } 74 return mOp; 75 } 76 77 public String getOp() { 78 return mOp; 79 } 80 81 public Expr getExpr() { 82 return getChildren().get(0); 83 } 84 85 @Override 86 public String toString() { 87 return mOp + getExpr(); 88 } 89 } 90