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 javax.jms.InvalidSelectorException;
032: import javax.jms.JMSException;
033: import javax.jms.Message;
034:
035: /**
036: * The base selector.
037: */
038: public class BooleanBinarySelector extends Selector {
039: private int _token;
040: private Selector _left;
041: private Selector _right;
042:
043: BooleanBinarySelector(int token, Selector left, Selector right)
044: throws JMSException {
045: _token = token;
046: _left = left;
047: _right = right;
048:
049: switch (_token) {
050: case SelectorParser.LT:
051: case SelectorParser.LE:
052: case SelectorParser.GT:
053: case SelectorParser.GE:
054: if (!_left.isUnknown() && !_left.isNumber())
055: throw new InvalidSelectorException(
056: L
057: .l(
058: "'{0}' must have a numeric value for comparison.",
059: this ));
060: if (!_right.isUnknown() && !_right.isNumber())
061: throw new InvalidSelectorException(
062: L
063: .l(
064: "'{0}' must have a numeric value for comparison.",
065: this ));
066: break;
067: case SelectorParser.EQ:
068: case SelectorParser.NE:
069: if (_left.isUnknown() || _right.isUnknown()) {
070: } else if (_left.isNumber() != _right.isNumber()) {
071: throw new InvalidSelectorException(L.l(
072: "'{0}' test must have matching types.", this ));
073: }
074: break;
075:
076: case SelectorParser.AND:
077: case SelectorParser.OR:
078: if (!_left.isUnknown() && !_left.isBoolean())
079: throw new InvalidSelectorException(
080: L
081: .l(
082: "'{0}' must have a numeric value for comparison.",
083: this ));
084: if (!_right.isUnknown() && !_right.isBoolean())
085: throw new InvalidSelectorException(
086: L
087: .l(
088: "'{0}' must have a numeric value for comparison.",
089: this ));
090: break;
091: }
092: }
093:
094: /**
095: * Returns true since the value is a boolean.
096: */
097: boolean isBoolean() {
098: return true;
099: }
100:
101: /**
102: * Returns false, since the type is known.
103: */
104: boolean isUnknown() {
105: return false;
106: }
107:
108: /**
109: * Evaluate the message. The boolean literal selector returns
110: * the value of the boolean.
111: */
112: public Object evaluate(Message message) throws JMSException {
113: Object lvalue = _left.evaluate(message);
114: Object rvalue = _right.evaluate(message);
115:
116: if (lvalue == null || rvalue == null)
117: return NULL;
118:
119: switch (_token) {
120: case SelectorParser.EQ:
121: if (lvalue instanceof Number && rvalue instanceof Number)
122: return toBoolean(((Number) lvalue).doubleValue() == ((Number) rvalue)
123: .doubleValue());
124: else
125: return toBoolean(lvalue.equals(rvalue));
126:
127: case SelectorParser.NE:
128: if (lvalue instanceof Number && rvalue instanceof Number)
129: return toBoolean(!(((Number) lvalue).doubleValue() == ((Number) rvalue)
130: .doubleValue()));
131: else
132: return toBoolean(!lvalue.equals(rvalue));
133:
134: case SelectorParser.LT:
135: if (lvalue instanceof Number && rvalue instanceof Number)
136: return toBoolean(((Number) lvalue).doubleValue() < ((Number) rvalue)
137: .doubleValue());
138: else
139: return Boolean.FALSE;
140:
141: case SelectorParser.LE:
142: if (lvalue instanceof Number && rvalue instanceof Number)
143: return toBoolean(((Number) lvalue).doubleValue() <= ((Number) rvalue)
144: .doubleValue());
145: else
146: return Boolean.FALSE;
147:
148: case SelectorParser.GT:
149: if (lvalue instanceof Number && rvalue instanceof Number)
150: return toBoolean(((Number) lvalue).doubleValue() > ((Number) rvalue)
151: .doubleValue());
152: else
153: return Boolean.FALSE;
154:
155: case SelectorParser.GE:
156: if (lvalue instanceof Number && rvalue instanceof Number)
157: return toBoolean(((Number) lvalue).doubleValue() >= ((Number) rvalue)
158: .doubleValue());
159: else
160: return Boolean.FALSE;
161:
162: case SelectorParser.AND:
163: if (!(lvalue instanceof Boolean))
164: return NULL;
165: else if (!((Boolean) lvalue).booleanValue())
166: return Boolean.FALSE;
167: else if (!(rvalue instanceof Boolean))
168: return NULL;
169: else if (!((Boolean) rvalue).booleanValue())
170: return Boolean.FALSE;
171: else
172: return Boolean.TRUE;
173:
174: case SelectorParser.OR:
175: if ((lvalue instanceof Boolean)
176: && ((Boolean) lvalue).booleanValue())
177: return Boolean.TRUE;
178: else if ((rvalue instanceof Boolean)
179: && ((Boolean) rvalue).booleanValue())
180: return Boolean.TRUE;
181: else if (lvalue instanceof Boolean
182: || rvalue instanceof Boolean)
183: return Boolean.FALSE;
184: else
185: return NULL;
186:
187: default:
188: throw new JMSException("NOTONE");
189: }
190: }
191:
192: public String toString() {
193: switch (_token) {
194: case SelectorParser.EQ:
195: return _left + " = " + _right;
196: case SelectorParser.NE:
197: return _left + " <> " + _right;
198: case SelectorParser.LT:
199: return _left + " < " + _right;
200: case SelectorParser.LE:
201: return _left + " <= " + _right;
202: case SelectorParser.GT:
203: return _left + " > " + _right;
204: case SelectorParser.GE:
205: return _left + " >= " + _right;
206: case SelectorParser.AND:
207: return _left + " AND " + _right;
208: case SelectorParser.OR:
209: return _left + " OR " + _right;
210: default:
211: return super.toString();
212: }
213: }
214: }
|