1 22 package org.jboss.util; 23 24 import java.lang.reflect.Constructor ; 25 import java.lang.reflect.Array ; 26 27 import java.lang.ref.Reference ; 28 29 import java.io.IOException ; 30 import java.io.ObjectOutputStream ; 31 import java.io.ObjectInputStream ; 32 import java.io.ByteArrayOutputStream ; 33 import java.io.ByteArrayInputStream ; 34 import java.io.Serializable ; 35 36 import org.jboss.util.stream.Streams; 37 38 44 public final class Objects 45 { 46 50 57 public static Constructor getCompatibleConstructor(final Class type, 58 final Class valueType) 59 { 60 try { 62 return type.getConstructor(new Class [] { valueType }); 63 } 64 catch (Exception ignore) { 65 68 Class [] types = type.getClasses(); 70 71 for (int i=0; i<types.length; i++) { 72 try { 73 return type.getConstructor(new Class [] { types[i] }); 74 } 75 catch (Exception ignore2) {} 76 } 77 } 78 79 return null; 81 } 82 83 87 96 public static Object copy(final Serializable obj) 97 throws IOException , ClassNotFoundException 98 { 99 ObjectOutputStream out = null; 100 ObjectInputStream in = null; 101 Object copy = null; 102 103 try { 104 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 106 out = new ObjectOutputStream (baos); 107 out.writeObject(obj); 108 out.flush(); 109 110 byte data[] = baos.toByteArray(); 112 ByteArrayInputStream bais = new ByteArrayInputStream (data); 113 in = new ObjectInputStream (bais); 114 copy = in.readObject(); 115 } 116 finally { 117 Streams.close(out); 118 Streams.close(in); 119 } 120 121 return copy; 122 } 123 124 125 129 138 public static Object deref(final Object obj) { 139 if (obj != null && obj instanceof Reference ) { 140 Reference ref = (Reference )obj; 141 return ref.get(); 142 } 143 144 return obj; 145 } 146 147 153 public static boolean isArray(final Object obj) { 154 if (obj != null) 155 return obj.getClass().isArray(); 156 return false; 157 } 158 159 168 public static Object [] toArray(final Object obj) { 169 if (obj instanceof Object []) { 171 return (Object [])obj; 172 } 173 174 Class type = obj.getClass(); 176 Object array; 177 if (type.isArray()) { 178 int length = Array.getLength(obj); 179 Class componentType = type.getComponentType(); 180 array = Array.newInstance(componentType, length); 181 for (int i=0; i<length; i++) { 182 Array.set(array, i, Array.get(obj, i)); 183 } 184 } 185 else { 186 array = Array.newInstance(type, 1); 187 Array.set(array, 0, obj); 188 } 189 190 return (Object [])array; 191 } 192 193 201 public static boolean equals(final Object [] a, final Object [] b, 202 final boolean deep) 203 { 204 if (a == b) return true; 205 if (a == null || b == null) return false; 206 if (a.length != b.length) return false; 207 208 for (int i=0; i<a.length; i++) { 209 Object x = a[i]; 210 Object y = b[i]; 211 212 if (x != y) return false; 213 if (x == null || y == null) return false; 214 if (deep) { 215 if (x instanceof Object [] && y instanceof Object []) { 216 if (! equals((Object [])x, (Object [])y, true)) return false; 217 } 218 else { 219 return false; 220 } 221 } 222 if (! x.equals(y)) return false; 223 } 224 225 return true; 226 } 227 228 235 public static boolean equals(final Object [] a, final Object [] b) { 236 return equals(a, b, true); 237 } 238 } 239 | Popular Tags |