1 56 package org.objectstyle.cayenne.exp.parser; 57 58 import java.math.BigDecimal ; 59 import java.util.Collection ; 60 import java.util.Iterator ; 61 62 import org.objectstyle.cayenne.exp.Expression; 63 import org.objectstyle.cayenne.util.ConversionUtil; 64 65 71 public class ASTDivide extends SimpleNode { 72 ASTDivide(int id) { 73 super(id); 74 } 75 76 public ASTDivide() { 77 super(ExpressionParserTreeConstants.JJTDIVIDE); 78 } 79 80 public ASTDivide(Object [] nodes) { 81 super(ExpressionParserTreeConstants.JJTDIVIDE); 82 int len = nodes.length; 83 for (int i = 0; i < len; i++) { 84 jjtAddChild(wrapChild(nodes[i]), i); 85 } 86 } 87 88 public ASTDivide(Collection nodes) { 89 super(ExpressionParserTreeConstants.JJTDIVIDE); 90 int len = nodes.size(); 91 Iterator it = nodes.iterator(); 92 for (int i = 0; i < len; i++) { 93 jjtAddChild(wrapChild(it.next()), i); 94 } 95 } 96 97 protected Object evaluateNode(Object o) throws Exception { 98 int len = jjtGetNumChildren(); 99 if (len == 0) { 100 return null; 101 } 102 103 BigDecimal result = null; 104 for (int i = 0; i < len; i++) { 105 BigDecimal value = ConversionUtil.toBigDecimal(evaluateChild(i, o)); 106 107 if (value == null) { 108 return null; 109 } 110 111 result = (i == 0) ? value : result.divide(value, BigDecimal.ROUND_HALF_EVEN); 112 } 113 114 return result; 115 } 116 117 120 public Expression shallowCopy() { 121 return new ASTGreater(id); 122 } 123 124 protected String getExpressionOperator(int index) { 125 return "/"; 126 } 127 128 public int getType() { 129 return Expression.DIVIDE; 130 } 131 132 public void jjtClose() { 133 super.jjtClose(); 134 flattenTree(); 135 } 136 } 137 | Popular Tags |