KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > Objects


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Apr 10, 2005
23  */

24 package org.lobobrowser.util;
25
26 /**
27  * @author J. H. S.
28  */

29 public class Objects {
30     /**
31      *
32      */

33     private Objects() {
34     }
35
36     public static boolean equals(Object JavaDoc obj1, Object JavaDoc obj2) {
37         return obj1 == null ? (obj2 == null) : (obj1.equals(obj2));
38     }
39     
40     public static boolean isBoxClass(Class JavaDoc clazz) {
41         return clazz == Integer JavaDoc.class ||
42                clazz == Boolean JavaDoc.class ||
43                clazz == Double JavaDoc.class ||
44                clazz == Float JavaDoc.class ||
45                clazz == Long JavaDoc.class ||
46                clazz == Byte JavaDoc.class ||
47                clazz == Short JavaDoc.class ||
48                clazz == Character JavaDoc.class;
49     }
50     
51     public static boolean areAssignableTo(Object JavaDoc[] objects, Class JavaDoc[] types) {
52         int length = objects.length;
53         if(length != types.length) {
54             return false;
55         }
56         for(int i = 0; i < length; i++) {
57             if(!isAssignableOrBox(objects[i], types[i])) {
58                 return false;
59             }
60         }
61         return true;
62     }
63     
64     public static boolean isAssignableOrBox(Object JavaDoc value, Class JavaDoc clazz) {
65         return clazz.isInstance(value) ||
66             (clazz.isPrimitive() && (
67                 (clazz == double.class && value instanceof Double JavaDoc) ||
68                 (clazz == int.class && value instanceof Integer JavaDoc) ||
69                 (clazz == byte.class && value instanceof Byte JavaDoc) ||
70                 (clazz == boolean.class && value instanceof Boolean JavaDoc) ||
71                 (clazz == char.class && value instanceof Character JavaDoc) ||
72                 (clazz == short.class && value instanceof Short JavaDoc) ||
73                 (clazz == long.class && value instanceof Long JavaDoc) ||
74                 (clazz == float.class && value instanceof Float JavaDoc)
75             )) ||
76             (isNumeric(clazz) && isNumeric(value))
77             ;
78     }
79     
80     private static boolean isNumeric(Class JavaDoc clazz) {
81         return Number JavaDoc.class.isAssignableFrom(clazz) ||
82                (clazz.isPrimitive() &&
83                     (clazz == int.class || clazz == double.class || clazz == byte.class || clazz == short.class || clazz == float.class || clazz == long.class)
84                );
85     }
86     
87     private static boolean isNumeric(Object JavaDoc value) {
88         if(value == null) {
89             return false;
90         }
91         return isNumeric(value.getClass());
92     }
93 }
94
Popular Tags