KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > ObjectType


1 /*
2  * $Id: ObjectType.java 5570 2005-08-22 05:33:45Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util;
25
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import java.math.BigDecimal JavaDoc;
29 import java.text.DateFormat JavaDoc;
30 import java.text.NumberFormat JavaDoc;
31 import java.text.ParseException JavaDoc;
32 import java.text.SimpleDateFormat JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Locale JavaDoc;
37 import java.util.Map JavaDoc;
38
39 import javolution.util.FastMap;
40
41 /**
42  * Utilities for analyzing and converting Object types in Java
43  * Takes advantage of reflection
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
47  * @author <a HREF="mailto:gielen@aixcept.de">Rene Gielen</a>
48  * @version $Rev: 5570 $
49  * @since 2.0
50  */

51 public class ObjectType {
52     
53     public static final String JavaDoc module = ObjectType.class.getName();
54
55     public static final Object JavaDoc NULL = new NullObject();
56     
57     protected static Map JavaDoc classCache = new FastMap();
58
59     public static final String JavaDoc LANG_PACKAGE = "java.lang."; // We will test both the raw value and this + raw value
60
public static final String JavaDoc SQL_PACKAGE = "java.sql."; // We will test both the raw value and this + raw value
61

62     /**
63      * Loads a class with the current thread's context classloader
64      * @param className The name of the class to load
65      * @return The requested class
66      * @throws ClassNotFoundException
67      */

68     public static Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc {
69         // small block to speed things up by putting using preloaded classes for common objects, this turns out to help quite a bit...
70
Class JavaDoc theClass = (Class JavaDoc) CachedClassLoader.globalClassNameClassMap.get(className);
71
72         if (theClass != null) return theClass;
73
74         return loadClass(className, null);
75     }
76
77     /**
78      * Loads a class with the current thread's context classloader
79      * @param className The name of the class to load
80      * @param loader The ClassLoader to su
81      * @return The requested class
82      * @throws ClassNotFoundException
83      */

84     public static Class JavaDoc loadClass(String JavaDoc className, ClassLoader JavaDoc loader) throws ClassNotFoundException JavaDoc {
85         // small block to speed things up by putting using preloaded classes for common objects, this turns out to help quite a bit...
86
Class JavaDoc theClass = (Class JavaDoc) CachedClassLoader.globalClassNameClassMap.get(className);
87
88         if (theClass != null) return theClass;
89
90         if (loader == null) loader = Thread.currentThread().getContextClassLoader();
91
92         try {
93             theClass = loader.loadClass(className);
94         } catch (Exception JavaDoc e) {
95             theClass = (Class JavaDoc) classCache.get(className);
96             if (theClass == null) {
97                 synchronized (ObjectType.class) {
98                     theClass = (Class JavaDoc) classCache.get(className);
99                     if (theClass == null) {
100                         theClass = Class.forName(className);
101                         if (theClass != null) {
102                             if (Debug.verboseOn()) Debug.logVerbose("Loaded Class: " + theClass.getName(), module);
103                             classCache.put(className, theClass);
104                         }
105                     }
106                 }
107             }
108         }
109
110         return theClass;
111     }
112
113     /**
114      * Returns an instance of the specified class
115      * @param className Name of the class to instantiate
116      * @return An instance of the named class
117      * @throws ClassNotFoundException
118      * @throws InstantiationException
119      * @throws IllegalAccessException
120      */

121     public static Object JavaDoc getInstance(String JavaDoc className) throws ClassNotFoundException JavaDoc,
122             InstantiationException JavaDoc, IllegalAccessException JavaDoc {
123         Class JavaDoc c = loadClass(className);
124         Object JavaDoc o = c.newInstance();
125
126         if (Debug.verboseOn()) Debug.logVerbose("Instantiated object: " + o.toString(), module);
127         return o;
128     }
129
130     /**
131      * Tests if a class properly implements the specified interface
132      * @param objectClass Class to test
133      * @param interfaceName Name of the interface to test against
134      * @return boolean indicating whether interfaceName is an interface of the obj
135      * @throws ClassNotFoundException
136      */

137     public static boolean interfaceOf(Class JavaDoc objectClass, String JavaDoc interfaceName) throws ClassNotFoundException JavaDoc {
138         Class JavaDoc interfaceClass = loadClass(interfaceName);
139
140         return interfaceOf(objectClass, interfaceClass);
141     }
142
143     /**
144      * Tests if a class properly implements the specified interface
145      * @param objectClass Class to test
146      * @param interfaceObject to test against
147      * @return boolean indicating whether interfaceObject is an interface of the obj
148      */

149     public static boolean interfaceOf(Class JavaDoc objectClass, Object JavaDoc interfaceObject) {
150         Class JavaDoc interfaceClass = interfaceObject.getClass();
151
152         return interfaceOf(objectClass, interfaceClass);
153     }
154
155     /**
156      * Returns an instance of the specified class using the constructor matching the specified parameters
157      * @param className Name of the class to instantiate
158      * @param parameters Parameters passed to the constructor
159      * @return An instance of the named class
160      * @throws ClassNotFoundException
161      * @throws InstantiationException
162      * @throws IllegalAccessException
163      */

164     public static Object JavaDoc getInstance(String JavaDoc className, Object JavaDoc[] parameters) throws ClassNotFoundException JavaDoc,
165             InstantiationException JavaDoc, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc {
166         Class JavaDoc[] sig = new Class JavaDoc[parameters.length];
167         for (int i = 0; i < sig.length; i++) {
168             sig[i] = parameters[i].getClass();
169         }
170         Class JavaDoc c = loadClass(className);
171         Constructor JavaDoc con = c.getConstructor(sig);
172         Object JavaDoc o = con.newInstance(parameters);
173
174         if (Debug.verboseOn()) Debug.logVerbose("Instantiated object: " + o.toString(), module);
175         return o;
176     }
177
178     /**
179      * Tests if an object properly implements the specified interface
180      * @param obj Object to test
181      * @param interfaceName Name of the interface to test against
182      * @return boolean indicating whether interfaceName is an interface of the obj
183      * @throws ClassNotFoundException
184      */

185     public static boolean interfaceOf(Object JavaDoc obj, String JavaDoc interfaceName) throws ClassNotFoundException JavaDoc {
186         Class JavaDoc interfaceClass = loadClass(interfaceName);
187
188         return interfaceOf(obj, interfaceClass);
189     }
190
191     /**
192      * Tests if an object properly implements the specified interface
193      * @param obj Object to test
194      * @param interfaceObject to test against
195      * @return boolean indicating whether interfaceObject is an interface of the obj
196      */

197     public static boolean interfaceOf(Object JavaDoc obj, Object JavaDoc interfaceObject) {
198         Class JavaDoc interfaceClass = interfaceObject.getClass();
199
200         return interfaceOf(obj, interfaceClass);
201     }
202
203     /**
204      * Tests if an object properly implements the specified interface
205      * @param obj Object to test
206      * @param interfaceClass Class to test against
207      * @return boolean indicating whether interfaceClass is an interface of the obj
208      */

209     public static boolean interfaceOf(Object JavaDoc obj, Class JavaDoc interfaceClass) {
210         Class JavaDoc objectClass = obj.getClass();
211
212         return interfaceOf(objectClass, interfaceClass);
213     }
214
215     /**
216      * Tests if a class properly implements the specified interface
217      * @param objectClass Class to test
218      * @param interfaceClass Class to test against
219      * @return boolean indicating whether interfaceClass is an interface of the obj
220      */

221     public static boolean interfaceOf(Class JavaDoc objectClass, Class JavaDoc interfaceClass) {
222         while (objectClass != null) {
223             Class JavaDoc[] ifaces = objectClass.getInterfaces();
224
225             for (int i = 0; i < ifaces.length; i++) {
226                 if (ifaces[i] == interfaceClass) return true;
227             }
228             objectClass = objectClass.getSuperclass();
229         }
230         return false;
231     }
232
233     /**
234      * Tests if a class is a class of or a sub-class of the parent
235      * @param objectClass Class to test
236      * @param parentName Name of the parent class to test against
237      * @return
238      * @throws ClassNotFoundException
239      */

240     public static boolean isOrSubOf(Class JavaDoc objectClass, String JavaDoc parentName) throws ClassNotFoundException JavaDoc {
241         Class JavaDoc parentClass = loadClass(parentName);
242
243         return isOrSubOf(objectClass, parentClass);
244     }
245
246     /**
247      * Tests if a class is a class of or a sub-class of the parent
248      * @param objectClass Class to test
249      * @param parentObject Object to test against
250      * @return
251      */

252     public static boolean isOrSubOf(Class JavaDoc objectClass, Object JavaDoc parentObject) {
253         Class JavaDoc parentClass = parentObject.getClass();
254
255         return isOrSubOf(objectClass, parentClass);
256     }
257
258     /**
259      * Tests if an object is an instance of or a sub-class of the parent
260      * @param obj Object to test
261      * @param parentName Name of the parent class to test against
262      * @return
263      * @throws ClassNotFoundException
264      */

265     public static boolean isOrSubOf(Object JavaDoc obj, String JavaDoc parentName) throws ClassNotFoundException JavaDoc {
266         Class JavaDoc parentClass = loadClass(parentName);
267
268         return isOrSubOf(obj, parentClass);
269     }
270
271     /**
272      * Tests if an object is an instance of or a sub-class of the parent
273      * @param obj Object to test
274      * @param parentObject Object to test against
275      * @return
276      */

277     public static boolean isOrSubOf(Object JavaDoc obj, Object JavaDoc parentObject) {
278         Class JavaDoc parentClass = parentObject.getClass();
279
280         return isOrSubOf(obj, parentClass);
281     }
282
283     /**
284      * Tests if an object is an instance of or a sub-class of the parent
285      * @param obj Object to test
286      * @param parentClass Class to test against
287      * @return
288      */

289     public static boolean isOrSubOf(Object JavaDoc obj, Class JavaDoc parentClass) {
290         Class JavaDoc objectClass = obj.getClass();
291
292         return isOrSubOf(objectClass, parentClass);
293     }
294
295     /**
296      * Tests if a class is a class of or a sub-class of the parent
297      * @param objectClass Class to test
298      * @param parentClass Class to test against
299      * @return
300      */

301     public static boolean isOrSubOf(Class JavaDoc objectClass, Class JavaDoc parentClass) {
302
303         while (objectClass != null) {
304             if (objectClass == parentClass) return true;
305             objectClass = objectClass.getSuperclass();
306         }
307         return false;
308     }
309
310     /**
311      * Tests if a class is a class of a sub-class of or properly implements an interface
312      * @param objectClass Class to test
313      * @param typeObject Object to test against
314      * @return
315      */

316     public static boolean instanceOf(Class JavaDoc objectClass, Object JavaDoc typeObject) {
317         Class JavaDoc typeClass = typeObject.getClass();
318
319         return instanceOf(objectClass, typeClass);
320     }
321
322     /**
323      * Tests if a class is a class of a sub-class of or properly implements an interface
324      * @param objectClass Class to test
325      * @param typeName name to test against
326      * @return
327      */

328     public static boolean instanceOf(Class JavaDoc objectClass, String JavaDoc typeName) {
329         return instanceOf(objectClass, typeName, null);
330     }
331
332     /**
333      * Tests if an object is an instance of a sub-class of or properly implements an interface
334      * @param obj Object to test
335      * @param typeObject Object to test against
336      * @return
337      */

338     public static boolean instanceOf(Object JavaDoc obj, Object JavaDoc typeObject) {
339         Class JavaDoc typeClass = typeObject.getClass();
340
341         return instanceOf(obj, typeClass);
342     }
343
344     /**
345      * Tests if an object is an instance of a sub-class of or properly implements an interface
346      * @param obj Object to test
347      * @param typeName name to test against
348      * @return
349      */

350     public static boolean instanceOf(Object JavaDoc obj, String JavaDoc typeName) {
351         return instanceOf(obj, typeName, null);
352     }
353
354     /**
355      * Tests if a class is a class of a sub-class of or properly implements an interface
356      * @param objectClass Class to test
357      * @param typeName Object to test against
358      * @param loader
359      * @return
360      */

361     public static boolean instanceOf(Class JavaDoc objectClass, String JavaDoc typeName, ClassLoader JavaDoc loader) {
362         Class JavaDoc infoClass = loadInfoClass(typeName, loader);
363
364         if (infoClass == null)
365             throw new IllegalArgumentException JavaDoc("Illegal type found in info map (could not load class for specified type)");
366
367         return instanceOf(objectClass, infoClass);
368     }
369
370     /**
371      * Tests if an object is an instance of a sub-class of or properly implements an interface
372      * @param obj Object to test
373      * @param typeName Object to test against
374      * @param loader
375      * @return
376      */

377     public static boolean instanceOf(Object JavaDoc obj, String JavaDoc typeName, ClassLoader JavaDoc loader) {
378         Class JavaDoc infoClass = loadInfoClass(typeName, loader);
379
380         if (infoClass == null)
381             throw new IllegalArgumentException JavaDoc("Illegal type found in info map (could not load class for specified type)");
382
383         return instanceOf(obj, infoClass);
384     }
385
386     public static Class JavaDoc loadInfoClass(String JavaDoc typeName, ClassLoader JavaDoc loader) {
387         //Class infoClass = null;
388
try {
389             return ObjectType.loadClass(typeName, loader);
390         } catch (SecurityException JavaDoc se1) {
391             throw new IllegalArgumentException JavaDoc("Problems with classloader: security exception (" +
392                     se1.getMessage() + ")");
393         } catch (ClassNotFoundException JavaDoc e1) {
394             try {
395                 return ObjectType.loadClass(LANG_PACKAGE + typeName, loader);
396             } catch (SecurityException JavaDoc se2) {
397                 throw new IllegalArgumentException JavaDoc("Problems with classloader: security exception (" +
398                         se2.getMessage() + ")");
399             } catch (ClassNotFoundException JavaDoc e2) {
400                 try {
401                     return ObjectType.loadClass(SQL_PACKAGE + typeName, loader);
402                 } catch (SecurityException JavaDoc se3) {
403                     throw new IllegalArgumentException JavaDoc("Problems with classloader: security exception (" +
404                             se3.getMessage() + ")");
405                 } catch (ClassNotFoundException JavaDoc e3) {
406                     throw new IllegalArgumentException JavaDoc("Cannot find and load the class of type: " + typeName +
407                             " or of type: " + LANG_PACKAGE + typeName + " or of type: " + SQL_PACKAGE + typeName +
408                             ": (" + e3.getMessage() + ")");
409                 }
410             }
411         }
412     }
413
414     /**
415      * Tests if an object is an instance of a sub-class of or properly implements an interface
416      * @param obj Object to test
417      * @param typeClass Class to test against
418      * @return
419      */

420     public static boolean instanceOf(Object JavaDoc obj, Class JavaDoc typeClass) {
421         if (obj == null) return true;
422         Class JavaDoc objectClass = obj.getClass();
423         return instanceOf(objectClass, typeClass);
424     }
425
426     /**
427      * Tests if a class is a class of a sub-class of or properly implements an interface
428      * @param objectClass Class to test
429      * @param typeClass Class to test against
430      * @return
431      */

432     public static boolean instanceOf(Class JavaDoc objectClass, Class JavaDoc typeClass) {
433         if (typeClass.isInterface()) {
434             return interfaceOf(objectClass, typeClass);
435         } else {
436             return isOrSubOf(objectClass, typeClass);
437         }
438     }
439
440     /**
441      * Converts the passed object to the named simple type; supported types
442      * include: String, Boolean, Double, Float, Long, Integer, Date (java.sql.Date),
443      * Time, Timestamp;
444      * @param obj Object to convert
445      * @param type Name of type to convert to
446      * @param format Optional (can be null) format string for Date, Time, Timestamp
447      * @param locale Optional (can be null) Locale for formatting and parsing Double, Float, Long, Integer
448      * @param noTypeFail Fail (Exception) when no type conversion is available, false will return the primary object
449      * @return
450      * @throws GeneralException
451      */

452     public static Object JavaDoc simpleTypeConvert(Object JavaDoc obj, String JavaDoc type, String JavaDoc format, Locale JavaDoc locale, boolean noTypeFail) throws GeneralException {
453         if (obj == null) {
454             return null;
455         }
456
457         if (obj.getClass().getName().equals(type)) {
458             return obj;
459         }
460         if ("PlainString".equals(type)) {
461             return obj.toString();
462         }
463         if ("Object".equals(type) || "java.lang.Object".equals(type)) {
464             return obj;
465         }
466
467         String JavaDoc fromType = null;
468
469         if (obj instanceof java.lang.String JavaDoc) {
470             fromType = "String";
471             String JavaDoc str = (String JavaDoc) obj;
472             if ("String".equals(type) || "java.lang.String".equals(type)) {
473                 return obj;
474             }
475             if (str.length() == 0) {
476                 return null;
477             }
478
479             if ("Boolean".equals(type) || "java.lang.Boolean".equals(type)) {
480                 Boolean JavaDoc value = null;
481                 if (str.equalsIgnoreCase("TRUE"))
482                     value = new Boolean JavaDoc(true);
483                 else
484                     value = new Boolean JavaDoc(false);
485                 return value;
486             } else if ("Locale".equals(type) || "java.util.Locale".equals(type)) {
487                 Locale JavaDoc loc = UtilMisc.parseLocale(str);
488                 if (loc != null) {
489                     return loc;
490                 } else {
491                     throw new GeneralException("Could not convert " + str + " to " + type + ": ");
492                 }
493             } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) {
494                 try {
495                     NumberFormat JavaDoc nf = null;
496                     if (locale == null) {
497                         nf = NumberFormat.getNumberInstance();
498                     } else {
499                         nf = NumberFormat.getNumberInstance(locale);
500                     }
501                     Number JavaDoc tempNum = nf.parse(str);
502                     return new BigDecimal JavaDoc(tempNum.toString());
503                 } catch (ParseException JavaDoc e) {
504                     throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
505                 }
506             } else if ("Double".equals(type) || "java.lang.Double".equals(type)) {
507                 try {
508                     NumberFormat JavaDoc nf = null;
509                     if (locale == null) {
510                         nf = NumberFormat.getNumberInstance();
511                     } else {
512                         nf = NumberFormat.getNumberInstance(locale);
513                     }
514                     Number JavaDoc tempNum = nf.parse(str);
515
516                     return new Double JavaDoc(tempNum.doubleValue());
517                 } catch (ParseException JavaDoc e) {
518                     throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
519                 }
520             } else if ("Float".equals(type) || "java.lang.Float".equals(type)) {
521                 try {
522                     NumberFormat JavaDoc nf = null;
523                     if (locale == null) {
524                         nf = NumberFormat.getNumberInstance();
525                     } else {
526                         nf = NumberFormat.getNumberInstance(locale);
527                     }
528                     Number JavaDoc tempNum = nf.parse(str);
529
530                     return new Float JavaDoc(tempNum.floatValue());
531                 } catch (ParseException JavaDoc e) {
532                     throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
533                 }
534             } else if ("Long".equals(type) || "java.lang.Long".equals(type)) {
535                 try {
536                     NumberFormat JavaDoc nf = null;
537                     if (locale == null) {
538                         nf = NumberFormat.getNumberInstance();
539                     } else {
540                         nf = NumberFormat.getNumberInstance(locale);
541                     }
542                     nf.setMaximumFractionDigits(0);
543                     Number JavaDoc tempNum = nf.parse(str);
544
545                     return new Long JavaDoc(tempNum.longValue());
546                 } catch (ParseException JavaDoc e) {
547                     throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
548                 }
549             } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
550                 try {
551                     NumberFormat JavaDoc nf = null;
552                     if (locale == null) {
553                         nf = NumberFormat.getNumberInstance();
554                     } else {
555                         nf = NumberFormat.getNumberInstance(locale);
556                     }
557                     nf.setMaximumFractionDigits(0);
558                     Number JavaDoc tempNum = nf.parse(str);
559
560                     return new Integer JavaDoc(tempNum.intValue());
561                 } catch (ParseException JavaDoc e) {
562                     throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
563                 }
564             } else if ("Date".equals(type) || "java.sql.Date".equals(type)) {
565                 if (format == null || format.length() == 0) {
566                     try {
567                         return java.sql.Date.valueOf(str);
568                     } catch (Exception JavaDoc e) {
569                         try {
570                             DateFormat JavaDoc df = null;
571                             if (locale != null) {
572                                 df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
573                             } else {
574                                 df = DateFormat.getDateInstance(DateFormat.SHORT);
575                             }
576                             Date JavaDoc fieldDate = df.parse(str);
577
578                             return new java.sql.Date JavaDoc(fieldDate.getTime());
579                         } catch (ParseException JavaDoc e1) {
580                             throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
581                         }
582                     }
583                 } else {
584                     try {
585                         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(format);
586                         java.util.Date JavaDoc fieldDate = sdf.parse(str);
587                         return new java.sql.Date JavaDoc(fieldDate.getTime());
588                     } catch (ParseException JavaDoc e) {
589                         throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
590                     }
591                 }
592             } else if ("Time".equals(type) || "java.sql.Time".equals(type)) {
593                 if (format == null || format.length() == 0) {
594                     try {
595                         return java.sql.Time.valueOf(str);
596                     } catch (Exception JavaDoc e) {
597                         try {
598                             DateFormat JavaDoc df = null;
599                             if (locale != null) {
600                                 df = DateFormat.getTimeInstance(DateFormat.SHORT, locale);
601                             } else {
602                                 df = DateFormat.getTimeInstance(DateFormat.SHORT);
603                             }
604                             Date JavaDoc fieldDate = df.parse(str);
605
606                             return new java.sql.Time JavaDoc(fieldDate.getTime());
607                         } catch (ParseException JavaDoc e1) {
608                             throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
609                         }
610                     }
611                 } else {
612                     try {
613                         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(format);
614                         java.util.Date JavaDoc fieldDate = sdf.parse(str);
615                         return new java.sql.Time JavaDoc(fieldDate.getTime());
616                     } catch (ParseException JavaDoc e) {
617                         throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
618                     }
619                 }
620             } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) {
621                 if (format == null || format.length() == 0) {
622                     try {
623                         return java.sql.Timestamp.valueOf(str);
624                     } catch (Exception JavaDoc e) {
625                         try {
626                             DateFormat JavaDoc df = null;
627                             if (locale != null) {
628                                 df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
629                             } else {
630                                 df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
631                             }
632                             Date JavaDoc fieldDate = df.parse(str);
633                             return new java.sql.Timestamp JavaDoc(fieldDate.getTime());
634                         } catch (ParseException JavaDoc e1) {
635                             throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
636                         }
637                     }
638                 } else {
639                     try {
640                         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(format);
641                         java.util.Date JavaDoc fieldDate = sdf.parse(str);
642                         return new java.sql.Timestamp JavaDoc(fieldDate.getTime());
643                     } catch (ParseException JavaDoc e) {
644                         throw new GeneralException("Could not convert " + str + " to " + type + ": ", e);
645                     }
646                 }
647             } else {
648                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
649             }
650         } else if (obj instanceof Double JavaDoc) {
651             fromType = "Double";
652             Double JavaDoc dbl = (Double JavaDoc) obj;
653
654             if ("String".equals(type) || "java.lang.String".equals(type)) {
655                 NumberFormat JavaDoc nf = null;
656
657                 if (locale == null) {
658                     nf = NumberFormat.getNumberInstance();
659                 } else {
660                     nf = NumberFormat.getNumberInstance(locale);
661                 }
662                 return nf.format(dbl.doubleValue());
663             } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) {
664                 return new BigDecimal JavaDoc(dbl.doubleValue());
665             } else if ("Double".equals(type) || "java.lang.Double".equals(type)) {
666                 return obj;
667             } else if ("Float".equals(type) || "java.lang.Float".equals(type)) {
668                 return new Float JavaDoc(dbl.floatValue());
669             } else if ("Long".equals(type) || "java.lang.Long".equals(type)) {
670                 return new Long JavaDoc(Math.round(dbl.doubleValue()));
671             } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
672                 return new Integer JavaDoc((int) Math.round(dbl.doubleValue()));
673             } else {
674                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
675             }
676         } else if (obj instanceof Float JavaDoc) {
677             fromType = "Float";
678             Float JavaDoc flt = (Float JavaDoc) obj;
679
680             if ("String".equals(type)) {
681                 NumberFormat JavaDoc nf = null;
682
683                 if (locale == null)
684                     nf = NumberFormat.getNumberInstance();
685                 else
686                     nf = NumberFormat.getNumberInstance(locale);
687                 return nf.format(flt.doubleValue());
688             } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) {
689                 return new BigDecimal JavaDoc(flt.doubleValue());
690             } else if ("Double".equals(type)) {
691                 return new Double JavaDoc(flt.doubleValue());
692             } else if ("Float".equals(type)) {
693                 return obj;
694             } else if ("Long".equals(type)) {
695                 return new Long JavaDoc(Math.round(flt.doubleValue()));
696             } else if ("Integer".equals(type)) {
697                 return new Integer JavaDoc((int) Math.round(flt.doubleValue()));
698             } else {
699                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
700             }
701         } else if (obj instanceof Long JavaDoc) {
702             fromType = "Long";
703             Long JavaDoc lng = (Long JavaDoc) obj;
704
705             if ("String".equals(type) || "java.lang.String".equals(type)) {
706                 NumberFormat JavaDoc nf = null;
707                 if (locale == null) {
708                     nf = NumberFormat.getNumberInstance();
709                 } else {
710                     nf = NumberFormat.getNumberInstance(locale);
711                 }
712                 return nf.format(lng.longValue());
713             } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) {
714                 return BigDecimal.valueOf(lng.longValue());
715             } else if ("Double".equals(type) || "java.lang.Double".equals(type)) {
716                 return new Double JavaDoc(lng.doubleValue());
717             } else if ("Float".equals(type) || "java.lang.Float".equals(type)) {
718                 return new Float JavaDoc(lng.floatValue());
719             } else if ("Long".equals(type) || "java.lang.Long".equals(type)) {
720                 return obj;
721             } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
722                 return new Integer JavaDoc(lng.intValue());
723             } else {
724                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
725             }
726         } else if (obj instanceof Integer JavaDoc) {
727             fromType = "Integer";
728             Integer JavaDoc intgr = (Integer JavaDoc) obj;
729             if ("String".equals(type) || "java.lang.String".equals(type)) {
730                 NumberFormat JavaDoc nf = null;
731                 if (locale == null) {
732                     nf = NumberFormat.getNumberInstance();
733                 } else {
734                     nf = NumberFormat.getNumberInstance(locale);
735                 }
736                 return nf.format(intgr.longValue());
737             } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) {
738                 return BigDecimal.valueOf(intgr.longValue());
739             } else if ("Double".equals(type) || "java.lang.Double".equals(type)) {
740                 return new Double JavaDoc(intgr.doubleValue());
741             } else if ("Float".equals(type) || "java.lang.Float".equals(type)) {
742                 return new Float JavaDoc(intgr.floatValue());
743             } else if ("Long".equals(type) || "java.lang.Long".equals(type)) {
744                 return new Long JavaDoc(intgr.longValue());
745             } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
746                 return obj;
747             } else {
748                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
749             }
750         } else if (obj instanceof BigDecimal JavaDoc) {
751             fromType = "BigDecimal";
752             BigDecimal JavaDoc bigDec = (BigDecimal JavaDoc) obj;
753             if ("String".equals(type) || "java.lang.String".equals(type)) {
754                 NumberFormat JavaDoc nf = null;
755                 if (locale == null) {
756                     nf = NumberFormat.getNumberInstance();
757                 } else {
758                     nf = NumberFormat.getNumberInstance(locale);
759                 }
760                 return nf.format(bigDec.doubleValue());
761             } else if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) {
762                 return obj;
763             } else if ("Double".equals(type) || "java.lang.Double".equals(type)) {
764                 return new Double JavaDoc(bigDec.doubleValue());
765             } else if ("Float".equals(type) || "java.lang.Float".equals(type)) {
766                 return new Float JavaDoc(bigDec.floatValue());
767             } else if ("Long".equals(type) || "java.lang.Long".equals(type)) {
768                 return new Long JavaDoc(bigDec.longValue());
769             } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
770                 return new Integer JavaDoc(bigDec.intValue());
771             } else {
772                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
773             }
774         } else if (obj instanceof java.sql.Date JavaDoc) {
775             fromType = "Date";
776             java.sql.Date JavaDoc dte = (java.sql.Date JavaDoc) obj;
777             if ("String".equals(type) || "java.lang.String".equals(type)) {
778                 if (format == null || format.length() == 0) {
779                     return dte.toString();
780                 } else {
781                     SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(format);
782                     return sdf.format(new java.util.Date JavaDoc(dte.getTime()));
783                 }
784             } else if ("Date".equals(type) || "java.sql.Date".equals(type)) {
785                 return obj;
786             } else if ("Time".equals(type) || "java.sql.Time".equals(type)) {
787                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
788             } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) {
789                 return new java.sql.Timestamp JavaDoc(dte.getTime());
790             } else {
791                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
792             }
793         } else if (obj instanceof java.sql.Time JavaDoc) {
794             fromType = "Time";
795             java.sql.Time JavaDoc tme = (java.sql.Time JavaDoc) obj;
796
797             if ("String".equals(type) || "java.lang.String".equals(type)) {
798                 if (format == null || format.length() == 0) {
799                     return tme.toString();
800                 } else {
801                     SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(format);
802
803                     return sdf.format(new java.util.Date JavaDoc(tme.getTime()));
804                 }
805             } else if ("Date".equals(type) || "java.sql.Date".equals(type)) {
806                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
807             } else if ("Time".equals(type) || "java.sql.Time".equals(type)) {
808                 return obj;
809             } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) {
810                 return new java.sql.Timestamp JavaDoc(tme.getTime());
811             } else {
812                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
813             }
814         } else if (obj instanceof java.sql.Timestamp JavaDoc) {
815             fromType = "Timestamp";
816             java.sql.Timestamp JavaDoc tme = (java.sql.Timestamp JavaDoc) obj;
817
818             if ("String".equals(type) || "java.lang.String".equals(type)) {
819                 if (format == null || format.length() == 0) {
820                     return tme.toString();
821                 } else {
822                     SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(format);
823
824                     return sdf.format(new java.util.Date JavaDoc(tme.getTime()));
825                 }
826             } else if ("Date".equals(type) || "java.sql.Date".equals(type)) {
827                 return new java.sql.Date JavaDoc(tme.getTime());
828             } else if ("Time".equals(type) || "java.sql.Time".equals(type)) {
829                 return new java.sql.Time JavaDoc(tme.getTime());
830             } else if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) {
831                 return obj;
832             } else {
833                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
834             }
835         } else if (obj instanceof java.lang.Boolean JavaDoc) {
836             fromType = "Boolean";
837             Boolean JavaDoc bol = (Boolean JavaDoc) obj;
838             if ("Boolean".equals(type) || "java.lang.Boolean".equals(type)) {
839                 return bol;
840             } else if ("String".equals(type) || "java.lang.String".equals(type)) {
841                 return bol.toString();
842             } else if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
843                 if (bol.booleanValue())
844                     return new Integer JavaDoc(1);
845                 else
846                     return new Integer JavaDoc(0);
847             } else {
848                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
849             }
850         } else if (obj instanceof java.util.Locale JavaDoc) {
851             fromType = "Locale";
852             Locale JavaDoc loc = (Locale JavaDoc) obj;
853             if ("Locale".equals(type) || "java.util.Locale".equals(type)) {
854                 return loc;
855             } else if ("String".equals(type) || "java.lang.String".equals(type)) {
856                 return loc.toString();
857             } else {
858                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
859             }
860         } else if (obj.getClass().getName().equals("org.ofbiz.entity.GenericValue")) {
861             fromType = "GenericValue";
862             if ("GenericValue".equals(type) || "org.ofbiz.entity.GenericValue".equals(type)) {
863                 return obj;
864             } else if ("Map".equals(type) || "java.util.Map".equals(type)) {
865                 return obj;
866             } else if ("String".equals(type) || "java.lang.String".equals(type)) {
867                 return obj.toString();
868             } else {
869                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
870             }
871         } else if (obj instanceof java.util.Map JavaDoc) {
872             fromType = "Map";
873             Map JavaDoc map = (Map JavaDoc) obj;
874             if ("Map".equals(type) || "java.util.Map".equals(type)) {
875                 return map;
876             } else if ("String".equals(type) || "java.lang.String".equals(type)) {
877                 return map.toString();
878             } else {
879                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
880             }
881         } else if (obj instanceof java.util.List JavaDoc) {
882             fromType = "List";
883             List JavaDoc list = (List JavaDoc) obj;
884             if ("List".equals(type) || "java.util.List".equals(type)) {
885                 return list;
886             } else if ("String".equals(type) || "java.lang.String".equals(type)) {
887                 return list.toString();
888             } else {
889                 throw new GeneralException("Conversion from " + fromType + " to " + type + " not currently supported");
890             }
891         } else {
892             // we can pretty much always do a conversion to a String, so do that here
893
if ("String".equals(type) || "java.lang.String".equals(type)) {
894                 Debug.logWarning("No special conversion available for " + obj.getClass().getName() + " to String, returning object.toString().", module);
895                 return obj.toString();
896             }
897             
898             if (noTypeFail) {
899                 throw new GeneralException("Conversion from " + obj.getClass().getName() + " to " + type + " not currently supported");
900             } else {
901                 Debug.logWarning("No type conversion available for " + obj.getClass().getName() + " to " + type + ", returning original object.", module);
902                 return obj;
903             }
904         }
905     }
906
907     public static Object JavaDoc simpleTypeConvert(Object JavaDoc obj, String JavaDoc type, String JavaDoc format, Locale JavaDoc locale) throws GeneralException {
908         return simpleTypeConvert(obj, type, format, locale, true);
909     }
910
911     public static Boolean JavaDoc doRealCompare(Object JavaDoc value1, Object JavaDoc value2, String JavaDoc operator, String JavaDoc type, String JavaDoc format,
912         List JavaDoc messages, Locale JavaDoc locale, ClassLoader JavaDoc loader) {
913         boolean verboseOn = Debug.verboseOn();
914
915         if (verboseOn) Debug.logVerbose("Comparing value1: \"" + value1 + "\" " + operator + " value2:\"" + value2 + "\"", module);
916
917         try {
918             if (!"PlainString".equals(type)) {
919                 Class JavaDoc clz = ObjectType.loadClass(type, loader);
920                 type = clz.getName();
921             }
922         } catch (ClassNotFoundException JavaDoc e) {
923             Debug.logWarning("The specified type [" + type + "] is not a valid class or a known special type, may see more errors later because of this: " + e.getMessage(), module);
924         }
925
926         // some default behavior for null values, results in a bit cleaner operation
927
if ("is-null".equals(operator) && value1 == null) {
928             return Boolean.TRUE;
929         } else if ("is-not-null".equals(operator) && value1 == null) {
930             return Boolean.FALSE;
931         } else if ("is-empty".equals(operator) && value1 == null) {
932             return Boolean.TRUE;
933         } else if ("is-not-empty".equals(operator) && value1 == null) {
934             return Boolean.FALSE;
935         } else if ("contains".equals(operator) && value1 == null) {
936             return Boolean.FALSE;
937         }
938         
939         int result = 0;
940
941         Object JavaDoc convertedValue2 = null;
942         if (value2 != null) {
943             try {
944                 convertedValue2 = ObjectType.simpleTypeConvert(value2, type, format, locale);
945             } catch (GeneralException e) {
946                 Debug.logError(e, module);
947                 messages.add("Could not convert value2 for comparison: " + e.getMessage());
948                 return null;
949             }
950         }
951
952         // have converted value 2, now before converting value 1 see if it is a Collection and we are doing a contains comparison
953
if ("contains".equals(operator) && value1 instanceof Collection JavaDoc) {
954             Collection JavaDoc col1 = (Collection JavaDoc) value1;
955             if (col1.contains(convertedValue2)) {
956                 return Boolean.TRUE;
957             } else {
958                 return Boolean.FALSE;
959             }
960         }
961
962         Object JavaDoc convertedValue1 = null;
963         try {
964             convertedValue1 = ObjectType.simpleTypeConvert(value1, type, format, locale);
965         } catch (GeneralException e) {
966             Debug.logError(e, module);
967             messages.add("Could not convert value1 for comparison: " + e.getMessage());
968             return null;
969         }
970
971         // handle null values...
972
if (convertedValue1 == null || convertedValue2 == null) {
973             if ("equals".equals(operator)) {
974                 if (convertedValue1 == null && convertedValue2 == null) {
975                     return Boolean.TRUE;
976                 } else {
977                     return Boolean.FALSE;
978                 }
979             } else if ("not-equals".equals(operator)) {
980                 if (convertedValue1 == null && convertedValue2 == null) {
981                     return Boolean.FALSE;
982                 } else {
983                     return Boolean.TRUE;
984                 }
985             } else if ("is-not-empty".equals(operator) || "is-empty".equals(operator)) {
986                 // do nothing, handled later...
987
} else {
988                 if (convertedValue1 == null) {
989                     messages.add("Left value is null, cannot complete compare for the operator " + operator);
990                     return null;
991                 }
992                 if (convertedValue2 == null) {
993                     messages.add("Right value is null, cannot complete compare for the operator " + operator);
994                     return null;
995                 }
996             }
997         }
998
999         if ("contains".equals(operator)) {
1000            if ("java.lang.String".equals(type) || "PlainString".equals(type)) {
1001                String JavaDoc str1 = (String JavaDoc) convertedValue1;
1002                String JavaDoc str2 = (String JavaDoc) convertedValue2;
1003
1004                if (str1.indexOf(str2) < 0) {
1005                    return Boolean.FALSE;
1006                } else {
1007                    return Boolean.TRUE;
1008                }
1009            } else {
1010                messages.add("Error in XML file: cannot do a contains compare between a String and a non-String type");
1011                return null;
1012            }
1013        } else if ("is-empty".equals(operator)) {
1014            if (convertedValue1 == null)
1015                return Boolean.TRUE;
1016            if (convertedValue1 instanceof String JavaDoc && ((String JavaDoc) convertedValue1).length() == 0)
1017                return Boolean.TRUE;
1018            if (convertedValue1 instanceof List JavaDoc && ((List JavaDoc) convertedValue1).size() == 0)
1019                return Boolean.TRUE;
1020            if (convertedValue1 instanceof Map JavaDoc && ((Map JavaDoc) convertedValue1).size() == 0)
1021                return Boolean.TRUE;
1022            return Boolean.FALSE;
1023        } else if ("is-not-empty".equals(operator)) {
1024            if (convertedValue1 == null)
1025                return Boolean.FALSE;
1026            if (convertedValue1 instanceof String JavaDoc && ((String JavaDoc) convertedValue1).length() == 0)
1027                return Boolean.FALSE;
1028            if (convertedValue1 instanceof List JavaDoc && ((List JavaDoc) convertedValue1).size() == 0)
1029                return Boolean.FALSE;
1030            if (convertedValue1 instanceof Map JavaDoc && ((Map JavaDoc) convertedValue1).size() == 0)
1031                return Boolean.FALSE;
1032            return Boolean.TRUE;
1033        }
1034        
1035        if ("java.lang.String".equals(type) || "PlainString".equals(type)) {
1036            String JavaDoc str1 = (String JavaDoc) convertedValue1;
1037            String JavaDoc str2 = (String JavaDoc) convertedValue2;
1038
1039            if (str1.length() == 0 || str2.length() == 0) {
1040                if ("equals".equals(operator)) {
1041                    if (str1.length() == 0 && str2.length() == 0) {
1042                        return Boolean.TRUE;
1043                    } else {
1044                        return Boolean.FALSE;
1045                    }
1046                } else if ("not-equals".equals(operator)) {
1047                    if (str1.length() == 0 && str2.length() == 0) {
1048                        return Boolean.FALSE;
1049                    } else {
1050                        return Boolean.TRUE;
1051                    }
1052                } else {
1053                    messages.add("ERROR: Could not do a compare between strings with one empty string for the operator " + operator);
1054                    return null;
1055                }
1056            }
1057            result = str1.compareTo(str2);
1058        } else if ("java.lang.Double".equals(type) || "java.lang.Float".equals(type) || "java.lang.Long".equals(type) || "java.lang.Integer".equals(type) || "java.math.BigDecimal".equals(type)) {
1059            Number JavaDoc tempNum = (Number JavaDoc) convertedValue1;
1060            double value1Double = tempNum.doubleValue();
1061
1062            tempNum = (Number JavaDoc) convertedValue2;
1063            double value2Double = tempNum.doubleValue();
1064
1065            if (value1Double < value2Double)
1066                result = -1;
1067            else if (value1Double > value2Double)
1068                result = 1;
1069            else
1070                result = 0;
1071        } else if ("java.sql.Date".equals(type)) {
1072            java.sql.Date JavaDoc value1Date = (java.sql.Date JavaDoc) convertedValue1;
1073            java.sql.Date JavaDoc value2Date = (java.sql.Date JavaDoc) convertedValue2;
1074            result = value1Date.compareTo(value2Date);
1075        } else if ("java.sql.Time".equals(type)) {
1076            java.sql.Time JavaDoc value1Time = (java.sql.Time JavaDoc) convertedValue1;
1077            java.sql.Time JavaDoc value2Time = (java.sql.Time JavaDoc) convertedValue2;
1078            result = value1Time.compareTo(value2Time);
1079        } else if ("java.sql.Timestamp".equals(type)) {
1080            java.sql.Timestamp JavaDoc value1Timestamp = (java.sql.Timestamp JavaDoc) convertedValue1;
1081            java.sql.Timestamp JavaDoc value2Timestamp = (java.sql.Timestamp JavaDoc) convertedValue2;
1082            result = value1Timestamp.compareTo(value2Timestamp);
1083        } else if ("java.lang.Boolean".equals(type)) {
1084            Boolean JavaDoc value1Boolean = (Boolean JavaDoc) convertedValue1;
1085            Boolean JavaDoc value2Boolean = (Boolean JavaDoc) convertedValue2;
1086            if ("equals".equals(operator)) {
1087                if ((value1Boolean.booleanValue() && value2Boolean.booleanValue()) || (!value1Boolean.booleanValue() && !value2Boolean.booleanValue()))
1088                    result = 0;
1089                else
1090                    result = 1;
1091            } else if ("not-equals".equals(operator)) {
1092                if ((!value1Boolean.booleanValue() && value2Boolean.booleanValue()) || (value1Boolean.booleanValue() && !value2Boolean.booleanValue()))
1093                    result = 0;
1094                else
1095                    result = 1;
1096            } else {
1097                messages.add("Can only compare Booleans using the operators 'equals' or 'not-equals'");
1098                return null;
1099            }
1100        } else if ("java.lang.Object".equals(type)) {
1101            if (convertedValue1.equals(convertedValue2)) {
1102                result = 0;
1103            } else {
1104                result = 1;
1105            }
1106        } else {
1107            messages.add("Type \"" + type + "\" specified for compare not supported.");
1108            return null;
1109        }
1110
1111        if (verboseOn) Debug.logVerbose("Got Compare result: " + result + ", operator: " + operator, module);
1112        if ("less".equals(operator)) {
1113            if (result >= 0)
1114                return Boolean.FALSE;
1115        } else if ("greater".equals(operator)) {
1116            if (result <= 0)
1117                return Boolean.FALSE;
1118        } else if ("less-equals".equals(operator)) {
1119            if (result > 0)
1120                return Boolean.FALSE;
1121        } else if ("greater-equals".equals(operator)) {
1122            if (result < 0)
1123                return Boolean.FALSE;
1124        } else if ("equals".equals(operator)) {
1125            if (result != 0)
1126                return Boolean.FALSE;
1127        } else if ("not-equals".equals(operator)) {
1128            if (result == 0)
1129                return Boolean.FALSE;
1130        } else {
1131            messages.add("Specified compare operator \"" + operator + "\" not known.");
1132            return null;
1133        }
1134
1135        if (verboseOn) Debug.logVerbose("Returning true", module);
1136        return Boolean.TRUE;
1137    }
1138    
1139    public static boolean isEmpty(Object JavaDoc value) {
1140        if (value == null) return true;
1141        
1142        if (value instanceof String JavaDoc) {
1143            if (((String JavaDoc) value).length() == 0) {
1144                return true;
1145            }
1146        } else if (value instanceof Collection JavaDoc) {
1147            if (((Collection JavaDoc) value).size() == 0) {
1148                return true;
1149            }
1150        } else if (value instanceof Map JavaDoc) {
1151            if (((Map JavaDoc) value).size() == 0) {
1152                return true;
1153            }
1154        }
1155        return false;
1156    }
1157    
1158    public static final class NullObject {
1159        public NullObject() { }
1160        
1161        public String JavaDoc toString() {
1162            return "ObjectType.NullObject";
1163        }
1164        
1165        public boolean equals(Object JavaDoc other) {
1166            if (other instanceof NullObject) {
1167                // should do equality of object? don't think so, just same type
1168
return true;
1169            } else {
1170                return false;
1171            }
1172        }
1173    }
1174}
1175
Popular Tags