KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > selector > DefaultExpressionFactory


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2000-2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42  */

43
44 package org.exolab.jms.selector;
45
46 import java.util.HashSet JavaDoc;
47
48 import org.exolab.jms.selector.parser.SelectorTokenTypes;
49
50
51 /**
52  * Default implementation of {@link ExpressionFactory}
53  *
54  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:50:44 $
55  * @author <a HREF="mailto:tma@netspacce.net.au">Tim Anderson</a>
56  * @see ExpressionFactory
57  * @see SelectorTokenTypes
58  */

59 public final class DefaultExpressionFactory implements ExpressionFactory {
60
61     /**
62      * Create a binary operator expression
63      *
64      * @param operator the operator token type from SelectorTokenTypes
65      * @param left the left-hand side of the binary expression
66      * @param right the right-hand side of the binary expression
67      * @return a new binary expression
68      * @throws SelectorException if the operator is not a valid binary operator
69      */

70     public Expression binaryOperator(final int operator, final Expression left,
71                                      final Expression right)
72         throws SelectorException {
73
74         Expression result = null;
75         switch (operator) {
76             case SelectorTokenTypes.LITERAL_and:
77                 result = new And(left, right);
78                 break;
79             case SelectorTokenTypes.LITERAL_or:
80                 result = new Or(left, right);
81                 break;
82             case SelectorTokenTypes.EQUAL:
83                 result = new Equal(left, right);
84                 break;
85             case SelectorTokenTypes.NOT_EQUAL:
86                 result = new NotEqual(left, right);
87                 break;
88             case SelectorTokenTypes.LT:
89                 result = new Less(left, right);
90                 break;
91             case SelectorTokenTypes.GT:
92                 result = new Greater(left, right);
93                 break;
94             case SelectorTokenTypes.LE:
95                 result = new LessEqual(left, right);
96                 break;
97             case SelectorTokenTypes.GE:
98                 result = new GreaterEqual(left, right);
99                 break;
100             case SelectorTokenTypes.PLUS:
101                 result = new Add(left, right);
102                 break;
103             case SelectorTokenTypes.MINUS:
104                 result = new Subtract(left, right);
105                 break;
106             case SelectorTokenTypes.MULTIPLY:
107                 result = new Multiply(left, right);
108                 break;
109             case SelectorTokenTypes.DIVIDE:
110                 result = new Divide(left, right);
111                 break;
112             default:
113                 throw new SelectorException("Unknown binary operator type: "
114                                             + operator);
115         }
116         return result;
117     }
118
119     /**
120      * Create an unary operator expression
121      *
122      * @param operator the operator token type from SelectorTokenTypes
123      * @param operand the expression to apply the operator to
124      * @return a new unary expression
125      * @throws SelectorException if the operator is not a valid unary operator
126      */

127     public Expression unaryOperator(final int operator,
128                                     final Expression operand)
129         throws SelectorException {
130         Expression result = null;
131         switch (operator) {
132             case SelectorTokenTypes.LITERAL_not:
133                 result = new Not(operand);
134                 break;
135             case SelectorTokenTypes.UNARY_MINUS:
136                 result = new UnaryMinus(operand);
137                 break;
138             default:
139                 throw new SelectorException("Unknown unary operator type: "
140                                             + operator);
141         }
142         return result;
143     }
144
145     /**
146      * Create an identifier expression
147      *
148      * @param name the name of the identifier
149      * @return a new identifier expression
150      * @throws SelectorException is name is not a valid identifier
151      */

152     public Expression identifier(final String JavaDoc name) throws SelectorException {
153         return new Identifier(name);
154     }
155
156     /**
157      * Create an 'is null' expression
158      *
159      * @param identifier the identifer expression to apply the 'is null' test
160      * @return an 'is null' expression
161      * @throws SelectorException for any error
162      */

163     public Expression isNull(final Expression identifier)
164         throws SelectorException {
165         return new IsExpression((Identifier) identifier);
166     }
167
168     /**
169      * Create a 'like' expression
170      *
171      * @param identifier the identifer to apply the 'like' test to
172      * @param pattern the search pattern
173      * @param escape the escape character. This may be null
174      * @return a new 'like' expression
175      * @throws SelectorException if the pattern or escape is invalid
176      */

177     public Expression like(final Expression identifier, final String JavaDoc pattern,
178                            final String JavaDoc escape) throws SelectorException {
179         return new LikeExpression((Identifier) identifier, pattern, escape);
180     }
181
182     /**
183      * Create a 'between' expression that returns the result of:<br/>
184      * <code>num1 >= num2 and num1 <= num3</code>
185      * when evaluated
186      *
187      * @param num1 an arithmethic expression
188      * @param num2 an arithmethic expression
189      * @param num3 an arithmethic expression
190      * @return a new 'between' expression
191      * @throws SelectorException for any error
192      */

193     public Expression between(final Expression num1, final Expression num2,
194                               final Expression num3) throws SelectorException {
195         return new BetweenExpression(num1, num2, num3);
196     }
197
198     /**
199      * Create an 'in' expression
200      *
201      * @param identifier string identifer to apply the 'in' test to
202      * @param set the set of string values to compare against
203      * @return a new 'in' expression
204      * @throws SelectorException for any error
205      */

206     public Expression in(final Expression identifier, final HashSet JavaDoc set)
207         throws SelectorException {
208         return new InExpression((Identifier) identifier, set);
209     }
210
211     /**
212      * Create a literal expression
213      *
214      * @param type the operator token type from SelectorTokenTypes
215      * @param text the literal text
216      * @return a new literal expression
217      * @throws SelectorException if type is not a valid literal type
218      */

219     public Expression literal(final int type, final String JavaDoc text)
220         throws SelectorException {
221         Expression result = null;
222         switch (type) {
223             case SelectorTokenTypes.NUM_FLOAT:
224                 result = Literal.approxNumericLiteral(text);
225                 break;
226             case SelectorTokenTypes.NUM_INT:
227                 result = Literal.exactNumericLiteral(text);
228                 break;
229             case SelectorTokenTypes.STRING_LITERAL:
230                 result = Literal.stringLiteral(text);
231                 break;
232             case SelectorTokenTypes.LITERAL_true:
233                 result = Literal.booleanLiteral(true);
234                 break;
235             case SelectorTokenTypes.LITERAL_false:
236                 result = Literal.booleanLiteral(false);
237                 break;
238             default:
239                 throw new SelectorException("Unknown literal type: " + type);
240         }
241
242         return result;
243     }
244
245 } //-- DefaultExpressionFactory
246
Popular Tags