KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jstl > Coercions


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

16
17 package org.apache.taglibs.standard.lang.jstl;
18
19 import java.beans.PropertyEditor JavaDoc;
20 import java.beans.PropertyEditorManager JavaDoc;
21
22 /**
23  *
24  * <p>This class contains the logic for coercing data types before
25  * operators are applied to them.
26  *
27  * <p>The following is the list of rules applied for various type
28  * conversions.
29  *
30  * <ul><pre>
31  * Applying arithmetic operator
32  * Binary operator - A {+,-,*} B
33  * if A and B are null
34  * return 0
35  * if A or B is Float, Double, or String containing ".", "e", or "E"
36  * coerce both A and B to Double
37  * apply operator
38  * otherwise
39  * coerce both A and B to Long
40  * apply operator
41  * if operator results in exception (such as divide by 0), error
42  *
43  * Binary operator - A {/,div} B
44  * if A and B are null
45  * return 0
46  * otherwise
47  * coerce both A and B to Double
48  * apply operator
49  * if operator results in exception (such as divide by 0), error
50  *
51  * Binary operator - A {%,mod} B
52  * if A and B are null
53  * return 0
54  * if A or B is Float, Double, or String containing ".", "e" or "E"
55  * coerce both to Double
56  * apply operator
57  * otherwise
58  * coerce both A and B to Long
59  * apply operator
60  * if operator results in exception (such as divide by 0), error
61  *
62  * Unary minus operator - -A
63  * if A is null
64  * return 0
65  * if A is String
66  * if A contains ".", "e", or "E"
67  * coerce to Double, apply operator
68  * otherwise
69  * coerce to a Long and apply operator
70  * if A is Byte,Short,Integer,Long,Float,Double
71  * retain type, apply operator
72  * if operator results in exception, error
73  * otherwise
74  * error
75  *
76  * Applying "empty" operator - empty A
77  * if A is null
78  * return true
79  * if A is zero-length String
80  * return true
81  * if A is zero-length array
82  * return true
83  * if A is List and ((List) A).isEmpty()
84  * return true
85  * if A is Map and ((Map) A).isEmpty()
86  * return true
87  * otherwise
88  * return false
89  *
90  * Applying logical operators
91  * Binary operator - A {and,or} B
92  * coerce both A and B to Boolean, apply operator
93  * NOTE - operator stops as soon as expression can be determined, i.e.,
94  * A and B and C and D - if B is false, then only A and B is evaluated
95  * Unary not operator - not A
96  * coerce A to Boolean, apply operator
97  *
98  * Applying relational operator
99  * A {<,>,<=,>=,lt,gt,lte,gte} B
100  * if A==B
101  * if operator is >= or <=
102  * return true
103  * otherwise
104  * return false
105  * if A or B is null
106  * return false
107  * if A or B is Float or Double
108  * coerce both A and B to Double
109  * apply operator
110  * if A or B is Byte,Short,Character,Integer,Long
111  * coerce both A and B to Long
112  * apply operator
113  * if A or B is String
114  * coerce both A and B to String, compare lexically
115  * if A is Comparable
116  * if A.compareTo (B) throws exception
117  * error
118  * otherwise
119  * use result of A.compareTo(B)
120  * if B is Comparable
121  * if B.compareTo (A) throws exception
122  * error
123  * otherwise
124  * use result of B.compareTo(A)
125  * otherwise
126  * error
127  *
128  * Applying equality operator
129  * A {==,!=} B
130  * if A==B
131  * apply operator
132  * if A or B is null
133  * return false for ==, true for !=
134  * if A or B is Float or Double
135  * coerce both A and B to Double
136  * apply operator
137  * if A or B is Byte,Short,Character,Integer,Long
138  * coerce both A and B to Long
139  * apply operator
140  * if A or B is Boolean
141  * coerce both A and B to Boolean
142  * apply operator
143  * if A or B is String
144  * coerce both A and B to String, compare lexically
145  * otherwise
146  * if an error occurs while calling A.equals(B)
147  * error
148  * apply operator to result of A.equals(B)
149  *
150  * coercions
151  *
152  * coerce A to String
153  * A is String
154  * return A
155  * A is null
156  * return ""
157  * A.toString throws exception
158  * error
159  * otherwise
160  * return A.toString
161  *
162  * coerce A to primitive Number type N
163  * A is null or ""
164  * return 0
165  * A is Character
166  * convert to short, apply following rules
167  * A is Boolean
168  * error
169  * A is Number type N
170  * return A
171  * A is Number with less precision than N
172  * coerce quietly
173  * A is Number with greater precision than N
174  * coerce quietly
175  * A is String
176  * new N.valueOf(A) throws exception
177  * error
178  * return N.valueOf(A)
179  * otherwise
180  * error
181  *
182  * coerce A to Character should be
183  * A is null or ""
184  * return (char) 0
185  * A is Character
186  * return A
187  * A is Boolean
188  * error
189  * A is Number with less precision than short
190  * coerce quietly - return (char) A
191  * A is Number with greater precision than short
192  * coerce quietly - return (char) A
193  * A is String
194  * return A.charAt (0)
195  * otherwise
196  * error
197  *
198  * coerce A to Boolean
199  * A is null or ""
200  * return false
201  * A is Boolean
202  * return A
203  * A is String
204  * Boolean.valueOf(A) throws exception
205  * error
206  * return Boolean.valueOf(A)
207  * otherwise
208  * error
209  *
210  * coerce A to any other type T
211  * A is null
212  * return null
213  * A is assignable to T
214  * coerce quietly
215  * A is String
216  * T has no PropertyEditor
217  * if A is "", return null
218  * otherwise error
219  * T's PropertyEditor throws exception
220  * if A is "", return null
221  * otherwise error
222  * otherwise
223  * apply T's PropertyEditor
224  * otherwise
225  * error
226  * </pre></ul>
227  *
228  * @author Nathan Abramson - Art Technology Group
229  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: pierred $
230  **/

231
232 public class Coercions
233 {
234   //-------------------------------------
235
/**
236    *
237    * Coerces the given value to the specified class.
238    **/

239   public static Object JavaDoc coerce (Object JavaDoc pValue,
240                    Class JavaDoc pClass,
241                    Logger pLogger)
242     throws ELException
243   {
244     if (pClass == String JavaDoc.class) {
245       return coerceToString (pValue, pLogger);
246     }
247     else if (isPrimitiveNumberClass (pClass)) {
248       return coerceToPrimitiveNumber (pValue, pClass, pLogger);
249     }
250     else if (pClass == Character JavaDoc.class ||
251          pClass == Character.TYPE) {
252       return coerceToCharacter (pValue, pLogger);
253     }
254     else if (pClass == Boolean JavaDoc.class ||
255          pClass == Boolean.TYPE) {
256       return coerceToBoolean (pValue, pLogger);
257     }
258     else {
259       return coerceToObject (pValue, pClass, pLogger);
260     }
261   }
262
263   //-------------------------------------
264
/**
265    *
266    * Returns true if the given class is Byte, Short, Integer, Long,
267    * Float, Double
268    **/

269   static boolean isPrimitiveNumberClass (Class JavaDoc pClass)
270   {
271     return
272       pClass == Byte JavaDoc.class ||
273       pClass == Byte.TYPE ||
274       pClass == Short JavaDoc.class ||
275       pClass == Short.TYPE ||
276       pClass == Integer JavaDoc.class ||
277       pClass == Integer.TYPE ||
278       pClass == Long JavaDoc.class ||
279       pClass == Long.TYPE ||
280       pClass == Float JavaDoc.class ||
281       pClass == Float.TYPE ||
282       pClass == Double JavaDoc.class ||
283       pClass == Double.TYPE;
284   }
285
286   //-------------------------------------
287
/**
288    *
289    * Coerces the specified value to a String
290    **/

291   public static String JavaDoc coerceToString (Object JavaDoc pValue,
292                        Logger pLogger)
293     throws ELException
294   {
295     if (pValue == null) {
296       return "";
297     }
298     else if (pValue instanceof String JavaDoc) {
299       return (String JavaDoc) pValue;
300     }
301     else {
302       try {
303     return pValue.toString ();
304       }
305       catch (Exception JavaDoc exc) {
306     if (pLogger.isLoggingError ()) {
307       pLogger.logError (Constants.TOSTRING_EXCEPTION,
308                 exc,
309                 pValue.getClass ().getName ());
310     }
311     return "";
312       }
313     }
314   }
315
316   //-------------------------------------
317
/**
318    *
319    * Coerces a value to the given primitive number class
320    **/

321   public static Number JavaDoc coerceToPrimitiveNumber (Object JavaDoc pValue,
322                         Class JavaDoc pClass,
323                         Logger pLogger)
324     throws ELException
325   {
326     if (pValue == null ||
327     "".equals (pValue)) {
328       return coerceToPrimitiveNumber (0, pClass);
329     }
330     else if (pValue instanceof Character JavaDoc) {
331       char val = ((Character JavaDoc) pValue).charValue ();
332       return coerceToPrimitiveNumber ((short) val, pClass);
333     }
334     else if (pValue instanceof Boolean JavaDoc) {
335       if (pLogger.isLoggingError ()) {
336     pLogger.logError (Constants.BOOLEAN_TO_NUMBER,
337               pValue,
338               pClass.getName ());
339       }
340       return coerceToPrimitiveNumber (0, pClass);
341     }
342     else if (pValue.getClass () == pClass) {
343       return (Number JavaDoc) pValue;
344     }
345     else if (pValue instanceof Number JavaDoc) {
346       return coerceToPrimitiveNumber ((Number JavaDoc) pValue, pClass);
347     }
348     else if (pValue instanceof String JavaDoc) {
349       try {
350     return coerceToPrimitiveNumber ((String JavaDoc) pValue, pClass);
351       }
352       catch (Exception JavaDoc exc) {
353     if (pLogger.isLoggingError ()) {
354       pLogger.logError
355         (Constants.STRING_TO_NUMBER_EXCEPTION,
356          (String JavaDoc) pValue,
357          pClass.getName ());
358     }
359     return coerceToPrimitiveNumber (0, pClass);
360       }
361     }
362     else {
363       if (pLogger.isLoggingError ()) {
364     pLogger.logError
365       (Constants.COERCE_TO_NUMBER,
366        pValue.getClass ().getName (),
367        pClass.getName ());
368       }
369       return coerceToPrimitiveNumber (0, pClass);
370     }
371   }
372
373   //-------------------------------------
374
/**
375    *
376    * Coerces a value to an Integer, returning null if the coercion
377    * isn't possible.
378    **/

379   public static Integer JavaDoc coerceToInteger (Object JavaDoc pValue,
380                      Logger pLogger)
381     throws ELException
382   {
383     if (pValue == null) {
384       return null;
385     }
386     else if (pValue instanceof Character JavaDoc) {
387       return PrimitiveObjects.getInteger
388     ((int) (((Character JavaDoc) pValue).charValue ()));
389     }
390     else if (pValue instanceof Boolean JavaDoc) {
391       if (pLogger.isLoggingWarning ()) {
392     pLogger.logWarning (Constants.BOOLEAN_TO_NUMBER,
393                 pValue,
394                 Integer JavaDoc.class.getName ());
395       }
396       return PrimitiveObjects.getInteger
397     (((Boolean JavaDoc) pValue).booleanValue () ? 1 : 0);
398     }
399     else if (pValue instanceof Integer JavaDoc) {
400       return (Integer JavaDoc) pValue;
401     }
402     else if (pValue instanceof Number JavaDoc) {
403       return PrimitiveObjects.getInteger (((Number JavaDoc) pValue).intValue ());
404     }
405     else if (pValue instanceof String JavaDoc) {
406       try {
407     return Integer.valueOf ((String JavaDoc) pValue);
408       }
409       catch (Exception JavaDoc exc) {
410     if (pLogger.isLoggingWarning ()) {
411       pLogger.logWarning
412         (Constants.STRING_TO_NUMBER_EXCEPTION,
413          (String JavaDoc) pValue,
414          Integer JavaDoc.class.getName ());
415     }
416     return null;
417       }
418     }
419     else {
420       if (pLogger.isLoggingWarning ()) {
421     pLogger.logWarning
422       (Constants.COERCE_TO_NUMBER,
423        pValue.getClass ().getName (),
424        Integer JavaDoc.class.getName ());
425       }
426       return null;
427     }
428   }
429
430   //-------------------------------------
431
/**
432    *
433    * Coerces a long to the given primitive number class
434    **/

435   static Number JavaDoc coerceToPrimitiveNumber (long pValue,
436                      Class JavaDoc pClass)
437     throws ELException
438   {
439     if (pClass == Byte JavaDoc.class || pClass == Byte.TYPE) {
440       return PrimitiveObjects.getByte ((byte) pValue);
441     }
442     else if (pClass == Short JavaDoc.class || pClass == Short.TYPE) {
443       return PrimitiveObjects.getShort ((short) pValue);
444     }
445     else if (pClass == Integer JavaDoc.class || pClass == Integer.TYPE) {
446       return PrimitiveObjects.getInteger ((int) pValue);
447     }
448     else if (pClass == Long JavaDoc.class || pClass == Long.TYPE) {
449       return PrimitiveObjects.getLong ((long) pValue);
450     }
451     else if (pClass == Float JavaDoc.class || pClass == Float.TYPE) {
452       return PrimitiveObjects.getFloat ((float) pValue);
453     }
454     else if (pClass == Double JavaDoc.class || pClass == Double.TYPE) {
455       return PrimitiveObjects.getDouble ((double) pValue);
456     }
457     else {
458       return PrimitiveObjects.getInteger (0);
459     }
460   }
461
462   //-------------------------------------
463
/**
464    *
465    * Coerces a double to the given primitive number class
466    **/

467   static Number JavaDoc coerceToPrimitiveNumber (double pValue,
468                      Class JavaDoc pClass)
469     throws ELException
470   {
471     if (pClass == Byte JavaDoc.class || pClass == Byte.TYPE) {
472       return PrimitiveObjects.getByte ((byte) pValue);
473     }
474     else if (pClass == Short JavaDoc.class || pClass == Short.TYPE) {
475       return PrimitiveObjects.getShort ((short) pValue);
476     }
477     else if (pClass == Integer JavaDoc.class || pClass == Integer.TYPE) {
478       return PrimitiveObjects.getInteger ((int) pValue);
479     }
480     else if (pClass == Long JavaDoc.class || pClass == Long.TYPE) {
481       return PrimitiveObjects.getLong ((long) pValue);
482     }
483     else if (pClass == Float JavaDoc.class || pClass == Float.TYPE) {
484       return PrimitiveObjects.getFloat ((float) pValue);
485     }
486     else if (pClass == Double JavaDoc.class || pClass == Double.TYPE) {
487       return PrimitiveObjects.getDouble ((double) pValue);
488     }
489     else {
490       return PrimitiveObjects.getInteger (0);
491     }
492   }
493
494   //-------------------------------------
495
/**
496    *
497    * Coerces a Number to the given primitive number class
498    **/

499   static Number JavaDoc coerceToPrimitiveNumber (Number JavaDoc pValue,
500                      Class JavaDoc pClass)
501     throws ELException
502   {
503     if (pClass == Byte JavaDoc.class || pClass == Byte.TYPE) {
504       return PrimitiveObjects.getByte (pValue.byteValue ());
505     }
506     else if (pClass == Short JavaDoc.class || pClass == Short.TYPE) {
507       return PrimitiveObjects.getShort (pValue.shortValue ());
508     }
509     else if (pClass == Integer JavaDoc.class || pClass == Integer.TYPE) {
510       return PrimitiveObjects.getInteger (pValue.intValue ());
511     }
512     else if (pClass == Long JavaDoc.class || pClass == Long.TYPE) {
513       return PrimitiveObjects.getLong (pValue.longValue ());
514     }
515     else if (pClass == Float JavaDoc.class || pClass == Float.TYPE) {
516       return PrimitiveObjects.getFloat (pValue.floatValue ());
517     }
518     else if (pClass == Double JavaDoc.class || pClass == Double.TYPE) {
519       return PrimitiveObjects.getDouble (pValue.doubleValue ());
520     }
521     else {
522       return PrimitiveObjects.getInteger (0);
523     }
524   }
525
526   //-------------------------------------
527
/**
528    *
529    * Coerces a String to the given primitive number class
530    **/

531   static Number JavaDoc coerceToPrimitiveNumber (String JavaDoc pValue,
532                      Class JavaDoc pClass)
533     throws ELException
534   {
535     if (pClass == Byte JavaDoc.class || pClass == Byte.TYPE) {
536       return Byte.valueOf (pValue);
537     }
538     else if (pClass == Short JavaDoc.class || pClass == Short.TYPE) {
539       return Short.valueOf (pValue);
540     }
541     else if (pClass == Integer JavaDoc.class || pClass == Integer.TYPE) {
542       return Integer.valueOf (pValue);
543     }
544     else if (pClass == Long JavaDoc.class || pClass == Long.TYPE) {
545       return Long.valueOf (pValue);
546     }
547     else if (pClass == Float JavaDoc.class || pClass == Float.TYPE) {
548       return Float.valueOf (pValue);
549     }
550     else if (pClass == Double JavaDoc.class || pClass == Double.TYPE) {
551       return Double.valueOf (pValue);
552     }
553     else {
554       return PrimitiveObjects.getInteger (0);
555     }
556   }
557
558   //-------------------------------------
559
/**
560    *
561    * Coerces a value to a Character
562    **/

563   public static Character JavaDoc coerceToCharacter (Object JavaDoc pValue,
564                          Logger pLogger)
565     throws ELException
566   {
567     if (pValue == null ||
568     "".equals (pValue)) {
569       return PrimitiveObjects.getCharacter ((char) 0);
570     }
571     else if (pValue instanceof Character JavaDoc) {
572       return (Character JavaDoc) pValue;
573     }
574     else if (pValue instanceof Boolean JavaDoc) {
575       if (pLogger.isLoggingError ()) {
576     pLogger.logError (Constants.BOOLEAN_TO_CHARACTER, pValue);
577       }
578       return PrimitiveObjects.getCharacter ((char) 0);
579     }
580     else if (pValue instanceof Number JavaDoc) {
581       return PrimitiveObjects.getCharacter
582     ((char) ((Number JavaDoc) pValue).shortValue ());
583     }
584     else if (pValue instanceof String JavaDoc) {
585       String JavaDoc str = (String JavaDoc) pValue;
586       return PrimitiveObjects.getCharacter (str.charAt (0));
587     }
588     else {
589       if (pLogger.isLoggingError ()) {
590     pLogger.logError
591       (Constants.COERCE_TO_CHARACTER,
592        pValue.getClass ().getName ());
593       }
594       return PrimitiveObjects.getCharacter ((char) 0);
595     }
596   }
597
598   //-------------------------------------
599
/**
600    *
601    * Coerces a value to a Boolean
602    **/

603   public static Boolean JavaDoc coerceToBoolean (Object JavaDoc pValue,
604                      Logger pLogger)
605     throws ELException
606   {
607     if (pValue == null ||
608     "".equals (pValue)) {
609       return Boolean.FALSE;
610     }
611     else if (pValue instanceof Boolean JavaDoc) {
612       return (Boolean JavaDoc) pValue;
613     }
614     else if (pValue instanceof String JavaDoc) {
615       String JavaDoc str = (String JavaDoc) pValue;
616       try {
617     return Boolean.valueOf (str);
618       }
619       catch (Exception JavaDoc exc) {
620     if (pLogger.isLoggingError ()) {
621       pLogger.logError
622         (Constants.STRING_TO_BOOLEAN,
623          exc,
624          (String JavaDoc) pValue);
625     }
626     return Boolean.FALSE;
627       }
628     }
629     else {
630       if (pLogger.isLoggingError ()) {
631     pLogger.logError
632       (Constants.COERCE_TO_BOOLEAN,
633        pValue.getClass ().getName ());
634       }
635       return Boolean.TRUE;
636     }
637   }
638
639   //-------------------------------------
640
/**
641    *
642    * Coerces a value to the specified Class that is not covered by any
643    * of the above cases
644    **/

645   public static Object JavaDoc coerceToObject (Object JavaDoc pValue,
646                        Class JavaDoc pClass,
647                        Logger pLogger)
648     throws ELException
649   {
650     if (pValue == null) {
651       return null;
652     }
653     else if (pClass.isAssignableFrom (pValue.getClass ())) {
654       return pValue;
655     }
656     else if (pValue instanceof String JavaDoc) {
657       String JavaDoc str = (String JavaDoc) pValue;
658       PropertyEditor JavaDoc pe = PropertyEditorManager.findEditor (pClass);
659       if (pe == null) {
660     if ("".equals (str)) {
661       return null;
662     }
663     else {
664       if (pLogger.isLoggingError ()) {
665         pLogger.logError
666           (Constants.NO_PROPERTY_EDITOR,
667            str,
668            pClass.getName ());
669       }
670       return null;
671     }
672       }
673       try {
674     pe.setAsText (str);
675     return pe.getValue ();
676       }
677       catch (IllegalArgumentException JavaDoc exc) {
678     if ("".equals (str)) {
679       return null;
680     }
681     else {
682       if (pLogger.isLoggingError ()) {
683         pLogger.logError
684           (Constants.PROPERTY_EDITOR_ERROR,
685            exc,
686            pValue,
687            pClass.getName ());
688       }
689       return null;
690     }
691       }
692     }
693     else {
694       if (pLogger.isLoggingError ()) {
695     pLogger.logError
696       (Constants.COERCE_TO_OBJECT,
697        pValue.getClass ().getName (),
698        pClass.getName ());
699       }
700       return null;
701     }
702   }
703
704   //-------------------------------------
705
// Applying operators
706
//-------------------------------------
707
/**
708    *
709    * Performs all of the necessary type conversions, then calls on the
710    * appropriate operator.
711    **/

712   public static Object JavaDoc applyArithmeticOperator
713     (Object JavaDoc pLeft,
714      Object JavaDoc pRight,
715      ArithmeticOperator pOperator,
716      Logger pLogger)
717     throws ELException
718   {
719     if (pLeft == null &&
720     pRight == null) {
721       if (pLogger.isLoggingWarning ()) {
722     pLogger.logWarning
723       (Constants.ARITH_OP_NULL,
724        pOperator.getOperatorSymbol ());
725       }
726       return PrimitiveObjects.getInteger (0);
727     }
728
729     else if (isFloatingPointType (pLeft) ||
730          isFloatingPointType (pRight) ||
731          isFloatingPointString (pLeft) ||
732          isFloatingPointString (pRight)) {
733       double left =
734     coerceToPrimitiveNumber (pLeft, Double JavaDoc.class, pLogger).
735     doubleValue ();
736       double right =
737     coerceToPrimitiveNumber (pRight, Double JavaDoc.class, pLogger).
738     doubleValue ();
739       return
740     PrimitiveObjects.getDouble (pOperator.apply (left, right, pLogger));
741     }
742
743     else {
744       long left =
745     coerceToPrimitiveNumber (pLeft, Long JavaDoc.class, pLogger).
746     longValue ();
747       long right =
748     coerceToPrimitiveNumber (pRight, Long JavaDoc.class, pLogger).
749     longValue ();
750       return
751     PrimitiveObjects.getLong (pOperator.apply (left, right, pLogger));
752     }
753   }
754
755   //-------------------------------------
756
/**
757    *
758    * Performs all of the necessary type conversions, then calls on the
759    * appropriate operator.
760    **/

761   public static Object JavaDoc applyRelationalOperator
762     (Object JavaDoc pLeft,
763      Object JavaDoc pRight,
764      RelationalOperator pOperator,
765      Logger pLogger)
766     throws ELException
767   {
768     if (isFloatingPointType (pLeft) ||
769     isFloatingPointType (pRight)) {
770       double left =
771     coerceToPrimitiveNumber (pLeft, Double JavaDoc.class, pLogger).
772     doubleValue ();
773       double right =
774     coerceToPrimitiveNumber (pRight, Double JavaDoc.class, pLogger).
775     doubleValue ();
776       return
777     PrimitiveObjects.getBoolean (pOperator.apply (left, right, pLogger));
778     }
779
780     else if (isIntegerType (pLeft) ||
781          isIntegerType (pRight)) {
782       long left =
783     coerceToPrimitiveNumber (pLeft, Long JavaDoc.class, pLogger).
784     longValue ();
785       long right =
786     coerceToPrimitiveNumber (pRight, Long JavaDoc.class, pLogger).
787     longValue ();
788       return
789     PrimitiveObjects.getBoolean (pOperator.apply (left, right, pLogger));
790     }
791
792     else if (pLeft instanceof String JavaDoc ||
793          pRight instanceof String JavaDoc) {
794       String JavaDoc left = coerceToString (pLeft, pLogger);
795       String JavaDoc right = coerceToString (pRight, pLogger);
796       return
797     PrimitiveObjects.getBoolean (pOperator.apply (left, right, pLogger));
798     }
799
800     else if (pLeft instanceof Comparable JavaDoc) {
801       try {
802     int result = ((Comparable JavaDoc) pLeft).compareTo (pRight);
803     return
804       PrimitiveObjects.getBoolean
805       (pOperator.apply (result, -result, pLogger));
806       }
807       catch (Exception JavaDoc exc) {
808     if (pLogger.isLoggingError ()) {
809       pLogger.logError
810         (Constants.COMPARABLE_ERROR,
811          exc,
812          pLeft.getClass ().getName (),
813          (pRight == null) ? "null" : pRight.getClass ().getName (),
814          pOperator.getOperatorSymbol ());
815     }
816     return Boolean.FALSE;
817       }
818     }
819
820     else if (pRight instanceof Comparable JavaDoc) {
821       try {
822     int result = ((Comparable JavaDoc) pRight).compareTo (pLeft);
823     return
824       PrimitiveObjects.getBoolean
825       (pOperator.apply (-result, result, pLogger));
826       }
827       catch (Exception JavaDoc exc) {
828     if (pLogger.isLoggingError ()) {
829       pLogger.logError
830         (Constants.COMPARABLE_ERROR,
831          exc,
832          pRight.getClass ().getName (),
833          (pLeft == null) ? "null" : pLeft.getClass ().getName (),
834          pOperator.getOperatorSymbol ());
835     }
836     return Boolean.FALSE;
837       }
838     }
839
840     else {
841       if (pLogger.isLoggingError ()) {
842     pLogger.logError
843       (Constants.ARITH_OP_BAD_TYPE,
844        pOperator.getOperatorSymbol (),
845        pLeft.getClass ().getName (),
846        pRight.getClass ().getName ());
847       }
848       return Boolean.FALSE;
849     }
850   }
851
852   //-------------------------------------
853
/**
854    *
855    * Performs all of the necessary type conversions, then calls on the
856    * appropriate operator.
857    **/

858   public static Object JavaDoc applyEqualityOperator
859     (Object JavaDoc pLeft,
860      Object JavaDoc pRight,
861      EqualityOperator pOperator,
862      Logger pLogger)
863     throws ELException
864   {
865     if (pLeft == pRight) {
866       return PrimitiveObjects.getBoolean (pOperator.apply (true, pLogger));
867     }
868
869     else if (pLeft == null ||
870          pRight == null) {
871       return PrimitiveObjects.getBoolean (pOperator.apply (false, pLogger));
872     }
873
874     else if (isFloatingPointType (pLeft) ||
875          isFloatingPointType (pRight)) {
876       double left =
877     coerceToPrimitiveNumber (pLeft, Double JavaDoc.class, pLogger).
878     doubleValue ();
879       double right =
880     coerceToPrimitiveNumber (pRight, Double JavaDoc.class, pLogger).
881     doubleValue ();
882       return
883     PrimitiveObjects.getBoolean
884     (pOperator.apply (left == right, pLogger));
885     }
886
887     else if (isIntegerType (pLeft) ||
888          isIntegerType (pRight)) {
889       long left =
890     coerceToPrimitiveNumber (pLeft, Long JavaDoc.class, pLogger).
891     longValue ();
892       long right =
893     coerceToPrimitiveNumber (pRight, Long JavaDoc.class, pLogger).
894     longValue ();
895       return
896     PrimitiveObjects.getBoolean
897     (pOperator.apply (left == right, pLogger));
898     }
899
900     else if (pLeft instanceof Boolean JavaDoc ||
901          pRight instanceof Boolean JavaDoc) {
902       boolean left = coerceToBoolean (pLeft, pLogger).booleanValue ();
903       boolean right = coerceToBoolean (pRight, pLogger).booleanValue ();
904       return
905     PrimitiveObjects.getBoolean
906     (pOperator.apply (left == right, pLogger));
907     }
908
909     else if (pLeft instanceof String JavaDoc ||
910          pRight instanceof String JavaDoc) {
911       String JavaDoc left = coerceToString (pLeft, pLogger);
912       String JavaDoc right = coerceToString (pRight, pLogger);
913       return
914     PrimitiveObjects.getBoolean
915     (pOperator.apply (left.equals (right), pLogger));
916     }
917
918     else {
919       try {
920       return
921     PrimitiveObjects.getBoolean
922     (pOperator.apply (pLeft.equals (pRight), pLogger));
923       }
924       catch (Exception JavaDoc exc) {
925     if (pLogger.isLoggingError ()) {
926       pLogger.logError
927         (Constants.ERROR_IN_EQUALS,
928          exc,
929          pLeft.getClass ().getName (),
930          pRight.getClass ().getName (),
931          pOperator.getOperatorSymbol ());
932     }
933     return Boolean.FALSE;
934       }
935     }
936   }
937
938   //-------------------------------------
939
/**
940    *
941    * Returns true if the given Object is of a floating point type
942    **/

943   public static boolean isFloatingPointType (Object JavaDoc pObject)
944   {
945     return
946       pObject != null &&
947       isFloatingPointType (pObject.getClass ());
948   }
949
950   //-------------------------------------
951
/**
952    *
953    * Returns true if the given class is of a floating point type
954    **/

955   public static boolean isFloatingPointType (Class JavaDoc pClass)
956   {
957     return
958       pClass == Float JavaDoc.class ||
959       pClass == Float.TYPE ||
960       pClass == Double JavaDoc.class ||
961       pClass == Double.TYPE;
962   }
963
964   //-------------------------------------
965
/**
966    *
967    * Returns true if the given string might contain a floating point
968    * number - i.e., it contains ".", "e", or "E"
969    **/

970   public static boolean isFloatingPointString (Object JavaDoc pObject)
971   {
972     if (pObject instanceof String JavaDoc) {
973       String JavaDoc str = (String JavaDoc) pObject;
974       int len = str.length ();
975       for (int i = 0; i < len; i++) {
976     char ch = str.charAt (i);
977     if (ch == '.' ||
978         ch == 'e' ||
979         ch == 'E') {
980       return true;
981     }
982       }
983       return false;
984     }
985     else {
986       return false;
987     }
988   }
989
990   //-------------------------------------
991
/**
992    *
993    * Returns true if the given Object is of an integer type
994    **/

995   public static boolean isIntegerType (Object JavaDoc pObject)
996   {
997     return
998       pObject != null &&
999       isIntegerType (pObject.getClass ());
1000  }
1001
1002  //-------------------------------------
1003
/**
1004   *
1005   * Returns true if the given class is of an integer type
1006   **/

1007  public static boolean isIntegerType (Class JavaDoc pClass)
1008  {
1009    return
1010      pClass == Byte JavaDoc.class ||
1011      pClass == Byte.TYPE ||
1012      pClass == Short JavaDoc.class ||
1013      pClass == Short.TYPE ||
1014      pClass == Character JavaDoc.class ||
1015      pClass == Character.TYPE ||
1016      pClass == Integer JavaDoc.class ||
1017      pClass == Integer.TYPE ||
1018      pClass == Long JavaDoc.class ||
1019      pClass == Long.TYPE;
1020  }
1021
1022  //-------------------------------------
1023

1024}
1025
Popular Tags