KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > api > jms > selector > syntax > DefaultExpressionFactory


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.api.jms.selector.syntax;
47
48 import java.util.HashSet JavaDoc;
49
50 import org.mr.api.jms.selector.parser.SelectorTokenTypes;
51
52
53
54 /**
55  * Default implementation of {@link ExpressionFactory}
56  *
57  */

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

70     public Expression binaryOperator(final int operator, final Expression left,final Expression right) throws SelectorException {
71
72         Expression result = null;
73         
74         switch (operator) {
75             case SelectorTokenTypes.LITERAL_and:
76                 result = new And(left, right);
77                 break;
78             case SelectorTokenTypes.LITERAL_or:
79                 result = new Or(left, right);
80                 break;
81             case SelectorTokenTypes.EQUAL:
82                 result = new Equal(left, right);
83                 break;
84             case SelectorTokenTypes.NOT_EQUAL:
85                 result = new NotEqual(left, right);
86                 break;
87             case SelectorTokenTypes.LT:
88                 result = new Less(left, right);
89                 break;
90             case SelectorTokenTypes.GT:
91                 result = new Greater(left, right);
92                 break;
93             case SelectorTokenTypes.LE:
94                 result = new LessEqual(left, right);
95                 break;
96             case SelectorTokenTypes.GE:
97                 result = new GreaterEqual(left, right);
98                 break;
99             case SelectorTokenTypes.PLUS:
100                 result = new Add(left, right);
101                 break;
102             case SelectorTokenTypes.MINUS:
103                 result = new Subtract(left, right);
104                 break;
105             case SelectorTokenTypes.MULTIPLY:
106                 result = new Multiply(left, right);
107                 break;
108             case SelectorTokenTypes.DIVIDE:
109                 result = new Divide(left, right);
110                 break;
111             default:
112                 throw new SelectorException("Unknown binary operator type: " + operator);
113         }//switch
114
return result;
115     }//binaryOperator
116

117     
118     /**
119      * Create an unary operator expression
120      *
121      * @param operator the operator token type from SelectorTokenTypes
122      * @param operand the expression to apply the operator to
123      * @return a new unary expression
124      * @throws SelectorException if the operator is not a valid unary operator
125      */

126     public Expression unaryOperator(final int operator,final Expression operand) throws SelectorException {
127
128         Expression result = null;
129         
130         switch (operator) {
131             case SelectorTokenTypes.LITERAL_not:
132                 result = new Not(operand);
133                 break;
134             case SelectorTokenTypes.UNARY_MINUS:
135                 result = new UnaryMinus(operand);
136                 break;
137             default:
138                 throw new SelectorException("Unknown unary operator type: " + operator);
139         }//switch
140
return result;
141     }//unaryOperator
142

143     
144     /**
145      * Create an identifier expression
146      *
147      * @param name the name of the identifier
148      * @return a new identifier expression
149      * @throws SelectorException is name is not a valid identifier
150      */

151     public Expression identifier(final String JavaDoc name) throws SelectorException {
152         return new Identifier(name);
153     }//identifier
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     }//isNull
167

168
169     /**
170      * Create a 'like' expression
171      *
172      * @param identifier the identifer to apply the 'like' test to
173      * @param pattern the search pattern
174      * @param escape the escape character. This may be null
175      * @return a new 'like' expression
176      * @throws SelectorException if the pattern or escape is invalid
177      */

178     public Expression like(final Expression identifier, final String JavaDoc pattern,final String JavaDoc escape) throws SelectorException {
179         return new LikeExpression((Identifier) identifier, pattern, escape);
180     }//like
181

182     
183     /**
184      * Create a 'between' expression that returns the result of:<br/>
185      * <code>num1 >= num2 and num1 <= num3</code>
186      * when evaluated
187      *
188      * @param num1 an arithmethic expression
189      * @param num2 an arithmethic expression
190      * @param num3 an arithmethic expression
191      * @return a new 'between' expression
192      * @throws SelectorException for any error
193      */

194     public Expression between(final Expression num1, final Expression num2,final Expression num3) throws SelectorException {
195         return new BetweenExpression(num1, num2, num3);
196     }//between
197

198
199     /**
200      * Create an 'in' expression
201      *
202      * @param identifier string identifer to apply the 'in' test to
203      * @param set the set of string values to compare against
204      * @return a new 'in' expression
205      * @throws SelectorException for any error
206      */

207     public Expression in(final Expression identifier, final HashSet JavaDoc set) throws SelectorException {
208         return new InExpression((Identifier) identifier, set);
209     }//in
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) throws SelectorException {
220
221         Expression result = null;
222         
223         switch (type) {
224             case SelectorTokenTypes.NUM_FLOAT:
225                 result = Literal.approxNumericLiteral(text);
226                 break;
227             case SelectorTokenTypes.NUM_INT:
228                 result = Literal.exactNumericLiteral(text);
229                 break;
230             case SelectorTokenTypes.STRING_LITERAL:
231                 result = Literal.stringLiteral(text);
232                 break;
233             case SelectorTokenTypes.LITERAL_true:
234                 result = Literal.booleanLiteral(true);
235                 break;
236             case SelectorTokenTypes.LITERAL_false:
237                 result = Literal.booleanLiteral(false);
238                 break;
239             default:
240                 throw new SelectorException("Unknown literal type: " + type);
241         }//switch
242

243         return result;
244     }//literal
245
}//DefaultExpressionFactory
246
Popular Tags