001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jms.selector;
030:
031: import com.caucho.util.L10N;
032:
033: import javax.jms.JMSException;
034: import javax.jms.Message;
035:
036: /**
037: * The base selector.
038: */
039: public class NumericBinarySelector extends Selector {
040: static L10N L = new L10N(Selector.class);
041:
042: private int _token;
043: private Selector _left;
044: private Selector _right;
045:
046: NumericBinarySelector(int token, Selector left, Selector right) {
047: _token = token;
048: _left = left;
049: _right = right;
050: }
051:
052: /**
053: * Evaluate the message. The boolean literal selector returns
054: * the value of the boolean.
055: */
056: Object evaluate(Message message) throws JMSException {
057: Object lobj = _left.evaluate(message);
058: Object robj = _right.evaluate(message);
059:
060: if (!(lobj instanceof Number) || !(robj instanceof Number))
061: return NULL;
062:
063: if (isInteger(lobj) && isInteger(robj)) {
064: long lvalue = toLong(lobj);
065: long rvalue = toLong(robj);
066:
067: switch (_token) {
068: case '+':
069: return new Long(lvalue + rvalue);
070:
071: case '-':
072: return new Long(lvalue - rvalue);
073:
074: case '*':
075: return new Long(lvalue * rvalue);
076:
077: case '/':
078: return new Long(lvalue / rvalue);
079:
080: default:
081: throw new RuntimeException("Unknown expression");
082: }
083: } else {
084: double lvalue = ((Number) lobj).doubleValue();
085: double rvalue = ((Number) robj).doubleValue();
086:
087: switch (_token) {
088: case '+':
089: return new Double(lvalue + rvalue);
090:
091: case '-':
092: return new Double(lvalue - rvalue);
093:
094: case '*':
095: return new Double(lvalue * rvalue);
096:
097: case '/':
098: return new Double(lvalue / rvalue);
099:
100: default:
101: throw new RuntimeException("Unknown expression");
102: }
103: }
104: }
105: }
|