1 7 package com.inversoft.util; 8 9 10 import java.lang.reflect.Array ; 11 import java.util.Arrays ; 12 import java.util.Collection ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 17 18 27 public class ObjectTools { 28 29 36 public static String cleanToString(Object obj) { 37 38 if (obj == null) { 39 return ""; 40 } 41 42 return obj.toString(); 43 } 44 45 52 public static String toString(Object obj) { 53 return (obj == null) ? null : obj.toString(); 54 } 55 56 83 public static boolean areObjectsEqual(Object obj1, Object obj2) { 84 85 if (obj1 == obj2) { 87 return true; 88 } 89 90 if (obj1 == null || obj2 == null) { 92 return false; 93 } 94 95 if (obj1 instanceof String ) { 97 return areObjectsEqual((String ) obj1, obj2); 98 } else if (obj2 instanceof String ) { 99 return areObjectsEqual((String ) obj2, obj1); 100 } 101 102 boolean is1Array = obj1.getClass().isArray(); 103 boolean is2Array = obj2.getClass().isArray(); 104 boolean is1Collection = (obj1 instanceof Collection ); 105 boolean is2Collection = (obj2 instanceof Collection ); 106 107 if (is1Array && is2Array) { 108 return Arrays.equals((Object []) obj1, (Object []) obj2); 109 } else if (is1Array && is2Collection) { 110 return Arrays.equals((Object []) obj1, ((Collection ) obj2).toArray()); 111 } else if (is2Array && is1Collection) { 112 return Arrays.equals(((Collection ) obj1).toArray(), (Object []) obj2); 113 } else if (is1Array) { 114 return arrayContains((Object []) obj1, obj2); 115 } else if (is2Array) { 116 return arrayContains((Object []) obj2, obj1); 117 } else if (is1Collection && is2Collection) { 118 return ((Collection ) obj1).containsAll( (Collection ) obj2 ); 119 } else if (is1Collection) { 120 return ((Collection ) obj1).contains(obj2); 121 } else if (is2Collection) { 122 return ((Collection ) obj2).contains(obj1); 123 } 124 125 return obj1.equals(obj2); 126 } 127 128 147 public static boolean areObjectsEqual(String string, Object obj) { 148 149 if (string == obj) { 150 return true; 151 } 152 153 if (string == null || obj == null) { 154 return false; 155 } 156 157 if (obj instanceof Object []) { 158 return arrayContainsString((Object []) obj, string); 159 } else if (obj instanceof Collection ) { 160 return arrayContainsString(((Collection ) obj).toArray(), string); 161 } 162 163 return string.equals(obj.toString()); 164 } 165 166 178 public static boolean arrayContains(Object [] array, Object obj) { 179 180 if (array == obj) { 181 return true; 182 } 183 184 if (array == null || obj == null) { 185 return false; 186 } 187 188 if (obj instanceof String ) { 189 return arrayContainsString(array, (String ) obj); 190 } 191 192 for (int i = 0; i < array.length; i++) { 193 194 if (array[i] != null && areObjectsEqual(array[i], obj)) { 195 return true; 196 } 197 } 198 199 return false; 200 } 201 202 212 public static boolean arrayContainsString(Object [] array, String string) { 213 214 if (array == null && string == null) { 215 return true; 216 } 217 218 if (array == null || string == null) { 219 return false; 220 } 221 222 for (int i = 0; i < array.length; i++) { 223 224 if (array[i] != null && string.equals(array[i].toString())) { 225 return true; 226 } 227 } 228 229 return false; 230 } 231 232 239 public static Number [] wrapArray(Object array) { 240 Number [] numbers = null; 241 if (array instanceof byte[]) { 242 byte[] bytes = (byte[]) array; 243 numbers = new Byte [bytes.length]; 244 for (int i = 0; i < bytes.length; i++) { 245 numbers[i] = new Byte (bytes[i]); 246 } 247 } else if (array instanceof double[]) { 248 double[] doubles = (double[]) array; 249 numbers = new Double [doubles.length]; 250 for (int i = 0; i < doubles.length; i++) { 251 numbers[i] = new Double (doubles[i]); 252 } 253 } else if (array instanceof float[]) { 254 float[] floats = (float[]) array; 255 numbers = new Float [floats.length]; 256 for (int i = 0; i < floats.length; i++) { 257 numbers[i] = new Float (floats[i]); 258 } 259 } else if (array instanceof int[]) { 260 int[] ints = (int[]) array; 261 numbers = new Integer [ints.length]; 262 for (int i = 0; i < ints.length; i++) { 263 numbers[i] = new Integer (ints[i]); 264 } 265 } else if (array instanceof long[]) { 266 long[] longs = (long[]) array; 267 numbers = new Long [longs.length]; 268 for (int i = 0; i < longs.length; i++) { 269 numbers[i] = new Long (longs[i]); 270 } 271 } else if (array instanceof short[]) { 272 short[] shorts = (short[]) array; 273 numbers = new Short [shorts.length]; 274 for (int i = 0; i < shorts.length; i++) { 275 numbers[i] = new Short (shorts[i]); 276 } 277 } 278 279 return numbers; 280 } 281 282 298 public static Object getValueFromCollection(final Object dataStructure, 299 final Object key) { 300 Object value = null; 301 302 if (dataStructure instanceof Map ) { 304 value = ((Map ) dataStructure).get(key); 305 } else { 306 Class type = dataStructure.getClass(); 307 boolean isList = (List .class.isAssignableFrom(type)); 308 boolean isCollection = (Collection .class.isAssignableFrom(type)); 309 boolean isArray = (type.isArray()); 310 311 if (!isList && !isArray && !isCollection) { 312 throw new IllegalArgumentException ("Invalid data structure type: " + 313 type.getName()); 314 } 315 316 int index = -1; 317 try { 318 index = ((Integer ) key).intValue(); 319 } catch (ClassCastException cce) { 320 throw new IllegalArgumentException ("Invalid indices/key: " + key + 321 " for data strucutre of type " + type.getName()); 322 } 323 324 if (isList) { 325 value = ((List ) dataStructure).get(index); 326 } else if (isCollection) { 327 328 Iterator iter = ((Collection ) dataStructure).iterator(); 330 for (int i = 0; i < index && iter.hasNext(); i++) { 331 iter.next(); 332 } 333 334 if (iter.hasNext()) { 335 value = iter.next(); 336 } else { 337 throw new IndexOutOfBoundsException ("Index of out bounds: " + 338 index); 339 } 340 } else if (isArray) { 341 value = ((Object []) dataStructure)[index]; 342 } 343 } 344 345 return value; 346 } 347 348 358 public static void setIntoArray(Object array, int index, Object value) { 359 Class type = array.getClass(); 360 if (!type.isArray()) { 361 throw new IllegalArgumentException ("Type not an array: " + 362 type.getName()); 363 } 364 365 Class compType = type.getComponentType(); 366 if (compType == Boolean.TYPE) { 367 ((boolean[]) array)[index] = ((Boolean ) value).booleanValue(); 368 } else if (compType == Byte.TYPE) { 369 ((byte[]) array)[index] = ((Byte ) value).byteValue(); 370 } else if (compType == Character.TYPE) { 371 ((char[]) array)[index] = ((Character ) value).charValue(); 372 } else if (compType == Double.TYPE) { 373 ((double[]) array)[index] = ((Double ) value).doubleValue(); 374 } else if (compType == Float.TYPE) { 375 ((float[]) array)[index] = ((Float ) value).floatValue(); 376 } else if (compType == Integer.TYPE) { 377 ((int[]) array)[index] = ((Integer ) value).intValue(); 378 } else if (compType == Long.TYPE) { 379 ((long[]) array)[index] = ((Long ) value).longValue(); 380 } else if (compType == Short.TYPE) { 381 ((short[]) array)[index] = ((Short ) value).shortValue(); 382 } else { 383 ((Object []) array)[index] = value; 384 } 385 } 386 387 402 public static void setValueIntoCollection(final Object dataStructure, 403 final Object key, final Object value) { 404 405 if (dataStructure instanceof Map ) { 407 ((Map ) dataStructure).put(key, value); 408 } else { 409 Class type = dataStructure.getClass(); 410 boolean isList = (List .class.isAssignableFrom(type)); 411 boolean isCollection = (Collection .class.isAssignableFrom(type)); 412 boolean isArray = (type.isArray()); 413 414 if (!isList && !isArray && !isCollection) { 415 throw new IllegalArgumentException ("Invalid data structure type: " + 416 type.getName()); 417 } 418 419 int index = -1; 420 if (key instanceof Integer ) { 421 index = ((Integer ) key).intValue(); 422 } else { 423 throw new IllegalArgumentException ("Invalid indices/key: " + key + 424 " for data strucutre of type " + type.getName()); 425 } 426 427 if (List .class.isAssignableFrom(type)) { 428 ((List ) dataStructure).set(index, value); 429 } else if (Collection .class.isAssignableFrom(type)) { 430 throw new IllegalArgumentException ("You can not set a property " + 431 "of type Collection using an indices. Use a List instead"); 432 } else if (type.isArray()) { 433 ObjectTools.setIntoArray(dataStructure, index, value); 434 } 435 } 436 } 437 438 444 public static boolean isCollection(Class type) { 445 return (Map .class.isAssignableFrom(type) || 446 Collection .class.isAssignableFrom(type)); 447 } 448 449 455 public static boolean isCollection(Object object) { 456 if (object == null) { 457 return false; 458 } 459 460 Class type = object.getClass(); 461 return (Map .class.isAssignableFrom(type) || 462 Collection .class.isAssignableFrom(type)); 463 } 464 465 471 public static boolean isArray(Object object) { 472 if (object == null) { 473 return false; 474 } 475 476 Class type = object.getClass(); 477 return type.isArray(); 478 } 479 480 487 public static boolean isDataStructure(Object object) { 488 if (object == null) { 489 return false; 490 } 491 492 Class type = object.getClass(); 493 return (type.isArray() || isCollection(type)); 494 } 495 496 518 public static ArrayInfo createArray(final Class type, final List indices, 519 final int start) { 520 int size = indices.size(); 521 int[] dims = new int[indices.size() - start]; 522 ArrayInfo ai = new ArrayInfo(); 523 ai.type = type; 524 525 int indexToUse = start; 526 int dimsIndex = 0; 527 Integer index; 528 while (ai.type.isArray() && indexToUse != size) { 529 try { 530 index = (Integer ) indices.get(indexToUse); 531 } catch (ClassCastException cce) { 532 throw new IllegalArgumentException ("Invalid index: " + cce); 533 } 534 535 dims[dimsIndex] = index.intValue() + 1; 536 ai.type = ai.type.getComponentType(); 537 dimsIndex++; 538 indexToUse++; 539 } 540 541 if (dimsIndex != dims.length) { 544 int[] oldDims = dims; 545 dims = new int[dimsIndex]; 546 System.arraycopy(oldDims, 0, dims, 0, dims.length); 547 } 548 549 ai.complete = (indexToUse == size && !ai.type.isArray()); 550 ai.array = Array.newInstance(ai.type, dims); 551 return ai; 552 } 553 554 557 public static class ArrayInfo { 558 559 563 public Class type; 564 565 568 public Object array; 569 570 573 public boolean complete; 574 } 575 } | Popular Tags |