KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > internal > Utilities


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.internal;
16
17 import java.util.List JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.Modifier JavaDoc;
27 import java.lang.reflect.Constructor JavaDoc;
28
29 public class Utilities
30 {
31
32     private static final String JavaDoc F = "f";
33     private static final String JavaDoc E = "e";
34     private static final String JavaDoc A = "a";
35     private static final String JavaDoc N = "n";
36
37     public static final int GT = 0;
38     public static final int GTE = 1;
39     public static final int LT = 2;
40     public static final int LTE = 3;
41     public static final int EQ = 4;
42
43     private static Map JavaDoc pCNames = new HashMap JavaDoc ();
44     private static Map JavaDoc primNames = new HashMap JavaDoc ();
45     private static Map JavaDoc primCls = new HashMap JavaDoc ();
46
47     static
48     {
49
50     Utilities.pCNames.put ("double",
51                    "");
52     Utilities.pCNames.put (Double JavaDoc.class.getName (),
53                    "");
54     Utilities.pCNames.put ("int",
55                    "");
56     Utilities.pCNames.put (Integer JavaDoc.class.getName (),
57                    "");
58     Utilities.pCNames.put ("float",
59                    "");
60     Utilities.pCNames.put (Float JavaDoc.class.getName (),
61                    "");
62     Utilities.pCNames.put ("long",
63                    "");
64     Utilities.pCNames.put (Long JavaDoc.class.getName (),
65                    "");
66     Utilities.pCNames.put ("short",
67                    "");
68     Utilities.pCNames.put (Short JavaDoc.class.getName (),
69                    "");
70     Utilities.pCNames.put ("byte",
71                    "");
72     Utilities.pCNames.put (Byte JavaDoc.class.getName (),
73                    "");
74     Utilities.pCNames.put (Number JavaDoc.class.getName (),
75                    "");
76
77     Utilities.primNames.put (Double.TYPE.getName (),
78                  Double JavaDoc.class.getName ());
79     Utilities.primNames.put (Double JavaDoc.class.getName (),
80                  Double.TYPE.getName ());
81     Utilities.primNames.put (Integer.TYPE.getName (),
82                  Integer JavaDoc.class.getName ());
83     Utilities.primNames.put (Integer JavaDoc.class.getName (),
84                  Integer.TYPE.getName ());
85     Utilities.primNames.put (Float.TYPE.getName (),
86                  Float JavaDoc.class.getName ());
87     Utilities.primNames.put (Float JavaDoc.class.getName (),
88                  Float.TYPE.getName ());
89     Utilities.primNames.put (Long.TYPE.getName (),
90                  Long JavaDoc.class.getName ());
91     Utilities.primNames.put (Long JavaDoc.class.getName (),
92                  Long.TYPE.getName ());
93     Utilities.primNames.put (Short.TYPE.getName (),
94                  Short JavaDoc.class.getName ());
95     Utilities.primNames.put (Short JavaDoc.class.getName (),
96                  Short.TYPE.getName ());
97     Utilities.primNames.put (Byte.TYPE.getName (),
98                  Byte JavaDoc.class.getName ());
99     Utilities.primNames.put (Byte JavaDoc.class.getName (),
100                  Byte.TYPE.getName ());
101     Utilities.primNames.put (Character.TYPE.getName (),
102                  Character JavaDoc.class.getName ());
103     Utilities.primNames.put (Character JavaDoc.class.getName (),
104                  Character.TYPE.getName ());
105     Utilities.primNames.put (Boolean.TYPE.getName (),
106                  Boolean JavaDoc.class.getName ());
107     Utilities.primNames.put (Boolean JavaDoc.class.getName (),
108                  Boolean.TYPE.getName ());
109
110     Utilities.primCls.put (Double.TYPE.getName (),
111                    Double.TYPE);
112     Utilities.primCls.put (Double JavaDoc.class.getName (),
113                    Double.TYPE);
114
115     Utilities.primCls.put (Integer.TYPE.getName (),
116                    Integer.TYPE);
117     Utilities.primCls.put (Integer JavaDoc.class.getName (),
118                    Integer.TYPE);
119
120     Utilities.primCls.put (Float.TYPE.getName (),
121                    Float.TYPE);
122     Utilities.primCls.put (Float JavaDoc.class.getName (),
123                    Float.TYPE);
124
125     Utilities.primCls.put (Long.TYPE.getName (),
126                    Long.TYPE);
127     Utilities.primCls.put (Long JavaDoc.class.getName (),
128                    Long.TYPE);
129
130     Utilities.primCls.put (Short.TYPE.getName (),
131                    Short.TYPE);
132     Utilities.primCls.put (Short JavaDoc.class.getName (),
133                    Short.TYPE);
134
135     Utilities.primCls.put (Byte.TYPE.getName (),
136                    Byte.TYPE);
137     Utilities.primCls.put (Byte JavaDoc.class.getName (),
138                    Byte.TYPE);
139
140     Utilities.primCls.put (Character.TYPE.getName (),
141                    Character.TYPE);
142     Utilities.primCls.put (Character JavaDoc.class.getName (),
143                    Character.TYPE);
144
145     Utilities.primCls.put (Boolean.TYPE.getName (),
146                    Boolean.TYPE);
147     Utilities.primCls.put (Boolean JavaDoc.class.getName (),
148                    Boolean.TYPE);
149
150     }
151
152     public static Class JavaDoc getObjectClass (Class JavaDoc c)
153     {
154
155     if (c.isPrimitive ())
156     {
157
158         String JavaDoc n = c.getName ();
159
160         if (n.equals (Boolean.TYPE.getName ()))
161         {
162
163         return Boolean JavaDoc.class;
164
165         }
166
167         if (n.equals (Long.TYPE.getName ()))
168         {
169
170         return Long JavaDoc.class;
171
172         }
173
174         if (n.equals (Short.TYPE.getName ()))
175         {
176
177         return Short JavaDoc.class;
178
179         }
180
181         if (n.equals (Integer.TYPE.getName ()))
182         {
183
184         return Integer JavaDoc.class;
185
186         }
187
188         if (n.equals (Double.TYPE.getName ()))
189         {
190
191         return Double JavaDoc.class;
192
193         }
194
195         if (n.equals (Character.TYPE.getName ()))
196         {
197
198         return Character JavaDoc.class;
199
200         }
201
202         if (n.equals (Float.TYPE.getName ()))
203         {
204
205         return Float JavaDoc.class;
206
207         }
208
209         if (n.equals (Byte.TYPE.getName ()))
210         {
211
212         return Byte JavaDoc.class;
213
214         }
215
216     }
217
218     return c;
219
220     }
221
222     public static Class JavaDoc getPrimitiveClass (Class JavaDoc c)
223     {
224
225     return (Class JavaDoc) Utilities.primCls.get (c.getName ());
226
227     }
228
229     public static boolean isPrimitiveClass (Class JavaDoc c)
230     {
231
232     return Utilities.primNames.containsKey (c.getName ());
233
234     }
235
236     public static boolean getResult (boolean v,
237                      boolean n)
238     {
239
240     if (v)
241     {
242
243         if (n)
244         {
245
246         return false;
247
248         }
249
250         return true;
251
252     }
253
254     if (n)
255     {
256
257         return true;
258
259     }
260
261     return false;
262
263     }
264
265     /**
266      * This method encapsulates our "matching" mechanism. It handles collections correctly
267      * and will match according to the combination of <b>igoreCase</b>, <b>type</b> and
268      * <b>not</b>.
269      *
270      * Note: this method deliberately throws no exceptions, and it tries hard to ensure that
271      * ClassCastExceptions and NullPointerExceptions are also NOT thrown, in other words in
272      * theory it should be possible to compare ANY object against ANY other in safety. However
273      * if the objects DO NOT implement comparable (and are type compatible, i.e. can <b>r</b>
274      * be assigned to <b>l</b>) then a string comparison is performed so you may be at the mercy
275      * of the {@link String#toString()} method of each object. In general this is not a problem
276      * but beware of potential gotchas such as:
277      * <code>
278      * SELECT *
279      * FROM MyObject
280      * WHERE 20 >= (SELECT value
281      * FROM myList)
282      * </code>
283      * <pre>
284      * It's tempting to think here that the query will return the correct result, however this is
285      * NOT true because the sub-query will return a List of Lists (with "value" as the single
286      * item in each list of the sub-query results). To make the query above work as expected you
287      * should use:
288      * <code>
289      * SELECT *
290      * FROM MyObject
291      * // The value will be returned instead of an enclosing list.
292      * WHERE 20 >= (SELECT [*] value
293      * FROM myList)
294      * </code>
295      *
296      * @param l The LHS object.
297      * @param r The RHS object.
298      * @param ignoreCase Whether to ignore the case or not, note: setting this to <code>true</code>
299      * will force a string comparison (the object to compare against will be
300      * converted to a string via: {@link String#toString()} and then "lowered".
301      * @param type The type of comparison to make, should be one of:
302      * {@link Utilities#GT}, {@link Utilities#GTE}, {@link Utilities#LT}
303      * {@link Utilities#LTE}, {@link Utilities#EQ}.
304      * @param not Whether the result should be reversed.
305      * @return <code>true</code> if <b>l</b> matches <b>r</b> given the rules defined by the
306      * other parms, <code>false</code> otherwise.
307      */

308     public static boolean matches (Object JavaDoc l,
309                    Object JavaDoc r,
310                    boolean ignoreCase,
311                    int type,
312                    boolean not)
313     {
314
315     if (l instanceof Collection JavaDoc)
316     {
317
318         if (r instanceof Collection JavaDoc)
319         {
320
321         Collection JavaDoc cl = (Collection JavaDoc) l;
322         Collection JavaDoc cr = (Collection JavaDoc) r;
323
324         boolean lisra = (cl instanceof List JavaDoc);
325         boolean risra = (cr instanceof List JavaDoc);
326
327         List JavaDoc rl = null;
328
329         if (risra)
330         {
331
332             rl = (List JavaDoc) cr;
333
334         }
335
336         int rs = cr.size () - 1;
337
338         // Need to now ensure that each item in the left is >, <, >=, <=
339
// than every item in the right... sigh... ;)
340
if (lisra)
341         {
342
343             List JavaDoc ll = (List JavaDoc) cl;
344
345             // Can go backwards for slightly faster access.
346
int s = ll.size () - 1;
347
348             for (int i = s; i > -1; i--)
349             {
350
351                 l = Utilities.lowerValue (ll.get (i),
352                           ignoreCase);
353
354             if (risra)
355             {
356
357                 for (int j = rs; j > -1; j--)
358                 {
359
360                 r = Utilities.lowerValue (rl.get (j),
361                               ignoreCase);
362
363                 if (!Utilities.compare2 (l,
364                              r,
365                              type,
366                              not))
367                 {
368
369                     return false;
370
371                 }
372
373                 }
374
375             } else {
376
377                 Iterator JavaDoc it = cr.iterator ();
378
379                 while (it.hasNext ())
380                 {
381
382                 r = Utilities.lowerValue (it.next (),
383                               ignoreCase);
384
385                 if (!Utilities.compare2 (l,
386                              r,
387                              type,
388                              not))
389                 {
390
391                     return false;
392
393                 }
394
395                 }
396
397             }
398
399             }
400
401         } else {
402
403             Iterator JavaDoc it = cl.iterator ();
404
405             while (it.hasNext ())
406             {
407
408             l = Utilities.lowerValue (it.next (),
409                           ignoreCase);
410
411             if (risra)
412             {
413
414                 for (int j = rs; j > -1; j--)
415                 {
416
417                 r = Utilities.lowerValue (rl.get (j),
418                               ignoreCase);
419
420                 if (!Utilities.compare2 (l,
421                              r,
422                              type,
423                              not))
424                 {
425
426                     return false;
427
428                 }
429
430                 }
431
432             } else {
433
434                 Iterator JavaDoc itr = cr.iterator ();
435
436                 while (itr.hasNext ())
437                 {
438
439                 r = Utilities.lowerValue (itr.next (),
440                               ignoreCase);
441
442                 if (!Utilities.compare2 (l,
443                              r,
444                              type,
445                              not))
446                 {
447
448                     return false;
449
450                 }
451
452                 }
453
454             }
455
456             }
457
458         }
459
460         // If we are here then it means that ALL values in the RHS
461
// collection match the condition with the LHS values
462
// so we return true.
463
return true;
464
465         } else {
466
467         // Here we need to check to ensure that ALL values in the LHS
468
// collection match the condition for the single RHS value.
469
Collection JavaDoc cl = (Collection JavaDoc) l;
470
471         r = Utilities.lowerValue (r,
472                       ignoreCase);
473
474         if (cl instanceof List JavaDoc)
475         {
476
477             List JavaDoc ll = (List JavaDoc) cl;
478
479             int ls = cl.size () - 1;
480
481             for (int i = ls; i > -1; i--)
482             {
483
484             l = Utilities.lowerValue (ll.get (i),
485                           ignoreCase);
486
487             if (!Utilities.compare2 (l,
488                          r,
489                          type,
490                          not))
491             {
492
493                 return false;
494
495             }
496
497             }
498
499         } else {
500
501             Iterator JavaDoc cli = cl.iterator ();
502
503             while (cli.hasNext ())
504             {
505
506             l = Utilities.lowerValue (cli.next (),
507                           ignoreCase);
508
509             if (!Utilities.compare2 (l,
510                          r,
511                          type,
512                          not))
513             {
514
515                 return false;
516
517             }
518
519             }
520
521         }
522
523         // All values in the LHS match the condition for the RHS
524
// value.
525
return true;
526
527         }
528
529     } else {
530
531         // See if the RHS is a collection.
532
if (r instanceof Collection JavaDoc)
533         {
534
535         l = Utilities.lowerValue (l,
536                       ignoreCase);
537
538         Collection JavaDoc cr = (Collection JavaDoc) r;
539
540         if (cr instanceof List JavaDoc)
541         {
542
543             List JavaDoc rl = (List JavaDoc) cr;
544
545             int rs = rl.size () - 1;
546
547             for (int i = rs; i > -1; i--)
548             {
549
550             r = Utilities.lowerValue (rl.get (i),
551                           ignoreCase);
552
553             if (!Utilities.compare2 (l,
554                          r,
555                          type,
556                          not))
557             {
558
559                 return false;
560
561             }
562
563             }
564
565         } else {
566
567             Iterator JavaDoc cri = cr.iterator ();
568
569             while (cri.hasNext ())
570             {
571
572             r = Utilities.lowerValue (cri.next (),
573                           ignoreCase);
574
575             if (!Utilities.compare2 (l,
576                          r,
577                          type,
578                          not))
579             {
580
581                 return false;
582
583             }
584
585             }
586
587         }
588
589         // All values in the RHS match the condition for the LHS
590
// value.
591
return true;
592
593         }
594
595     }
596
597     // Just vanilla objects, so compare.
598
// Note: we perform the "lower" at this point because (from above)
599
// we want to ensure that the lower is only ever performed once and
600
// it's not until now that we can be sure that the objects are not collections.
601
return Utilities.compare2 (Utilities.lowerValue (l,
602                              ignoreCase),
603                    Utilities.lowerValue (r,
604                              ignoreCase),
605                    type,
606                    not);
607
608     }
609
610     private static Object JavaDoc lowerValue (Object JavaDoc o,
611                       boolean ignoreCase)
612     {
613
614     if (ignoreCase)
615     {
616
617         o = o.toString ();
618         
619         if (o != null)
620         {
621
622         o = ((String JavaDoc) o).toLowerCase ();
623         
624         }
625
626     }
627
628     return o;
629
630     }
631
632     private static boolean compare2 (Object JavaDoc l,
633                      Object JavaDoc r,
634                      int type,
635                      boolean not)
636     {
637
638     int c = Utilities.compare (l,
639                    r);
640     
641     // Use the direct values here for speed.
642

643     // Check for >
644
if ((type == 0)
645         &&
646         (c < 1)
647        )
648     {
649
650         // The LHS value is equal to or less than
651
// the RHS value, but we expect >, so can safely
652
// return false.
653
if (not)
654         {
655
656         return true;
657
658         }
659
660         return false;
661         
662     }
663     
664     // Check for >= or =
665
if (((type == 1)
666          ||
667          (type == 4)
668         )
669         &&
670         (c < 0)
671        )
672     {
673
674         // The RHS value is less than the LHS value
675
// but we expect >=, so can safely return false.
676
if (not)
677         {
678
679         return true;
680
681         }
682
683         return false;
684         
685     }
686
687     // Check for <
688
if ((type == 2)
689         &&
690         (c > -1)
691        )
692     {
693
694         // The RHS value is greater than or equal to
695
// the LHS value but we expect <, so can safely return
696
// false.
697
if (not)
698         {
699
700         return true;
701
702         }
703
704         return false;
705
706     }
707
708     // Check for <=
709
if (((type == 3)
710          ||
711          (type == 4)
712         )
713         &&
714         (c > 0)
715        )
716     {
717
718         // The RHS value is greater than the LHS value
719
// but we expect <=, so can safely return false.
720
if (not)
721         {
722
723         return true;
724
725         }
726
727         return false;
728         
729     }
730
731     if (not)
732     {
733
734         return false;
735
736     }
737
738     return true;
739
740     }
741
742     public static int compare (Object JavaDoc o1,
743                    Object JavaDoc o2)
744     {
745
746     if ((o1 == null)
747         &&
748         (o2 == null)
749        )
750     {
751
752         return 0;
753
754     }
755
756     if ((o1 == null)
757         ||
758         (o2 == null)
759        )
760     {
761
762         return -1;
763
764     }
765
766     if ((o1 instanceof Number JavaDoc)
767         &&
768         (o2 instanceof Number JavaDoc)
769        )
770     {
771
772         return Utilities.getDoubleObject (o1).compareTo (Utilities.getDoubleObject (o2));
773
774     }
775
776     if ((o1 instanceof Comparable JavaDoc)
777         &&
778         (o2 instanceof Comparable JavaDoc)
779         &&
780         (o1.getClass ().isAssignableFrom (o2.getClass ()))
781        )
782     {
783
784         return ((Comparable JavaDoc) o1).compareTo ((Comparable JavaDoc) o2);
785
786     }
787
788     // Force a string comparison.
789
String JavaDoc s1 = o1.toString ();
790     String JavaDoc s2 = o2.toString ();
791
792     return s1.compareTo (s2);
793
794     }
795
796     public static boolean isGTEquals (Object JavaDoc o1,
797                       Object JavaDoc o2)
798     {
799
800     return Utilities.matches (o1,
801                   o2,
802                   false,
803                   Utilities.GTE,
804                   false);
805
806     }
807
808     public static boolean isLTEquals (Object JavaDoc o1,
809                       Object JavaDoc o2)
810     {
811
812     return Utilities.matches (o1,
813                   o2,
814                   false,
815                   Utilities.LTE,
816                   false);
817
818     }
819
820     public static boolean isEquals (Object JavaDoc o1,
821                     Object JavaDoc o2)
822     {
823
824     return Utilities.compare (o1,
825                   o2) == 0;
826
827     }
828
829     public static Double JavaDoc getDoubleObject (Object JavaDoc o)
830     {
831
832     return new Double JavaDoc (Utilities.getDouble (o));
833
834     }
835
836     public static double getDouble (Object JavaDoc o)
837     {
838
839     return ((Number JavaDoc) o).doubleValue ();
840
841     }
842
843     public static boolean isNumber (Object JavaDoc o)
844     {
845
846     if (o == null)
847     {
848
849         return false;
850
851     }
852
853     return Utilities.pCNames.containsKey (o.getClass ().getName ());
854
855     }
856
857     public static boolean isNumber (Class JavaDoc c)
858     {
859
860     return Utilities.pCNames.containsKey (c.getName ());
861
862     }
863
864     public static String JavaDoc formatSignature (String JavaDoc name,
865                       Class JavaDoc[] ps)
866     {
867
868     StringBuffer JavaDoc buf = new StringBuffer JavaDoc (name);
869     buf.append ("(");
870     
871     if (ps != null)
872     {
873
874         for (int i = 0; i < ps.length; i++)
875         {
876             
877         buf.append (ps[i].getName ());
878         
879         if (i < (ps.length - 1))
880         {
881
882             buf.append (",");
883             
884         }
885         
886         }
887         
888     }
889     
890     buf.append (")");
891     
892     return buf.toString ();
893
894     }
895
896     public static boolean matchLikePattern (List JavaDoc p,
897                         Object JavaDoc lhs,
898                         boolean not,
899                         boolean ignoreCase)
900     {
901
902     if (lhs instanceof Collection JavaDoc)
903     {
904
905         return Utilities.matchLikePattern (p,
906                            (Collection JavaDoc) lhs,
907                            not,
908                            ignoreCase);
909
910     }
911
912     boolean v = Utilities.matchLikePattern (p,
913                         lhs,
914                         ignoreCase);
915
916     if ((!v)
917         &&
918         (not)
919        )
920     {
921
922         return true;
923
924     }
925
926     if ((v)
927         &&
928         (not)
929        )
930     {
931
932         return false;
933
934     }
935
936     return v;
937
938     }
939
940     public static boolean matchLikePattern (List JavaDoc p,
941                         Collection JavaDoc lhs,
942                         boolean not,
943                         boolean ignoreCase)
944     {
945
946     if (lhs instanceof List JavaDoc)
947     {
948
949         int s = lhs.size () - 1;
950
951         List JavaDoc l = (List JavaDoc) lhs;
952
953         for (int i = s; i > -1; i--)
954         {
955
956         Object JavaDoc o = l.get (i);
957
958         if (!Utilities.matchLikePattern (p,
959                          o,
960                          ignoreCase))
961         {
962
963             if (not)
964             {
965
966             return true;
967
968             }
969
970             return false;
971
972         }
973
974         }
975
976         if (not)
977         {
978
979         return false;
980
981         }
982
983         return true;
984
985     }
986
987     Iterator JavaDoc iter = lhs.iterator ();
988
989     while (iter.hasNext ())
990     {
991
992         Object JavaDoc o = iter.next ();
993
994         if (!Utilities.matchLikePattern (p,
995                          o,
996                          ignoreCase))
997         {
998
999         if (not)
1000        {
1001
1002            return true;
1003
1004        }
1005
1006        return false;
1007
1008        }
1009
1010    }
1011    
1012    if (not)
1013    {
1014
1015        return false;
1016
1017    }
1018
1019    return true;
1020
1021    }
1022
1023    public static boolean matchLikePattern (List JavaDoc p,
1024                        Object JavaDoc o,
1025                        boolean ignoreCase)
1026    {
1027
1028    if (o == null)
1029    {
1030
1031        return false;
1032
1033    }
1034
1035    String JavaDoc st = o.toString ();
1036    
1037    if (ignoreCase)
1038    {
1039
1040        st = st.toLowerCase ();
1041
1042    }
1043
1044    return Utilities.matchLikePattern (p,
1045                       st);
1046
1047    }
1048
1049    public static boolean matchLikePattern (List JavaDoc p,
1050                        String JavaDoc value)
1051    {
1052
1053    if (value == null)
1054    {
1055
1056        return false;
1057
1058    }
1059
1060        boolean accept = true;
1061
1062        String JavaDoc c = null;
1063        String JavaDoc pm = null;
1064
1065        int currPos = 0;
1066        int cmdPos = 0;
1067
1068    int s = p.size ();
1069
1070        while (cmdPos < s)
1071        {
1072
1073            c = (String JavaDoc) p.get (cmdPos);
1074            pm = (String JavaDoc) p.get (cmdPos + 1);
1075
1076            if (c.equals (Utilities.F))
1077            {
1078
1079                // if we are to find 'anything'
1080
// then we are done
1081
if (pm.equals (Utilities.A))
1082                {
1083
1084                    break;
1085
1086                }
1087
1088                // otherwise search for the param
1089
// from the curr pos
1090
int nextPos = value.indexOf (pm,
1091                         currPos);
1092
1093                if (nextPos >= 0)
1094                {
1095
1096                    // found it
1097
currPos = nextPos + pm.length ();
1098
1099                } else {
1100
1101                    accept = false;
1102            break;
1103
1104                }
1105
1106            } else {
1107
1108                if (c.equals (Utilities.E))
1109                {
1110
1111                    // if we are to expect 'nothing'
1112
// then we MUST be at the end of the string
1113
if (pm.equals (Utilities.N))
1114                    {
1115
1116                        if (currPos != value.length ())
1117                        {
1118
1119                            accept = false;
1120
1121                        }
1122
1123                        // since we expect nothing else,
1124
// we must finish here
1125
break;
1126
1127                    } else {
1128
1129                        // otherwise, check if the expected string
1130
// is at our current position
1131
int nextPos = value.indexOf (pm,
1132                             currPos);
1133
1134                        if (nextPos != currPos)
1135                        {
1136
1137                            accept = false;
1138                            break;
1139
1140                        }
1141
1142                        // if we've made it this far, then we've
1143
// found what we're looking for
1144
currPos += pm.length ();
1145
1146                    }
1147
1148                }
1149
1150            }
1151
1152            cmdPos += 2;
1153        }
1154
1155    return accept;
1156
1157    }
1158
1159    public static boolean matchLikePattern (List JavaDoc p,
1160                        String JavaDoc value,
1161                        boolean not)
1162    {
1163
1164    boolean accept = Utilities.matchLikePattern (p,
1165                             value);
1166
1167    if (not)
1168    {
1169
1170        return !accept;
1171
1172    }
1173
1174    return accept;
1175
1176    }
1177
1178    public static List JavaDoc getLikePattern (String JavaDoc value,
1179                       String JavaDoc wildcard)
1180    {
1181
1182    List JavaDoc p = new ArrayList JavaDoc ();
1183
1184        StringTokenizer JavaDoc t = new StringTokenizer JavaDoc (value,
1185                         wildcard,
1186                         true);
1187
1188        String JavaDoc tok = null;
1189
1190        while (t.hasMoreTokens ())
1191        {
1192
1193            tok = t.nextToken ();
1194
1195            if (tok.equals (wildcard))
1196            {
1197
1198                p.add (Utilities.F);
1199
1200                if (t.hasMoreTokens ())
1201                {
1202
1203                    tok = t.nextToken ();
1204                    p.add (tok);
1205
1206                } else {
1207
1208                    p.add (Utilities.A);
1209
1210                }
1211
1212            } else {
1213
1214                p.add (Utilities.E);
1215                p.add (tok);
1216
1217            }
1218
1219        }
1220
1221        if (!tok.equals (wildcard))
1222        {
1223
1224            p.add (Utilities.E);
1225            p.add (Utilities.N);
1226
1227        }
1228
1229    return p;
1230
1231    }
1232
1233    public static String JavaDoc stripQuotes (String JavaDoc s)
1234    {
1235
1236    if (s == null)
1237    {
1238
1239        return s;
1240
1241    }
1242
1243    if (((s.charAt (0) == '\'')
1244         &&
1245         (s.charAt (s.length () - 1) == '\'')
1246        )
1247        ||
1248        ((s.charAt (0) == '"')
1249         &&
1250         (s.charAt (s.length () - 1) == '"')
1251        )
1252       )
1253    {
1254
1255        return s.substring (1,
1256                s.length () - 1);
1257
1258    }
1259
1260    return s;
1261
1262    }
1263
1264    public static void getMethods (Class JavaDoc c,
1265                   String JavaDoc name,
1266                   int mods,
1267                   List JavaDoc ms)
1268    {
1269
1270    if (c == null)
1271    {
1272
1273        return;
1274
1275    }
1276
1277    Method JavaDoc[] meths = c.getDeclaredMethods ();
1278
1279    for (int i = 0; i < meths.length; i++)
1280    {
1281
1282        Method JavaDoc m = meths[i];
1283
1284        if ((m.getName ().equals (name))
1285        &&
1286        ((m.getModifiers () & mods) == mods)
1287           )
1288        {
1289
1290        if (!ms.contains (m))
1291        {
1292
1293            // This is one.
1294
ms.add (m);
1295
1296        }
1297
1298        }
1299
1300    }
1301
1302    // Now get all the super-classes.
1303
Class JavaDoc sup = c.getSuperclass ();
1304
1305    if (sup != null)
1306    {
1307
1308        Utilities.getMethods (sup,
1309                  name,
1310                  mods,
1311                  ms);
1312
1313    }
1314
1315    // Now work up through the super-classes/interfaces.
1316
Class JavaDoc[] ints = c.getInterfaces ();
1317
1318    for (int i = 0; i < ints.length; i++)
1319    {
1320
1321        Class JavaDoc in = ints[i];
1322
1323        Utilities.getMethods (in,
1324                  name,
1325                  mods,
1326                  ms);
1327
1328    }
1329
1330    }
1331
1332    public static int matchMethodArgs (Class JavaDoc[] args,
1333                       Class JavaDoc[] compArgs)
1334    {
1335
1336    if ((compArgs == null)
1337        &&
1338        (args == null)
1339       )
1340    {
1341
1342        return 2;
1343
1344    }
1345
1346    if ((compArgs == null)
1347        &&
1348        (args.length == 0)
1349       )
1350    {
1351
1352        return 2;
1353
1354    }
1355
1356    if ((compArgs == null)
1357        &&
1358        (args.length > 0)
1359       )
1360    {
1361
1362        return 0;
1363
1364    }
1365
1366    if (args.length != compArgs.length)
1367    {
1368
1369        return 0;
1370
1371    }
1372
1373    // The score here helps in argument resolution, a more specific argument
1374
// match (that is NOT expression in the method args) will score higher and
1375
// thus is a better match.
1376
int score = 0;
1377
1378    for (int i = 0; i < args.length; i++)
1379    {
1380
1381        Class JavaDoc c = args[i];
1382
1383        // See if the arg is object, which means "I can accept any type".
1384
if (c.getClass ().getName ().equals (Object JavaDoc.class.getName ()))
1385        {
1386
1387        score += 1;
1388
1389        continue;
1390
1391        }
1392
1393        Class JavaDoc cc = compArgs[i];
1394
1395        if (cc == null)
1396        {
1397
1398        // Can't match this arg.
1399
continue;
1400
1401        } else {
1402
1403        if (c.isAssignableFrom (cc))
1404        {
1405
1406            score += 2;
1407
1408            continue;
1409
1410        }
1411
1412        }
1413
1414        if ((Utilities.isNumber (cc))
1415        &&
1416        (Utilities.isNumber (c))
1417           )
1418        {
1419
1420        score += 1;
1421
1422        // This matches...
1423
continue;
1424
1425        }
1426
1427        if ((Utilities.isPrimitiveClass (c))
1428        &&
1429        (Utilities.isPrimitiveClass (cc))
1430           )
1431        {
1432
1433        // It is a primitive class as well, so now see if they are compatible.
1434
if (Utilities.getPrimitiveClass (c).isAssignableFrom (Utilities.getPrimitiveClass (cc)))
1435        {
1436
1437            score += 1;
1438
1439            // They are assignable...
1440
continue;
1441
1442        }
1443
1444        }
1445
1446        // See if the type is an object... this "may" mean
1447
// that we can match and it may not, it will be determined at runtime.
1448
if (cc.getName ().equals (Object JavaDoc.class.getName ()))
1449        {
1450
1451        score += 1;
1452
1453        continue;
1454
1455        }
1456
1457        // If we are here then we can't match this arg type...
1458
// No point checking any further...
1459
return 0;
1460
1461    }
1462
1463    // All args can be matched.
1464
return score;
1465
1466    }
1467
1468    public static Object JavaDoc[] convertArgs (Object JavaDoc[] args,
1469                    Class JavaDoc[] argTypes)
1470    {
1471
1472    if (args == null)
1473    {
1474
1475        return args;
1476
1477    }
1478
1479    Object JavaDoc[] nargs = new Object JavaDoc [args.length];
1480
1481    for (int i = 0; i < argTypes.length; i++)
1482    {
1483
1484        if (Utilities.isNumber (argTypes[i]))
1485        {
1486
1487        Class JavaDoc c = Utilities.getObjectClass (argTypes[i]);
1488
1489        // This arg is a number, need to now convert to the type in the args.
1490
Number JavaDoc arg = (Number JavaDoc) args[i];
1491
1492        if (Double JavaDoc.class.isAssignableFrom (c))
1493        {
1494
1495            nargs[i] = arg;
1496
1497            continue;
1498
1499        }
1500
1501        if (Short JavaDoc.class.isAssignableFrom (c))
1502        {
1503
1504            nargs[i] = new Short JavaDoc (arg.shortValue ());
1505
1506            continue;
1507
1508        }
1509
1510        if (Integer JavaDoc.class.isAssignableFrom (c))
1511        {
1512
1513            nargs[i] = new Integer JavaDoc (arg.intValue ());
1514
1515            continue;
1516
1517        }
1518
1519        if (Long JavaDoc.class.isAssignableFrom (c))
1520        {
1521
1522            nargs[i] = new Long JavaDoc (arg.longValue ());
1523
1524            continue;
1525
1526        }
1527
1528        if (Float JavaDoc.class.isAssignableFrom (c))
1529        {
1530
1531            nargs[i] = new Float JavaDoc (arg.floatValue ());
1532
1533            continue;
1534
1535        }
1536
1537        if (Byte JavaDoc.class.isAssignableFrom (c))
1538        {
1539
1540            nargs[i] = new Byte JavaDoc (arg.byteValue ());
1541
1542            continue;
1543
1544        }
1545
1546        } else {
1547
1548        nargs[i] = args[i];
1549
1550        }
1551
1552    }
1553
1554    return nargs;
1555
1556    }
1557
1558}
1559
Popular Tags