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 UnarySelector extends Selector {
040: static L10N L = new L10N(Selector.class);
041:
042: private int _token;
043: private Selector _expr;
044:
045: UnarySelector(int token, Selector expr) {
046: _token = token;
047: _expr = expr;
048: }
049:
050: boolean isBoolean() {
051: switch (_token) {
052: case SelectorParser.NOT:
053: case SelectorParser.NULL:
054: return true;
055: }
056:
057: return false;
058: }
059:
060: boolean isNumber() {
061: switch (_token) {
062: case '-':
063: case '+':
064: return true;
065: }
066:
067: return false;
068: }
069:
070: boolean isUnknown() {
071: return false;
072: }
073:
074: /**
075: * Evaluate the message. The boolean literal selector returns
076: * the value of the boolean.
077: */
078: Object evaluate(Message message) throws JMSException {
079: Object value = _expr.evaluate(message);
080:
081: switch (_token) {
082: case SelectorParser.NOT:
083: if (!(value instanceof Boolean))
084: return NULL;
085: else
086: return toBoolean(!((Boolean) value).booleanValue());
087:
088: case SelectorParser.NULL:
089: return toBoolean(value == null);
090:
091: case '+':
092: if (!(value instanceof Number))
093: return NULL;
094: else
095: return value;
096:
097: case '-':
098: if (!(value instanceof Number))
099: return NULL;
100: else if (isInteger(value))
101: return new Long(-toLong(value));
102: else
103: return new Double(-((Number) value).doubleValue());
104:
105: default:
106: throw new JMSException("NOTONE");
107: }
108: }
109: }
|