1 56 57 package org.objectstyle.cayenne.exp.parser; 58 59 import java.io.PrintWriter ; 60 import java.math.BigDecimal ; 61 62 import org.objectstyle.cayenne.exp.Expression; 63 import org.objectstyle.cayenne.util.ConversionUtil; 64 65 71 public class ASTNegate extends SimpleNode { 72 ASTNegate(int id) { 73 super(id); 74 } 75 76 public ASTNegate() { 77 super(ExpressionParserTreeConstants.JJTNEGATE); 78 } 79 80 public ASTNegate(Object node) { 81 super(ExpressionParserTreeConstants.JJTNEGATE); 82 jjtAddChild(wrapChild(node), 0); 83 } 84 85 88 public Expression shallowCopy() { 89 return new ASTNegate(id); 90 } 91 92 protected Object evaluateNode(Object o) throws Exception { 93 int len = jjtGetNumChildren(); 94 if (len == 0) { 95 return null; 96 } 97 98 BigDecimal result = ConversionUtil.toBigDecimal(evaluateChild(0, o)); 99 return result != null ? result.negate() : null; 100 } 101 102 public void encodeAsString(PrintWriter pw) { 103 if ((children != null) && (children.length > 0)) { 104 pw.print("-"); 105 106 SimpleNode child = (SimpleNode) children[0]; 107 108 boolean useParen = 110 parent != null 111 && !((child instanceof ASTScalar) || (child instanceof ASTPath)); 112 if (useParen) { 113 pw.print("("); 114 } 115 116 child.encodeAsString(pw); 117 118 if (useParen) { 119 pw.print(')'); 120 } 121 } 122 } 123 124 protected String getExpressionOperator(int index) { 125 throw new UnsupportedOperationException ( 126 "No operator for '" + ExpressionParserTreeConstants.jjtNodeName[id] + "'"); 127 } 128 129 public int getType() { 130 return Expression.NEGATIVE; 131 } 132 133 public int getOperandCount() { 134 return 1; 135 } 136 } 137 | Popular Tags |