KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > functions > ConversionFunctions


1 /*
2  * Copyright 2004-2005 Gary Bentley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package org.josql.functions;
16
17 import java.util.Map JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.GregorianCalendar JavaDoc;
21 import java.util.Calendar JavaDoc;
22
23 import java.text.SimpleDateFormat JavaDoc;
24
25 import org.josql.Query;
26 import org.josql.QueryExecutionException;
27
28 /**
29  * Note: creating new instances of SimpleDateFormat objects are VERY costly over
30  * large(ish) numbers of objects therefore a cache of objects is provided.
31  */

32 public class ConversionFunctions extends AbstractFunctionHandler
33 {
34
35     public static final String JavaDoc HANDLER_ID = "_internal_conversion";
36
37     /**
38      * Represents the {@link Calendar#MINUTE} field, is: <b>mi</b>.
39      */

40     public static final String JavaDoc MINUTE = "mi";
41
42     /**
43      * Represents the {@link Calendar#DATE} field, is: <b>d</b>.
44      */

45     public static final String JavaDoc DAY = "d";
46
47     /**
48      * Represents the {@link Calendar#YEAR} field, is: <b>y</b>.
49      */

50     public static final String JavaDoc YEAR = "y";
51
52     /**
53      * Represents the {@link Calendar#SECOND} field, is: <b>s</b>.
54      */

55     public static final String JavaDoc SECOND = "s";
56
57     /**
58      * Represents the {@link Calendar#HOUR_OF_DAY} field, is: <b>h</b>.
59      */

60     public static final String JavaDoc HOUR = "h";
61
62     /**
63      * Represents the {@link Calendar#MONTH} field, is: <b>m</b>.
64      */

65     public static final String JavaDoc MONTH = "m";
66
67     /**
68      * Represents the {@link Calendar#WEEK_OF_YEAR} field, is: <b>w</b>.
69      */

70     public static final String JavaDoc WEEK = "w";
71
72     public static String JavaDoc DEFAULT_DATE_FORMAT_SPEC = "dd/MMM/yyyy";
73     public static String JavaDoc DEFAULT_DATE_FORMAT_SPEC_2 = "dd-MMM-yyyy";
74     public static String JavaDoc DEFAULT_DATE_FORMAT_SPEC_3 = "dd MMM yyyy";
75
76     private static Map JavaDoc sdfs = new HashMap JavaDoc ();
77     private static Calendar JavaDoc cal = Calendar.getInstance ();
78
79     static
80     {
81
82     ConversionFunctions.sdfs.put (ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC,
83                       new SimpleDateFormat JavaDoc (ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC));
84     ConversionFunctions.sdfs.put (ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC_2,
85                       new SimpleDateFormat JavaDoc (ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC_2));
86     ConversionFunctions.sdfs.put (ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC_3,
87                       new SimpleDateFormat JavaDoc (ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC_3));
88
89     }
90
91     /**
92      * This method (function) will return the associated field from a
93      * {@link Calendar} instance. The <b>type</b> parm should be one of the
94      * constants from this class. The default {@link java.util.TimeZone} is used.
95      *
96      * @param d If the type is a long value then it is first converted to a Date.
97      * Or a {@link Date} should be used.
98      * @param type The type of field to get.
99      * @return The field from {@link Calendar}.
100      * @throws QueryExecutionException If the <b>d</b> parm isn't an instance of
101      * {@link Long} or {@link Date}.
102      */

103     public int timeField (Object JavaDoc d,
104                   String JavaDoc type)
105                       throws QueryExecutionException
106     {
107
108     if ((!(d instanceof Date JavaDoc))
109         &&
110         (!(d instanceof Long JavaDoc))
111        )
112     {
113
114         throw new QueryExecutionException ("Value passed in is of type: " +
115                            d.getClass ().getName () +
116                            " only: " +
117                            Long JavaDoc.class.getName () +
118                            " or: " +
119                            Date JavaDoc.class.getName () +
120                            " are supported.");
121
122     }
123
124     Date JavaDoc date = null;
125
126     if (d instanceof Long JavaDoc)
127     {
128
129         date = new Date JavaDoc (((Long JavaDoc) d).longValue ());
130
131     }
132
133     if (d instanceof Date JavaDoc)
134     {
135
136         date = (Date JavaDoc) d;
137
138     }
139
140     ConversionFunctions.cal.setTime (date);
141
142     type = type.toLowerCase ();
143
144     if (type.equals (ConversionFunctions.SECOND))
145     {
146
147         return ConversionFunctions.cal.get (Calendar.SECOND);
148
149     }
150
151     if (type.equals (ConversionFunctions.MINUTE))
152     {
153
154         return ConversionFunctions.cal.get (Calendar.MINUTE);
155
156     }
157
158     if (type.equals (ConversionFunctions.HOUR))
159     {
160
161         return ConversionFunctions.cal.get (Calendar.HOUR_OF_DAY);
162
163     }
164
165     if (type.equals (ConversionFunctions.DAY))
166     {
167
168         return ConversionFunctions.cal.get (Calendar.DATE);
169
170     }
171
172     if (type.equals (ConversionFunctions.WEEK))
173     {
174
175         return ConversionFunctions.cal.get (Calendar.WEEK_OF_YEAR);
176
177     }
178
179     if (type.equals (ConversionFunctions.MONTH))
180     {
181
182         return ConversionFunctions.cal.get (Calendar.MONTH);
183
184     }
185
186     if (type.equals (ConversionFunctions.YEAR))
187     {
188
189         return ConversionFunctions.cal.get (Calendar.YEAR);
190
191     }
192
193     // None of the above...
194
return -1;
195
196     }
197
198     public Date JavaDoc addTime (Date JavaDoc d,
199              Double JavaDoc amount,
200              String JavaDoc type)
201     {
202
203     int a = amount.intValue ();
204
205     long v = d.getTime ();
206
207     if (type.equals (ConversionFunctions.SECOND))
208     {
209
210         v += (a * 1000);
211
212         return new Date JavaDoc (v);
213
214     }
215
216     if (type.equals (ConversionFunctions.MINUTE))
217     {
218
219         v += (a * 60000);
220
221         return new Date JavaDoc (v);
222
223     }
224
225     if (type.equals (ConversionFunctions.HOUR))
226     {
227
228         v += (a * 3600000);
229
230         return new Date JavaDoc (v);
231
232     }
233
234     if (type.equals (ConversionFunctions.DAY))
235     {
236
237         v += (a * 24 * 3600000);
238
239         return new Date JavaDoc (v);
240
241     }
242
243     if (type.equals (ConversionFunctions.WEEK))
244     {
245
246         v += (a * 7 * 24 * 3600000);
247
248         return new Date JavaDoc (v);
249
250     }
251
252     if (type.equals (ConversionFunctions.MONTH))
253     {
254
255         // Need something a bit more sophisticated now...
256
GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc ();
257         gc.setTime (d);
258         
259         gc.add (Calendar.MONTH,
260             a);
261
262         return gc.getTime ();
263
264     }
265
266     if (type.equals (ConversionFunctions.YEAR))
267     {
268
269         // Need something a bit more sophisticated now...
270
GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc ();
271         gc.setTime (d);
272         
273         gc.add (Calendar.YEAR,
274             a);
275
276         return gc.getTime ();
277
278     }
279
280     // None of the above...
281
return d;
282
283     }
284
285     public Date JavaDoc toDate (Object JavaDoc value)
286                     throws QueryExecutionException
287     {
288
289     if (value == null)
290     {
291
292         return null;
293
294     }
295
296     if (value instanceof Number JavaDoc)
297     {
298
299         return new Date JavaDoc (((Number JavaDoc) value).longValue ());
300
301     }
302
303     if (value instanceof String JavaDoc)
304     {
305
306         return this.toDate ((String JavaDoc) value,
307                 null);
308
309     }
310
311     if (value instanceof Date JavaDoc)
312     {
313
314         return (Date JavaDoc) value;
315
316     }
317
318     throw new QueryExecutionException ("Type: " + value.getClass ().getName () + " is not supported.");
319
320     }
321
322     public Date JavaDoc to_date (Object JavaDoc value)
323                      throws QueryExecutionException
324     {
325
326     return this.toDate (value);
327
328     }
329
330     public Date JavaDoc to_date (String JavaDoc value,
331              String JavaDoc spec)
332                      throws QueryExecutionException
333     {
334
335     return this.toDate (value,
336                 spec);
337
338     }
339
340     public Date JavaDoc toDate (String JavaDoc value,
341             String JavaDoc spec)
342                     throws QueryExecutionException
343     {
344
345     if (spec == null)
346     {
347
348         spec = ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC;
349
350     }
351
352     SimpleDateFormat JavaDoc df = (SimpleDateFormat JavaDoc) ConversionFunctions.sdfs.get (spec);
353
354     if (df == null)
355     {
356
357         df = new SimpleDateFormat JavaDoc (spec);
358
359         ConversionFunctions.sdfs.put (spec,
360                       df);
361
362     }
363
364     try
365     {
366
367         return df.parse (value);
368
369     } catch (Exception JavaDoc e) {
370
371         throw new QueryExecutionException ("Unable to parse date value: " +
372                            value +
373                            " using spec: " +
374                            spec,
375                            e);
376
377     }
378
379     }
380
381     public Long JavaDoc toMillis (Date JavaDoc d)
382     {
383
384     return new Long JavaDoc (d.getTime ());
385
386     }
387
388     public Long JavaDoc toDateMillis (String JavaDoc value,
389                   String JavaDoc spec)
390                           throws QueryExecutionException
391     {
392
393     if (spec == null)
394     {
395
396         spec = ConversionFunctions.DEFAULT_DATE_FORMAT_SPEC;
397
398     }
399
400     SimpleDateFormat JavaDoc df = (SimpleDateFormat JavaDoc) ConversionFunctions.sdfs.get (spec);
401
402     if (df == null)
403     {
404
405         df = new SimpleDateFormat JavaDoc (spec);
406
407         ConversionFunctions.sdfs.put (spec,
408                       df);
409
410     }
411
412     try
413     {
414
415         Date JavaDoc d = df.parse (value);
416
417         return new Long JavaDoc (d.getTime ());
418
419     } catch (Exception JavaDoc e) {
420
421         throw new QueryExecutionException ("Unable to parse date value: " +
422                            value +
423                            " using spec: " +
424                            spec,
425                            e);
426
427     }
428
429     }
430
431     public String JavaDoc upper (Object JavaDoc o)
432     {
433
434     if (o == null)
435     {
436
437         return null;
438
439     }
440
441     return o.toString ().toUpperCase ();
442
443     }
444
445     public String JavaDoc lower (Object JavaDoc o)
446     {
447
448     if (o == null)
449     {
450
451         return null;
452
453     }
454
455     return o.toString ().toLowerCase ();
456
457     }
458
459     public String JavaDoc to_string (Object JavaDoc o)
460     {
461     
462     return this.toString (o);
463
464     }
465
466     public String JavaDoc toString (Object JavaDoc o)
467     {
468
469     return o + "";
470
471     }
472
473     public Number JavaDoc to_number (Object JavaDoc o)
474     {
475
476     return this.toNumber (o);
477
478     }
479
480     public Number JavaDoc toNumber (Object JavaDoc o)
481     {
482
483     if (o == null)
484     {
485
486         return null;
487
488     }
489
490     if (o instanceof String JavaDoc)
491     {
492
493         // Try and parse as a double.
494
try
495         {
496
497         return new Double JavaDoc ((String JavaDoc) o);
498
499         } catch (Exception JavaDoc e) {
500
501         // Ignore? Maybe have an option...
502

503         }
504
505     }
506
507     if (o instanceof Date JavaDoc)
508     {
509
510         return new Double JavaDoc (((Date JavaDoc) o).getTime ());
511
512     }
513
514     if (!(o instanceof Number JavaDoc))
515     {
516
517         return null;
518
519     }
520
521     return (Number JavaDoc) o;
522
523     }
524
525 }
526
Popular Tags