001: /**********************************************************************
002: Copyright (c) 2004 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: 2005 Andy Jefferson - added sin, cos, tan, asin, acos, atan methods
018: ...
019: **********************************************************************/package org.jpox.store.expression;
020:
021: import java.math.BigDecimal;
022: import java.math.BigInteger;
023:
024: /**
025: * Representation of Math functions in query languages.
026: * @version $Revision: 1.11 $
027: */
028: public class MathExpression extends ScalarExpression {
029: /**
030: * @param qs The query statement
031: */
032: public MathExpression(QueryExpression qs) {
033: super (qs);
034: }
035:
036: /**
037: * Returns the absolute value of the argument.
038: * @param expr the expression
039: * @return the result in a ScalarExpression instance
040: */
041: public ScalarExpression absMethod(ScalarExpression expr) {
042: if (expr == null) {
043: return new NullLiteral(qs);
044: }
045: if (expr instanceof Literal) {
046: if (expr instanceof ByteLiteral) {
047: int originalValue = ((BigInteger) ((ByteLiteral) expr)
048: .getValue()).intValue();
049: BigInteger absValue = new BigInteger(String
050: .valueOf(Math.abs(originalValue)));
051: return new ByteLiteral(qs, expr.mapping, absValue);
052: } else if (expr instanceof IntegerLiteral) {
053: int originalValue = ((Number) ((IntegerLiteral) expr)
054: .getValue()).intValue();
055: Integer absValue = new Integer(Math.abs(originalValue));
056: return new IntegerLiteral(qs, expr.mapping, absValue);
057: } else if (expr instanceof FloatingPointLiteral) {
058: double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr)
059: .getValue()).doubleValue();
060: Double absValue = new Double(Math.abs(originalValue));
061: return new FloatingPointLiteral(qs, expr.mapping,
062: absValue);
063: }
064: throw new IllegalOperationException(this , "absMethod", expr);
065: } else {
066: return qs.getStoreManager().getDatastoreAdapter()
067: .absMethod(expr);
068: }
069: }
070:
071: /**
072: * Returns the square root of the argument.
073: * @param expr the expression
074: * @return the result in a ScalarExpression instance
075: */
076: public ScalarExpression sqrtMethod(ScalarExpression expr) {
077: if (expr == null) {
078: return new NullLiteral(qs);
079: }
080: if (expr instanceof Literal) {
081: if (expr instanceof ByteLiteral) {
082: int originalValue = ((BigInteger) ((ByteLiteral) expr)
083: .getValue()).intValue();
084: Double newValue = new Double(Math.sqrt(originalValue));
085: return new FloatingPointLiteral(qs, expr.mapping,
086: newValue);
087: } else if (expr instanceof IntegerLiteral) {
088: int originalValue = ((Number) ((IntegerLiteral) expr)
089: .getValue()).intValue();
090: Double newValue = new Double(Math.sqrt(originalValue));
091: return new FloatingPointLiteral(qs, expr.mapping,
092: newValue);
093: } else if (expr instanceof FloatingPointLiteral) {
094: double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr)
095: .getValue()).doubleValue();
096: Double newValue = new Double(Math.sqrt(originalValue));
097: return new FloatingPointLiteral(qs, expr.mapping,
098: newValue);
099: }
100: throw new IllegalOperationException(this , "sqrtMethod",
101: expr);
102: } else {
103: return qs.getStoreManager().getDatastoreAdapter()
104: .sqrtMethod(expr);
105: }
106: }
107:
108: /**
109: * Returns the cosine of the argument.
110: * @param expr the expression
111: * @return the result in a ScalarExpression instance
112: */
113: public ScalarExpression cosMethod(ScalarExpression expr) {
114: if (expr == null) {
115: return new NullLiteral(qs);
116: }
117: if (expr instanceof Literal) {
118: if (expr instanceof ByteLiteral) {
119: int originalValue = ((BigInteger) ((ByteLiteral) expr)
120: .getValue()).intValue();
121: Double newValue = new Double(Math.cos(originalValue));
122: return new FloatingPointLiteral(qs, expr.mapping,
123: newValue);
124: } else if (expr instanceof IntegerLiteral) {
125: int originalValue = ((Number) ((IntegerLiteral) expr)
126: .getValue()).intValue();
127: Double newValue = new Double(Math.cos(originalValue));
128: return new FloatingPointLiteral(qs, expr.mapping,
129: newValue);
130: } else if (expr instanceof FloatingPointLiteral) {
131: double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr)
132: .getValue()).doubleValue();
133: Double newValue = new Double(Math.cos(originalValue));
134: return new FloatingPointLiteral(qs, expr.mapping,
135: newValue);
136: }
137: throw new IllegalOperationException(this , "cosMethod", expr);
138: } else {
139: return qs.getStoreManager().getDatastoreAdapter()
140: .cosMethod(expr);
141: }
142: }
143:
144: /**
145: * Returns the sine of the argument.
146: * @param expr the expression
147: * @return the result in a ScalarExpression instance
148: */
149: public ScalarExpression sinMethod(ScalarExpression expr) {
150: if (expr == null) {
151: return new NullLiteral(qs);
152: }
153: if (expr instanceof Literal) {
154: if (expr instanceof ByteLiteral) {
155: int originalValue = ((BigInteger) ((ByteLiteral) expr)
156: .getValue()).intValue();
157: Double newValue = new Double(Math.sin(originalValue));
158: return new FloatingPointLiteral(qs, expr.mapping,
159: newValue);
160: } else if (expr instanceof IntegerLiteral) {
161: int originalValue = ((Number) ((IntegerLiteral) expr)
162: .getValue()).intValue();
163: Double newValue = new Double(Math.sin(originalValue));
164: return new FloatingPointLiteral(qs, expr.mapping,
165: newValue);
166: } else if (expr instanceof FloatingPointLiteral) {
167: double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr)
168: .getValue()).doubleValue();
169: Double newValue = new Double(Math.sin(originalValue));
170: return new FloatingPointLiteral(qs, expr.mapping,
171: newValue);
172: }
173: throw new IllegalOperationException(this , "sinMethod", expr);
174: } else {
175: return qs.getStoreManager().getDatastoreAdapter()
176: .sinMethod(expr);
177: }
178: }
179:
180: /**
181: * Returns the tangent of the argument.
182: * @param expr the expression
183: * @return the result in a ScalarExpression instance
184: */
185: public ScalarExpression tanMethod(ScalarExpression expr) {
186: if (expr == null) {
187: return new NullLiteral(qs);
188: }
189: if (expr instanceof Literal) {
190: if (expr instanceof ByteLiteral) {
191: int originalValue = ((BigInteger) ((ByteLiteral) expr)
192: .getValue()).intValue();
193: Double newValue = new Double(Math.tan(originalValue));
194: return new FloatingPointLiteral(qs, expr.mapping,
195: newValue);
196: } else if (expr instanceof IntegerLiteral) {
197: int originalValue = ((Number) ((IntegerLiteral) expr)
198: .getValue()).intValue();
199: Double newValue = new Double(Math.tan(originalValue));
200: return new FloatingPointLiteral(qs, expr.mapping,
201: newValue);
202: } else if (expr instanceof FloatingPointLiteral) {
203: double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr)
204: .getValue()).doubleValue();
205: Double newValue = new Double(Math.tan(originalValue));
206: return new FloatingPointLiteral(qs, expr.mapping,
207: newValue);
208: }
209: throw new IllegalOperationException(this , "tanMethod", expr);
210: } else {
211: return qs.getStoreManager().getDatastoreAdapter()
212: .tanMethod(expr);
213: }
214: }
215:
216: /**
217: * Returns the arc cosine of the argument.
218: * @param expr the expression
219: * @return the result in a ScalarExpression instance
220: */
221: public ScalarExpression acosMethod(ScalarExpression expr) {
222: if (expr == null) {
223: return new NullLiteral(qs);
224: }
225: if (expr instanceof Literal) {
226: if (expr instanceof ByteLiteral) {
227: int originalValue = ((BigInteger) ((ByteLiteral) expr)
228: .getValue()).intValue();
229: Double newValue = new Double(Math.acos(originalValue));
230: return new FloatingPointLiteral(qs, expr.mapping,
231: newValue);
232: } else if (expr instanceof IntegerLiteral) {
233: int originalValue = ((Number) ((IntegerLiteral) expr)
234: .getValue()).intValue();
235: Double newValue = new Double(Math.acos(originalValue));
236: return new FloatingPointLiteral(qs, expr.mapping,
237: newValue);
238: } else if (expr instanceof FloatingPointLiteral) {
239: double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr)
240: .getValue()).doubleValue();
241: Double newValue = new Double(Math.acos(originalValue));
242: return new FloatingPointLiteral(qs, expr.mapping,
243: newValue);
244: }
245: throw new IllegalOperationException(this , "acosMethod",
246: expr);
247: } else {
248: return qs.getStoreManager().getDatastoreAdapter()
249: .acosMethod(expr);
250: }
251: }
252:
253: /**
254: * Returns the arc sine of the argument.
255: * @param expr the expression
256: * @return the result in a ScalarExpression instance
257: */
258: public ScalarExpression asinMethod(ScalarExpression expr) {
259: if (expr == null) {
260: return new NullLiteral(qs);
261: }
262: if (expr instanceof Literal) {
263: if (expr instanceof ByteLiteral) {
264: int originalValue = ((BigInteger) ((ByteLiteral) expr)
265: .getValue()).intValue();
266: Double newValue = new Double(Math.asin(originalValue));
267: return new FloatingPointLiteral(qs, expr.mapping,
268: newValue);
269: } else if (expr instanceof IntegerLiteral) {
270: int originalValue = ((Number) ((IntegerLiteral) expr)
271: .getValue()).intValue();
272: Double newValue = new Double(Math.asin(originalValue));
273: return new FloatingPointLiteral(qs, expr.mapping,
274: newValue);
275: } else if (expr instanceof FloatingPointLiteral) {
276: double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr)
277: .getValue()).doubleValue();
278: Double newValue = new Double(Math.asin(originalValue));
279: return new FloatingPointLiteral(qs, expr.mapping,
280: newValue);
281: }
282: throw new IllegalOperationException(this , "asinMethod",
283: expr);
284: } else {
285: return qs.getStoreManager().getDatastoreAdapter()
286: .asinMethod(expr);
287: }
288: }
289:
290: /**
291: * Returns the arc tangent of the argument.
292: * @param expr the expression
293: * @return the result in a ScalarExpression instance
294: */
295: public ScalarExpression atanMethod(ScalarExpression expr) {
296: if (expr == null) {
297: return new NullLiteral(qs);
298: }
299: if (expr instanceof Literal) {
300: if (expr instanceof ByteLiteral) {
301: int originalValue = ((BigInteger) ((ByteLiteral) expr)
302: .getValue()).intValue();
303: Double newValue = new Double(Math.atan(originalValue));
304: return new FloatingPointLiteral(qs, expr.mapping,
305: newValue);
306: } else if (expr instanceof IntegerLiteral) {
307: int originalValue = ((Number) ((IntegerLiteral) expr)
308: .getValue()).intValue();
309: Double newValue = new Double(Math.atan(originalValue));
310: return new FloatingPointLiteral(qs, expr.mapping,
311: newValue);
312: } else if (expr instanceof FloatingPointLiteral) {
313: double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr)
314: .getValue()).doubleValue();
315: Double newValue = new Double(Math.atan(originalValue));
316: return new FloatingPointLiteral(qs, expr.mapping,
317: newValue);
318: }
319: throw new IllegalOperationException(this , "atanMethod",
320: expr);
321: } else {
322: return qs.getStoreManager().getDatastoreAdapter()
323: .atanMethod(expr);
324: }
325: }
326:
327: /**
328: * Returns the exponent of the argument.
329: * @param expr the expression
330: * @return the result in a ScalarExpression instance
331: */
332: public ScalarExpression expMethod(ScalarExpression expr) {
333: if (expr == null) {
334: return new NullLiteral(qs);
335: }
336: if (expr instanceof Literal) {
337: if (expr instanceof ByteLiteral) {
338: int originalValue = ((BigInteger) ((ByteLiteral) expr)
339: .getValue()).intValue();
340: Double newValue = new Double(Math.exp(originalValue));
341: return new FloatingPointLiteral(qs, expr.mapping,
342: newValue);
343: } else if (expr instanceof IntegerLiteral) {
344: int originalValue = ((Number) ((IntegerLiteral) expr)
345: .getValue()).intValue();
346: Double newValue = new Double(Math.exp(originalValue));
347: return new FloatingPointLiteral(qs, expr.mapping,
348: newValue);
349: } else if (expr instanceof FloatingPointLiteral) {
350: double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr)
351: .getValue()).doubleValue();
352: Double newValue = new Double(Math.exp(originalValue));
353: return new FloatingPointLiteral(qs, expr.mapping,
354: newValue);
355: }
356: throw new IllegalOperationException(this , "expMethod", expr);
357: } else {
358: return qs.getStoreManager().getDatastoreAdapter()
359: .expMethod(expr);
360: }
361: }
362:
363: /**
364: * Returns the log of the argument.
365: * @param expr the expression
366: * @return the result in a ScalarExpression instance
367: */
368: public ScalarExpression logMethod(ScalarExpression expr) {
369: if (expr == null) {
370: return new NullLiteral(qs);
371: }
372: if (expr instanceof Literal) {
373: if (expr instanceof ByteLiteral) {
374: int originalValue = ((BigInteger) ((ByteLiteral) expr)
375: .getValue()).intValue();
376: Double newValue = new Double(Math.log(originalValue));
377: return new FloatingPointLiteral(qs, expr.mapping,
378: newValue);
379: } else if (expr instanceof IntegerLiteral) {
380: int originalValue = ((Number) ((IntegerLiteral) expr)
381: .getValue()).intValue();
382: Double newValue = new Double(Math.log(originalValue));
383: return new FloatingPointLiteral(qs, expr.mapping,
384: newValue);
385: } else if (expr instanceof FloatingPointLiteral) {
386: double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr)
387: .getValue()).doubleValue();
388: Double newValue = new Double(Math.log(originalValue));
389: return new FloatingPointLiteral(qs, expr.mapping,
390: newValue);
391: }
392: throw new IllegalOperationException(this , "logMethod", expr);
393: } else {
394: return qs.getStoreManager().getDatastoreAdapter()
395: .logMethod(expr);
396: }
397: }
398:
399: /**
400: * Returns the floor of the argument.
401: * @param expr the expression
402: * @return the result in a ScalarExpression instance
403: */
404: public ScalarExpression floorMethod(ScalarExpression expr) {
405: if (expr == null) {
406: return new NullLiteral(qs);
407: }
408: if (expr instanceof Literal) {
409: if (expr instanceof ByteLiteral) {
410: int originalValue = ((BigInteger) ((ByteLiteral) expr)
411: .getValue()).intValue();
412: Double newValue = new Double(Math.floor(originalValue));
413: return new FloatingPointLiteral(qs, expr.mapping,
414: newValue);
415: } else if (expr instanceof IntegerLiteral) {
416: int originalValue = ((Number) ((IntegerLiteral) expr)
417: .getValue()).intValue();
418: Double newValue = new Double(Math.floor(originalValue));
419: return new FloatingPointLiteral(qs, expr.mapping,
420: newValue);
421: } else if (expr instanceof FloatingPointLiteral) {
422: double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr)
423: .getValue()).doubleValue();
424: Double newValue = new Double(Math.floor(originalValue));
425: return new FloatingPointLiteral(qs, expr.mapping,
426: newValue);
427: }
428: throw new IllegalOperationException(this , "floorMethod",
429: expr);
430: } else {
431: return qs.getStoreManager().getDatastoreAdapter()
432: .floorMethod(expr);
433: }
434: }
435:
436: /**
437: * Returns the ceiling of the argument.
438: * @param expr the expression
439: * @return the result in a ScalarExpression instance
440: */
441: public ScalarExpression ceilrMethod(ScalarExpression expr) {
442: if (expr == null) {
443: return new NullLiteral(qs);
444: }
445: if (expr instanceof Literal) {
446: if (expr instanceof ByteLiteral) {
447: int originalValue = ((BigInteger) ((ByteLiteral) expr)
448: .getValue()).intValue();
449: Double newValue = new Double(Math.ceil(originalValue));
450: return new FloatingPointLiteral(qs, expr.mapping,
451: newValue);
452: } else if (expr instanceof IntegerLiteral) {
453: int originalValue = ((Number) ((IntegerLiteral) expr)
454: .getValue()).intValue();
455: Double newValue = new Double(Math.ceil(originalValue));
456: return new FloatingPointLiteral(qs, expr.mapping,
457: newValue);
458: } else if (expr instanceof FloatingPointLiteral) {
459: double originalValue = ((BigDecimal) ((FloatingPointLiteral) expr)
460: .getValue()).doubleValue();
461: Double newValue = new Double(Math.ceil(originalValue));
462: return new FloatingPointLiteral(qs, expr.mapping,
463: newValue);
464: }
465: throw new IllegalOperationException(this , "ceilMethod",
466: expr);
467: } else {
468: return qs.getStoreManager().getDatastoreAdapter()
469: .ceilMethod(expr);
470: }
471: }
472: }
|