KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > selector > BooleanBinarySelector


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jms.selector;
30
31 import javax.jms.InvalidSelectorException JavaDoc;
32 import javax.jms.JMSException JavaDoc;
33 import javax.jms.Message JavaDoc;
34
35 /**
36  * The base selector.
37  */

38 public class BooleanBinarySelector extends Selector {
39   private int _token;
40   private Selector _left;
41   private Selector _right;
42
43   BooleanBinarySelector(int token, Selector left, Selector right)
44     throws JMSException JavaDoc
45   {
46     _token = token;
47     _left = left;
48     _right = right;
49
50     switch (_token) {
51     case SelectorParser.LT:
52     case SelectorParser.LE:
53     case SelectorParser.GT:
54     case SelectorParser.GE:
55       if (! _left.isUnknown() && ! _left.isNumber())
56     throw new InvalidSelectorException JavaDoc(L.l("'{0}' must have a numeric value for comparison.",
57                            this));
58       if (! _right.isUnknown() && ! _right.isNumber())
59     throw new InvalidSelectorException JavaDoc(L.l("'{0}' must have a numeric value for comparison.",
60                            this));
61       break;
62     case SelectorParser.EQ:
63     case SelectorParser.NE:
64       if (_left.isUnknown() || _right.isUnknown()) {
65       }
66       else if (_left.isNumber() != _right.isNumber())
67     throw new InvalidSelectorException JavaDoc(L.l("'{0}' test must have matching types.",
68                            this));
69       break;
70       
71     case SelectorParser.AND:
72     case SelectorParser.OR:
73       if (! _left.isUnknown() && ! _left.isBoolean())
74     throw new InvalidSelectorException JavaDoc(L.l("'{0}' must have a numeric value for comparison.",
75                            this));
76       if (! _right.isUnknown() && ! _right.isBoolean())
77     throw new InvalidSelectorException JavaDoc(L.l("'{0}' must have a numeric value for comparison.",
78                            this));
79       break;
80     }
81   }
82
83   /**
84    * Returns true since the value is a boolean.
85    */

86   boolean isBoolean()
87   {
88     return true;
89   }
90
91   /**
92    * Returns false, since the type is known.
93    */

94   boolean isUnknown()
95   {
96     return false;
97   }
98
99   /**
100    * Evaluate the message. The boolean literal selector returns
101    * the value of the boolean.
102    */

103   public Object JavaDoc evaluate(Message JavaDoc message)
104     throws JMSException JavaDoc
105   {
106     Object JavaDoc lvalue = _left.evaluate(message);
107     Object JavaDoc rvalue = _right.evaluate(message);
108
109     if (lvalue == null || rvalue == null)
110       return NULL;
111
112     switch (_token) {
113     case SelectorParser.EQ:
114       if (lvalue instanceof Number JavaDoc && rvalue instanceof Number JavaDoc)
115         return toBoolean(((Number JavaDoc) lvalue).doubleValue() ==
116                          ((Number JavaDoc) rvalue).doubleValue());
117       else
118         return toBoolean(lvalue.equals(rvalue));
119       
120     case SelectorParser.NE:
121       if (lvalue instanceof Number JavaDoc && rvalue instanceof Number JavaDoc)
122         return toBoolean(! (((Number JavaDoc) lvalue).doubleValue() ==
123                 ((Number JavaDoc) rvalue).doubleValue()));
124       else
125         return toBoolean(! lvalue.equals(rvalue));
126       
127     case SelectorParser.LT:
128       if (lvalue instanceof Number JavaDoc && rvalue instanceof Number JavaDoc)
129         return toBoolean(((Number JavaDoc) lvalue).doubleValue() <
130              ((Number JavaDoc) rvalue).doubleValue());
131       else
132         return Boolean.FALSE;
133         
134     case SelectorParser.LE:
135       if (lvalue instanceof Number JavaDoc && rvalue instanceof Number JavaDoc)
136         return toBoolean(((Number JavaDoc) lvalue).doubleValue() <=
137              ((Number JavaDoc) rvalue).doubleValue());
138       else
139         return Boolean.FALSE;
140         
141     case SelectorParser.GT:
142       if (lvalue instanceof Number JavaDoc && rvalue instanceof Number JavaDoc)
143         return toBoolean(((Number JavaDoc) lvalue).doubleValue() >
144              ((Number JavaDoc) rvalue).doubleValue());
145       else
146         return Boolean.FALSE;
147         
148     case SelectorParser.GE:
149       if (lvalue instanceof Number JavaDoc && rvalue instanceof Number JavaDoc)
150         return toBoolean(((Number JavaDoc) lvalue).doubleValue() >=
151              ((Number JavaDoc) rvalue).doubleValue());
152       else
153         return Boolean.FALSE;
154         
155     case SelectorParser.AND:
156       if (! (lvalue instanceof Boolean JavaDoc))
157         return NULL;
158       else if (! ((Boolean JavaDoc) lvalue).booleanValue())
159         return Boolean.FALSE;
160       else if (! (rvalue instanceof Boolean JavaDoc))
161         return NULL;
162       else if (! ((Boolean JavaDoc) rvalue).booleanValue())
163         return Boolean.FALSE;
164       else
165         return Boolean.TRUE;
166         
167     case SelectorParser.OR:
168       if ((lvalue instanceof Boolean JavaDoc) && ((Boolean JavaDoc) lvalue).booleanValue())
169         return Boolean.TRUE;
170       else if ((rvalue instanceof Boolean JavaDoc) &&
171                ((Boolean JavaDoc) rvalue).booleanValue())
172         return Boolean.TRUE;
173       else if (lvalue instanceof Boolean JavaDoc || rvalue instanceof Boolean JavaDoc)
174         return Boolean.FALSE;
175       else
176         return NULL;
177       
178     default:
179       throw new JMSException JavaDoc("NOTONE");
180     }
181   }
182
183   public String JavaDoc toString()
184   {
185     switch (_token) {
186     case SelectorParser.EQ:
187       return _left + " = " + _right;
188     case SelectorParser.NE:
189       return _left + " <> " + _right;
190     case SelectorParser.LT:
191       return _left + " < " + _right;
192     case SelectorParser.LE:
193       return _left + " <= " + _right;
194     case SelectorParser.GT:
195       return _left + " > " + _right;
196     case SelectorParser.GE:
197       return _left + " >= " + _right;
198     case SelectorParser.AND:
199       return _left + " AND " + _right;
200     case SelectorParser.OR:
201       return _left + " OR " + _right;
202     default:
203       return super.toString();
204     }
205   }
206 }
207
Popular Tags