KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > expressions > ExpressionMath


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.expressions;
23
24 import oracle.toplink.essentials.internal.helper.ClassConstants;
25
26 /**
27  * <p>
28  * <b>Purpose</b>: This class mirrors the java.lang.Math class to allow mathimetical function support within expressions.</p>
29  * <p>Example:
30  * <pre><blockquote>
31  * ExpressionBuilder builder = new ExpressionBuilder();
32  * Expression poorAndRich = ExpressionMath.abs(builder.get("netWorth")).greaterThan(1000000);
33  * session.readAllObjects(Company.class, poorAndRich);
34  * </blockquote></pre></p>
35  */

36 public class ExpressionMath {
37
38     /**
39      * PUBLIC:
40      * Return a new expression that aplies the function to the given expression.
41      * <p>Example:
42      * <pre><blockquote>
43      * Example: ExpressionMath.abs(builder.get("netWorth")).greaterThan(1000000);
44      * </blockquote></pre>
45      */

46     public static Expression abs(Expression expression) {
47         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Abs);
48         return anOperator.expressionFor(expression);
49     }
50
51     /**
52      * PUBLIC:
53      * Return a new expression that aplies the function to the given expression.
54      */

55     public static Expression acos(Expression expression) {
56         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Acos);
57         return anOperator.expressionFor(expression);
58     }
59
60     /**
61      * PUBLIC:
62      * Return a new expression that aplies the function to the given expression.
63      */

64     public static Expression add(Expression left, int right) {
65         return add(left, new Integer JavaDoc(right));
66     }
67
68     /**
69      * PUBLIC:
70      * Return a new expression that aplies the function to the given expression.
71      */

72     public static Expression add(Expression right, Object JavaDoc left) {
73         ExpressionOperator anOperator = right.getOperator(ExpressionOperator.Add);
74         return anOperator.expressionFor(right, left);
75     }
76
77     /**
78      * PUBLIC:
79      * Return a new expression that aplies the function to the given expression.
80      */

81     public static Expression asin(Expression expression) {
82         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Asin);
83         return anOperator.expressionFor(expression);
84     }
85
86     /**
87      * PUBLIC:
88      * Return a new expression that aplies the function to the given expression.
89      */

90     public static Expression atan(Expression expression) {
91         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Atan);
92         return anOperator.expressionFor(expression);
93     }
94
95     /**
96      * PUBLIC:
97      * Return a new expression that applies the function to the given expression.
98      */

99     public static Expression atan2(Expression expression, int value) {
100         return atan2(expression, new Integer JavaDoc(value));
101     }
102
103     /**
104      * PUBLIC:
105      * Return a new expression that applies the function to the given expression.
106      */

107     public static Expression atan2(Expression expression, Object JavaDoc value) {
108         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Atan2);
109         return anOperator.expressionFor(expression, value);
110     }
111
112     /**
113      * PUBLIC:
114      * Return a new expression that applies the function to the given expression.
115      */

116     public static Expression atan2(Expression expression1, Expression expression2) {
117         ExpressionOperator anOperator = expression1.getOperator(ExpressionOperator.Atan2);
118         return anOperator.expressionFor(expression1, expression2);
119     }
120
121     /**
122      * PUBLIC:
123      * Return a new expression that applies the function to the given expression.
124      */

125     public static Expression ceil(Expression expression) {
126         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Ceil);
127         return anOperator.expressionFor(expression);
128     }
129
130     /**
131      * PUBLIC:
132      * Return a new expression that applies the function to the given expression.
133      */

134     public static Expression chr(Expression expression) {
135         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Chr);
136         return anOperator.expressionFor(expression);
137     }
138
139     /**
140      * PUBLIC:
141      * Return a new expression that applies the function to the given expression.
142      */

143     public static Expression cos(Expression expression) {
144         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Cos);
145         return anOperator.expressionFor(expression);
146     }
147
148     /**
149      * PUBLIC:
150      * Return a new expression that applies the function to the given expression.
151
152      */

153     public static Expression cosh(Expression expression) {
154         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Cosh);
155         return anOperator.expressionFor(expression);
156     }
157
158     /**
159      * PUBLIC:
160      * Return a new expression that applies the function to the given expression.
161      */

162     public static Expression cot(Expression expression) {
163         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Cot);
164         return anOperator.expressionFor(expression);
165     }
166
167     /**
168      * PUBLIC:
169      * Return a new expression that aplies the function to the given expression.
170      */

171     public static Expression divide(Expression left, int right) {
172         return divide(left, new Integer JavaDoc(right));
173     }
174
175     /**
176      * PUBLIC:
177      * Return a new expression that applies the function to the given expression.
178      */

179     public static Expression divide(Expression left, Object JavaDoc right) {
180         ExpressionOperator anOperator = left.getOperator(ExpressionOperator.Divide);
181         return anOperator.expressionFor(left, right);
182     }
183
184     /**
185      * PUBLIC:
186      * Return a new expression that applies the function to the given expression.
187      */

188     public static Expression exp(Expression expression) {
189         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Exp);
190         return anOperator.expressionFor(expression);
191     }
192
193     /**
194      * PUBLIC:
195      * Return a new expression that applies the function to the given expression.
196      */

197     public static Expression floor(Expression expression) {
198         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Floor);
199         return anOperator.expressionFor(expression);
200     }
201
202     /**
203      * INTERNAL:
204      * Return the operator.
205      */

206     public static ExpressionOperator getOperator(int selector) {
207         ExpressionOperator result = ExpressionOperator.getOperator(new Integer JavaDoc(selector));
208         if (result != null) {
209             return result;
210         }
211
212         // Make a temporary operator which we expect the platform
213
// to supply later.
214
result = new ExpressionOperator();
215         result.setSelector(selector);
216         result.setNodeClass(ClassConstants.FunctionExpression_Class);
217         return result;
218     }
219
220     /**
221      * PUBLIC:
222      * Return a new expression that applies the function to the given expression.
223      */

224     public static Expression ln(Expression expression) {
225         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Ln);
226         return anOperator.expressionFor(expression);
227     }
228
229     /**
230      * PUBLIC:
231      * Return a new expression that applies the function to the given expression.
232      */

233     public static Expression log(Expression expression) {
234         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Log);
235         return anOperator.expressionFor(expression);
236     }
237
238     /**
239      * PUBLIC:
240      * Return a new expression that applies the function to the given expression.
241      */

242     public static Expression max(Expression left, int right) {
243         return max(left, new Integer JavaDoc(right));
244     }
245
246     /**
247      * PUBLIC:
248      * Return a new expression that applies the function to the given expression.
249      */

250     public static Expression max(Expression left, Object JavaDoc right) {
251         ExpressionOperator anOperator = left.getOperator(ExpressionOperator.Greatest);
252         return anOperator.expressionFor(left, right);
253     }
254
255     /**
256      * PUBLIC:
257      * Return a new expression that applies the function to the given expression.
258      */

259     public static Expression min(Expression left, int right) {
260         return min(left, new Integer JavaDoc(right));
261     }
262
263     /**
264      * PUBLIC:
265      * Return a new expression that applies the function to the given expression.
266      */

267     public static Expression min(Expression left, Object JavaDoc right) {
268         ExpressionOperator anOperator = left.getOperator(ExpressionOperator.Least);
269         return anOperator.expressionFor(left, right);
270     }
271
272     /**
273      * PUBLIC:
274      * Return a new expression that applies the function to the given expression.
275      */

276     public static Expression mod(Expression expression, int base) {
277         return mod(expression, new Integer JavaDoc(base));
278     }
279
280     /**
281      * PUBLIC:
282      * Return a new expression that applies the function to the given expression.
283      */

284     public static Expression mod(Expression expression, Object JavaDoc base) {
285         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Mod);
286         return anOperator.expressionFor(expression, base);
287     }
288
289     /**
290      * PUBLIC:
291      * Return a new expression that applies the function to the given expression.
292      */

293     public static Expression multiply(Expression left, int right) {
294         return multiply(left, new Integer JavaDoc(right));
295     }
296
297     /**
298      * PUBLIC:
299      * Return a new expression that applies the function to the given expression.
300      */

301     public static Expression multiply(Expression left, Object JavaDoc right) {
302         ExpressionOperator anOperator = left.getOperator(ExpressionOperator.Multiply);
303         return anOperator.expressionFor(left, right);
304     }
305
306     /**
307      * PUBLIC:
308      * Return a new expression that applies the function to the given expression.
309      */

310     public static Expression power(Expression expression, int raised) {
311         return power(expression, new Integer JavaDoc(raised));
312     }
313
314     /**
315      * PUBLIC:
316      * Return a new expression that applies the function to the given expression.
317      */

318     public static Expression power(Expression expression, Object JavaDoc raised) {
319         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Power);
320         return anOperator.expressionFor(expression, raised);
321     }
322
323     /**
324      * PUBLIC:
325      * Return a new expression that applies the function to the given expression.
326      */

327     public static Expression round(Expression expression, int decimalPlaces) {
328         return round(expression, new Integer JavaDoc(decimalPlaces));
329     }
330
331     /**
332      * PUBLIC:
333      * Return a new expression that applies the function to the given expression.
334      */

335     public static Expression round(Expression expression, Object JavaDoc decimalPlaces) {
336         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Round);
337         return anOperator.expressionFor(expression, decimalPlaces);
338     }
339
340     /**
341      * PUBLIC:
342      * Return a new expression that applies the function to the given expression.
343      */

344     public static Expression sign(Expression expression) {
345         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Sign);
346         return anOperator.expressionFor(expression);
347     }
348
349     /**
350      * PUBLIC:
351      * Return a new expression that applies the function to the given expression.
352      */

353     public static Expression sin(Expression expression) {
354         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Sin);
355         return anOperator.expressionFor(expression);
356     }
357
358     /**
359      * PUBLIC:
360      * Return a new expression that applies the function to the given expression.
361      */

362     public static Expression sinh(Expression expression) {
363         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Sinh);
364         return anOperator.expressionFor(expression);
365     }
366
367     /**
368      * PUBLIC:
369      * Return a new expression that applies the function to the given expression.
370      */

371     public static Expression subtract(Expression left, int right) {
372         return subtract(left, new Integer JavaDoc(right));
373     }
374
375     /**
376      * PUBLIC:
377      * Return a new expression that applies the function to the given expression.
378      */

379     public static Expression subtract(Expression left, Object JavaDoc right) {
380         ExpressionOperator anOperator = left.getOperator(ExpressionOperator.Subtract);
381         return anOperator.expressionFor(left, right);
382     }
383
384     /**
385      * PUBLIC:
386      * Return a new expression that applies the function to the given expression.
387      */

388     public static Expression tan(Expression expression) {
389         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Tan);
390         return anOperator.expressionFor(expression);
391     }
392
393     /**
394      * PUBLIC:
395      * Return a new expression that applies the function to the given expression.
396      */

397     public static Expression tanh(Expression expression) {
398         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Tanh);
399         return anOperator.expressionFor(expression);
400     }
401
402     /**
403      * PUBLIC:
404      * Return a new expression that applies the function to the given expression.
405      */

406     public static Expression trunc(Expression expression, int decimalPlaces) {
407         return trunc(expression, new Integer JavaDoc(decimalPlaces));
408     }
409
410     /**
411      * PUBLIC:
412      * Return a new expression that applies the function to the given expression.
413      */

414     public static Expression trunc(Expression expression, Object JavaDoc decimalPlaces) {
415         ExpressionOperator anOperator = expression.getOperator(ExpressionOperator.Trunc);
416         return anOperator.expressionFor(expression, decimalPlaces);
417     }
418 }
419
Popular Tags