KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
18
19 import java.util.Random JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Calendar JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 import com.gentlyweb.utils.Getter;
27
28 import org.josql.Query;
29 import org.josql.QueryExecutionException;
30
31 import org.josql.expressions.Expression;
32
33 public class MiscellaneousFunctions extends AbstractFunctionHandler
34 {
35
36     public static final String JavaDoc HANDLER_ID = "_internal_misc";
37
38     private Map JavaDoc accessorCache = new HashMap JavaDoc ();
39
40     private Random JavaDoc rand = new Random JavaDoc ();
41
42     /**
43      * Return the current date.
44      *
45      * @param zeroTime If set to <code>true</code> then the date returned will have it's time fields
46      * set to zero.
47      * @return A date object.
48      */

49     public Date JavaDoc now (boolean zeroTime)
50     {
51
52     Date JavaDoc d = null;
53
54     if (zeroTime)
55     {
56
57         Calendar JavaDoc c = Calendar.getInstance ();
58
59         c.set (Calendar.HOUR_OF_DAY,
60            0);
61         c.set (Calendar.MINUTE,
62            0);
63         c.set (Calendar.SECOND,
64            0);
65         c.set (Calendar.MILLISECOND,
66            0);
67
68         d = c.getTime ();
69
70     } else {
71
72         d = new Date JavaDoc ();
73
74     }
75
76     return d;
77
78     }
79
80     public void cache (List JavaDoc allobjs,
81                        Getter get)
82                    throws QueryExecutionException
83     {
84
85     int s = allobjs.size ();
86
87     for (int i = 0; i < s; i++)
88     {
89
90         Object JavaDoc o = allobjs.get (i);
91
92         try
93         {
94
95         this.q.setSaveValue (o,
96                      get.getValue (o));
97
98         } catch (Exception JavaDoc e) {
99
100         throw new QueryExecutionException ("Unable to get value from accessor: " +
101                            get +
102                            " from object: " +
103                            i,
104                            e);
105
106         }
107
108     }
109
110     }
111
112     public void cache (List JavaDoc allobjs,
113                        Expression exp)
114                    throws QueryExecutionException
115     {
116
117     Object JavaDoc co = this.q.getCurrentObject ();
118
119     int s = allobjs.size ();
120
121     for (int i = 0; i < s; i++)
122     {
123
124         Object JavaDoc o = allobjs.get (i);
125
126         this.q.setCurrentObject (o);
127
128         try
129         {
130
131         this.q.setSaveValue (o,
132                      exp.getValue (o,
133                            q));
134
135         } catch (Exception JavaDoc e) {
136
137         throw new QueryExecutionException ("Unable to get value from expression: " +
138                            exp +
139                            " from object: " +
140                            i,
141                            e);
142
143         }
144
145     }
146
147     this.q.setCurrentObject (co);
148
149     }
150
151     public double abs (Number JavaDoc d)
152     {
153
154     return Math.abs (d.doubleValue ());
155
156     }
157
158     public int random ()
159     {
160
161     return this.rand.nextInt ();
162
163     }
164
165     public int random (Number JavaDoc n)
166     {
167
168     return this.rand.nextInt (n.intValue ());
169
170     }
171
172     public double randomDouble ()
173     {
174
175     return this.rand.nextDouble ();
176
177     }
178
179     public Object JavaDoc saveValue (Object JavaDoc saveValueName)
180     {
181
182     return this.q.getSaveValue (saveValueName);
183
184     }
185
186     public Object JavaDoc savevalue (Object JavaDoc saveValueName)
187     {
188
189     return this.q.getSaveValue (saveValueName);
190
191     }
192
193     public Object JavaDoc save_value (Object JavaDoc saveValueName)
194     {
195
196     return this.q.getSaveValue (saveValueName);
197
198     }
199
200     public String JavaDoc fileExtension (Object JavaDoc f)
201     {
202
203     if (f == null)
204     {
205
206         return null;
207
208     }
209
210     String JavaDoc n = null;
211
212     if (f instanceof String JavaDoc)
213     {
214
215         n = (String JavaDoc) f;
216
217     }
218
219     if (f instanceof File JavaDoc)
220     {
221
222         File JavaDoc fi = (File JavaDoc) f;
223
224         if (fi.isDirectory ())
225         {
226
227         return null;
228
229         }
230
231         n = ((File JavaDoc) f).getName ();
232
233     }
234
235     return n.substring (n.lastIndexOf (".") + 1);
236
237     }
238
239     /**
240      * Call the specified accessor on the object.
241      *
242      * @param oExp The expression to use to evaluate to get the object.
243      * @param accExp The expression that is evaluated to get the accessor.
244      * @return The value returned from the accessor.
245      * @throws Exception If there is something wrong.
246      */

247     public Object JavaDoc accessor (Expression oExp,
248                 Expression accExp)
249                         throws Exception JavaDoc
250     {
251
252     // Get the value for the object.
253
Object JavaDoc o = null;
254
255     try
256     {
257
258         o = oExp.getValue (this.q.getCurrentObject (),
259                    this.q);
260
261     } catch (Exception JavaDoc e) {
262
263         throw new QueryExecutionException ("Unable to evaluate expression: " +
264                            oExp +
265                            " to get object.");
266
267     }
268
269     Object JavaDoc a = null;
270
271     try
272     {
273
274         a = accExp.getValue (this.q.getCurrentObject (),
275                  this.q);
276
277     } catch (Exception JavaDoc e) {
278
279         throw new QueryExecutionException ("Unable to evaluate expression: " +
280                            accExp +
281                            " to get accessor.");
282
283     }
284
285     if (a == null)
286     {
287
288         throw new QueryExecutionException ("Accessor expression: " +
289                            accExp +
290                            " evaluates to null for object: " +
291                            o +
292                            " returned from expression: " +
293                            oExp);
294
295     }
296
297     // Co-erce to a string.
298
return this.accessor (o,
299                   a.toString ());
300
301     }
302
303     /**
304      * Call the specified accessor on the object.
305      *
306      * @param o The object to call the accessor on.
307      * @param acc The accessor.
308      * @return The value returned from the accessor.
309      * @throws Exception If there is something wrong.
310      */

311     public Object JavaDoc accessor (Object JavaDoc o,
312                 String JavaDoc acc)
313                         throws Exception JavaDoc
314     {
315
316     if (o == null)
317     {
318
319         return null;
320
321     }
322
323     // See if we already have the getter.
324
Getter g = (Getter) this.accessorCache.get (o.getClass ().getName () + acc);
325
326     if (g == null)
327     {
328
329         // Create a new Getter for the class of the object.
330
g = new Getter (acc,
331                 o.getClass ());
332
333         this.accessorCache.put (o.getClass ().getName () + acc,
334                     g);
335
336     }
337
338     return g.getValue (o);
339
340     }
341
342     public Object JavaDoc ifThen (Expression ifcond,
343               Expression thenVal)
344                       throws QueryExecutionException
345     {
346
347     if (ifcond.isTrue (this.q.getCurrentObject (),
348                this.q))
349     {
350
351         return thenVal.getValue (this.q.getCurrentObject (),
352                      this.q);
353
354     }
355
356     return null;
357
358     }
359
360     public Object JavaDoc ifThenElse (Expression ifcond,
361                   Expression thenVal,
362                   Expression elseVal)
363                           throws QueryExecutionException
364     {
365
366     Object JavaDoc i = this.ifThen (ifcond,
367                 thenVal);
368
369     if (i == null)
370     {
371
372         return elseVal.getValue (this.q.getCurrentObject (),
373                      this.q);
374
375     }
376
377     return i;
378
379     }
380
381     public Object JavaDoc eval (Expression exp)
382                     throws QueryExecutionException
383     {
384
385     return exp.getValue (this.q.getCurrentObject (),
386                  this.q);
387
388     }
389
390     /**
391      * Evaluates the <b>type</b> expression to produce a object whose type
392      * should be compared against the class gained from evaluation of the
393      * <b>clazz</b> expression.
394      * In effect the following is performed:
395      * <pre>
396      * obj.getValue (q.getCurrentObject (), q)
397      * instanceof clazz.getValue (q.getCurrentObject (), q).getClass ()
398      * </pre>
399      * <p>
400      * This is really just a thin wrapper around {@link Class#isInstanceof(Object)}.
401      *
402      * @param type The expression that represents the object to
403      * against.
404      * @param clazz The expression that represents the class of the type
405      * to compare against.
406      * @throws QueryExecutionException If either of the expressions can't
407      * be evaluated.
408      */

409     public Boolean JavaDoc instanceOf (Expression obj,
410                    Expression clazz)
411                            throws QueryExecutionException
412     {
413
414     return Boolean.valueOf (clazz.getValue (this.q.getCurrentObject (),
415                         q).getClass ().isInstance (obj.getValue (this.q.getCurrentObject (),
416                                              q)));
417
418     }
419
420 }
421
Popular Tags