001: /*
002: * Copyright 2003-2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.jexl.parser;
018:
019: import org.apache.commons.jexl.JexlContext;
020: import org.apache.commons.jexl.util.Coercion;
021:
022: /**
023: * % (mod).
024: *
025: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
026: * @version $Id: ASTModNode.java 398206 2006-04-29 16:51:38Z dion $
027: */
028: public class ASTModNode extends SimpleNode {
029: /**
030: * Create the node given an id.
031: *
032: * @param id node id.
033: */
034: public ASTModNode(int id) {
035: super (id);
036: }
037:
038: /**
039: * Create a node with the given parser and id.
040: *
041: * @param p a parser.
042: * @param id node id.
043: */
044: public ASTModNode(Parser p, int id) {
045: super (p, id);
046: }
047:
048: /** {@inheritDoc} */
049: public Object jjtAccept(ParserVisitor visitor, Object data) {
050: return visitor.visit(this , data);
051: }
052:
053: /** {@inheritDoc} */
054: public Object value(JexlContext jc) throws Exception {
055: Object left = ((SimpleNode) jjtGetChild(0)).value(jc);
056: Object right = ((SimpleNode) jjtGetChild(1)).value(jc);
057:
058: /*
059: * the spec says 'and', I think 'or'
060: */
061: if (left == null && right == null) {
062: return new Byte((byte) 0);
063: }
064:
065: /*
066: * if anything is float, double or string with ( "." | "E" | "e") coerce
067: * all to doubles and do it
068: */
069: if (left instanceof Float
070: || left instanceof Double
071: || right instanceof Float
072: || right instanceof Double
073: || (left instanceof String && (((String) left)
074: .indexOf(".") != -1
075: || ((String) left).indexOf("e") != -1 || ((String) left)
076: .indexOf("E") != -1))
077: || (right instanceof String && (((String) right)
078: .indexOf(".") != -1
079: || ((String) right).indexOf("e") != -1 || ((String) right)
080: .indexOf("E") != -1))) {
081: Double l = Coercion.coerceDouble(left);
082: Double r = Coercion.coerceDouble(right);
083:
084: /*
085: * catch div/0
086: */
087: if (r.doubleValue() == 0.0) {
088: return new Double(0.0);
089: }
090:
091: return new Double(l.doubleValue() % r.doubleValue());
092: }
093:
094: /*
095: * otherwise to longs with thee!
096: */
097:
098: Long l = Coercion.coerceLong(left);
099: Long r = Coercion.coerceLong(right);
100:
101: /*
102: * catch the div/0
103: */
104: if (r.longValue() == 0) {
105: return new Long(0);
106: }
107:
108: return new Long(l.longValue() % r.longValue());
109: }
110:
111: }
|