1 52 53 package freemarker.core; 54 55 import freemarker.template.*; 56 57 final class UnaryPlusMinusExpression extends Expression { 58 59 private final Expression target; 60 private final boolean isMinus; 61 private static final Integer MINUS_ONE = new Integer (-1); 62 63 UnaryPlusMinusExpression(Expression target, boolean isMinus) { 64 this.target = target; 65 this.isMinus = isMinus; 66 } 67 68 TemplateModel _getAsTemplateModel(Environment env) throws TemplateException { 69 TemplateNumberModel targetModel = null; 70 try { 71 targetModel = (TemplateNumberModel) target.getAsTemplateModel(env); 72 } catch (ClassCastException cce) { 73 String msg = "Error " + getStartLocation(); 74 msg += "\nExpression " + target + " is not numerical."; 75 throw new NonNumericalException(msg, env); 76 } 77 if (!isMinus) { 78 return targetModel; 79 } 80 Number n = targetModel.getAsNumber(); 81 n = ArithmeticEngine.CONSERVATIVE_ENGINE.multiply(MINUS_ONE, n); 82 return new SimpleNumber(n); 83 } 84 85 public String getCanonicalForm() { 86 String op = isMinus ? "-" : "+"; 87 return op + target.getCanonicalForm(); 88 } 89 90 boolean isLiteral() { 91 return target.isLiteral(); 92 } 93 94 Expression _deepClone(String name, Expression subst) { 95 return new UnaryPlusMinusExpression(target.deepClone(name, subst), isMinus); 96 } 97 } 98 | Popular Tags |