1 16 17 package org.springframework.util; 18 19 import java.lang.reflect.Array ; 20 import java.util.Arrays ; 21 22 35 public abstract class ObjectUtils { 36 37 private static final int INITIAL_HASH = 7; 38 private static final int MULTIPLIER = 31; 39 40 private static final String EMPTY_STRING = ""; 41 private static final String NULL_STRING = "null"; 42 private static final String ARRAY_START = "{"; 43 private static final String ARRAY_END = "}"; 44 private static final String EMPTY_ARRAY = ARRAY_START + ARRAY_END; 45 private static final String ARRAY_ELEMENT_SEPARATOR = ", "; 46 47 48 57 public static boolean isCheckedException(Throwable ex) { 58 return !(ex instanceof RuntimeException || ex instanceof Error ); 59 } 60 61 68 public static boolean isCompatibleWithThrowsClause(Throwable ex, Class [] declaredExceptions) { 69 if (!isCheckedException(ex)) { 70 return true; 71 } 72 if (declaredExceptions != null) { 73 for (int i = 0; i < declaredExceptions.length; i++) { 74 if (declaredExceptions[i].isAssignableFrom(ex.getClass())) { 75 return true; 76 } 77 } 78 } 79 return false; 80 } 81 82 88 public static boolean isEmpty(Object [] array) { 89 return (array == null || array.length == 0); 90 } 91 92 99 public static Object [] addObjectToArray(Object [] array, Object obj) { 100 Class compType = Object .class; 101 if (array != null) { 102 compType = array.getClass().getComponentType(); 103 } 104 else if (obj != null) { 105 compType = obj.getClass(); 106 } 107 int newArrLength = (array != null ? array.length + 1 : 1); 108 Object [] newArr = (Object []) Array.newInstance(compType, newArrLength); 109 if (array != null) { 110 System.arraycopy(array, 0, newArr, 0, array.length); 111 } 112 newArr[newArr.length - 1] = obj; 113 return newArr; 114 } 115 116 125 public static Object [] toObjectArray(Object source) { 126 if (source instanceof Object []) { 127 return (Object []) source; 128 } 129 if (source == null) { 130 return new Object [0]; 131 } 132 if (!source.getClass().isArray()) { 133 throw new IllegalArgumentException ("Source is not an array: " + source); 134 } 135 int length = Array.getLength(source); 136 if (length == 0) { 137 return new Object [0]; 138 } 139 Class wrapperType = Array.get(source, 0).getClass(); 140 Object [] newArray = (Object []) Array.newInstance(wrapperType, length); 141 for (int i = 0; i < length; i++) { 142 newArray[i] = Array.get(source, i); 143 } 144 return newArray; 145 } 146 147 148 152 163 public static boolean nullSafeEquals(Object o1, Object o2) { 164 if (o1 == o2) { 165 return true; 166 } 167 if (o1 == null || o2 == null) { 168 return false; 169 } 170 if (o1.equals(o2)) { 171 return true; 172 } 173 if (o1 instanceof Object [] && o2 instanceof Object []) { 174 return Arrays.equals((Object []) o1, (Object []) o2); 175 } 176 if (o1 instanceof boolean[] && o2 instanceof boolean[]) { 177 return Arrays.equals((boolean[]) o1, (boolean[]) o2); 178 } 179 if (o1 instanceof byte[] && o2 instanceof byte[]) { 180 return Arrays.equals((byte[]) o1, (byte[]) o2); 181 } 182 if (o1 instanceof char[] && o2 instanceof char[]) { 183 return Arrays.equals((char[]) o1, (char[]) o2); 184 } 185 if (o1 instanceof double[] && o2 instanceof double[]) { 186 return Arrays.equals((double[]) o1, (double[]) o2); 187 } 188 if (o1 instanceof float[] && o2 instanceof float[]) { 189 return Arrays.equals((float[]) o1, (float[]) o2); 190 } 191 if (o1 instanceof int[] && o2 instanceof int[]) { 192 return Arrays.equals((int[]) o1, (int[]) o2); 193 } 194 if (o1 instanceof long[] && o2 instanceof long[]) { 195 return Arrays.equals((long[]) o1, (long[]) o2); 196 } 197 if (o1 instanceof short[] && o2 instanceof short[]) { 198 return Arrays.equals((short[]) o1, (short[]) o2); 199 } 200 return false; 201 } 202 203 219 public static int nullSafeHashCode(Object obj) { 220 if (obj == null) { 221 return 0; 222 } 223 if (obj instanceof Object []) { 224 return nullSafeHashCode((Object []) obj); 225 } 226 if (obj instanceof boolean[]) { 227 return nullSafeHashCode((boolean[]) obj); 228 } 229 if (obj instanceof byte[]) { 230 return nullSafeHashCode((byte[]) obj); 231 } 232 if (obj instanceof char[]) { 233 return nullSafeHashCode((char[]) obj); 234 } 235 if (obj instanceof double[]) { 236 return nullSafeHashCode((double[]) obj); 237 } 238 if (obj instanceof float[]) { 239 return nullSafeHashCode((float[]) obj); 240 } 241 if (obj instanceof int[]) { 242 return nullSafeHashCode((int[]) obj); 243 } 244 if (obj instanceof long[]) { 245 return nullSafeHashCode((long[]) obj); 246 } 247 if (obj instanceof short[]) { 248 return nullSafeHashCode((short[]) obj); 249 } 250 return obj.hashCode(); 251 } 252 253 257 public static int nullSafeHashCode(Object [] array) { 258 if (array == null) { 259 return 0; 260 } 261 int hash = INITIAL_HASH; 262 int arraySize = array.length; 263 for (int i = 0; i < arraySize; i++) { 264 hash = MULTIPLIER * hash + nullSafeHashCode(array[i]); 265 } 266 return hash; 267 } 268 269 273 public static int nullSafeHashCode(boolean[] array) { 274 if (array == null) { 275 return 0; 276 } 277 int hash = INITIAL_HASH; 278 int arraySize = array.length; 279 for (int i = 0; i < arraySize; i++) { 280 hash = MULTIPLIER * hash + hashCode(array[i]); 281 } 282 return hash; 283 } 284 285 289 public static int nullSafeHashCode(byte[] array) { 290 if (array == null) { 291 return 0; 292 } 293 int hash = INITIAL_HASH; 294 int arraySize = array.length; 295 for (int i = 0; i < arraySize; i++) { 296 hash = MULTIPLIER * hash + array[i]; 297 } 298 return hash; 299 } 300 301 305 public static int nullSafeHashCode(char[] array) { 306 if (array == null) { 307 return 0; 308 } 309 int hash = INITIAL_HASH; 310 int arraySize = array.length; 311 for (int i = 0; i < arraySize; i++) { 312 hash = MULTIPLIER * hash + array[i]; 313 } 314 return hash; 315 } 316 317 321 public static int nullSafeHashCode(double[] array) { 322 if (array == null) { 323 return 0; 324 } 325 int hash = INITIAL_HASH; 326 int arraySize = array.length; 327 for (int i = 0; i < arraySize; i++) { 328 hash = MULTIPLIER * hash + hashCode(array[i]); 329 } 330 return hash; 331 } 332 333 337 public static int nullSafeHashCode(float[] array) { 338 if (array == null) { 339 return 0; 340 } 341 int hash = INITIAL_HASH; 342 int arraySize = array.length; 343 for (int i = 0; i < arraySize; i++) { 344 hash = MULTIPLIER * hash + hashCode(array[i]); 345 } 346 return hash; 347 } 348 349 353 public static int nullSafeHashCode(int[] array) { 354 if (array == null) { 355 return 0; 356 } 357 int hash = INITIAL_HASH; 358 int arraySize = array.length; 359 for (int i = 0; i < arraySize; i++) { 360 hash = MULTIPLIER * hash + array[i]; 361 } 362 return hash; 363 } 364 365 369 public static int nullSafeHashCode(long[] array) { 370 if (array == null) { 371 return 0; 372 } 373 int hash = INITIAL_HASH; 374 int arraySize = array.length; 375 for (int i = 0; i < arraySize; i++) { 376 hash = MULTIPLIER * hash + hashCode(array[i]); 377 } 378 return hash; 379 } 380 381 385 public static int nullSafeHashCode(short[] array) { 386 if (array == null) { 387 return 0; 388 } 389 int hash = INITIAL_HASH; 390 int arraySize = array.length; 391 for (int i = 0; i < arraySize; i++) { 392 hash = MULTIPLIER * hash + array[i]; 393 } 394 return hash; 395 } 396 397 401 public static int hashCode(boolean bool) { 402 return bool ? 1231 : 1237; 403 } 404 405 409 public static int hashCode(double dbl) { 410 long bits = Double.doubleToLongBits(dbl); 411 return hashCode(bits); 412 } 413 414 418 public static int hashCode(float flt) { 419 return Float.floatToIntBits(flt); 420 } 421 422 426 public static int hashCode(long lng) { 427 return (int) (lng ^ (lng >>> 32)); 428 } 429 430 431 435 441 public static String identityToString(Object obj) { 442 if (obj == null) { 443 return EMPTY_STRING; 444 } 445 return obj.getClass().getName() + "@" + getIdentityHexString(obj); 446 } 447 448 453 public static String getIdentityHexString(Object obj) { 454 return Integer.toHexString(System.identityHashCode(obj)); 455 } 456 457 466 public static String getDisplayString(Object obj) { 467 if (obj == null) { 468 return EMPTY_STRING; 469 } 470 return nullSafeToString(obj); 471 } 472 473 479 public static String nullSafeClassName(Object obj) { 480 return (obj != null ? obj.getClass().getName() : NULL_STRING); 481 } 482 483 490 public static String nullSafeToString(Object obj) { 491 if (obj == null) { 492 return NULL_STRING; 493 } 494 if (obj instanceof String ) { 495 return (String ) obj; 496 } 497 if (obj instanceof Object []) { 498 return nullSafeToString((Object []) obj); 499 } 500 if (obj instanceof boolean[]) { 501 return nullSafeToString((boolean[]) obj); 502 } 503 if (obj instanceof byte[]) { 504 return nullSafeToString((byte[]) obj); 505 } 506 if (obj instanceof char[]) { 507 return nullSafeToString((char[]) obj); 508 } 509 if (obj instanceof double[]) { 510 return nullSafeToString((double[]) obj); 511 } 512 if (obj instanceof float[]) { 513 return nullSafeToString((float[]) obj); 514 } 515 if (obj instanceof int[]) { 516 return nullSafeToString((int[]) obj); 517 } 518 if (obj instanceof long[]) { 519 return nullSafeToString((long[]) obj); 520 } 521 if (obj instanceof short[]) { 522 return nullSafeToString((short[]) obj); 523 } 524 return obj.toString(); 525 } 526 527 536 public static String nullSafeToString(Object [] array) { 537 if (array == null) { 538 return NULL_STRING; 539 } 540 int length = array.length; 541 if (length == 0) { 542 return EMPTY_ARRAY; 543 } 544 StringBuffer buffer = new StringBuffer (); 545 for (int i = 0; i < length; i++) { 546 if (i == 0) { 547 buffer.append(ARRAY_START); 548 } 549 else { 550 buffer.append(ARRAY_ELEMENT_SEPARATOR); 551 } 552 buffer.append(String.valueOf(array[i])); 553 } 554 buffer.append(ARRAY_END); 555 return buffer.toString(); 556 } 557 558 567 public static String nullSafeToString(boolean[] array) { 568 if (array == null) { 569 return NULL_STRING; 570 } 571 int length = array.length; 572 if (length == 0) { 573 return EMPTY_ARRAY; 574 } 575 StringBuffer buffer = new StringBuffer (); 576 for (int i = 0; i < length; i++) { 577 if (i == 0) { 578 buffer.append(ARRAY_START); 579 } 580 else { 581 buffer.append(ARRAY_ELEMENT_SEPARATOR); 582 } 583 584 buffer.append(array[i]); 585 } 586 buffer.append(ARRAY_END); 587 return buffer.toString(); 588 } 589 590 599 public static String nullSafeToString(byte[] array) { 600 if (array == null) { 601 return NULL_STRING; 602 } 603 int length = array.length; 604 if (length == 0) { 605 return EMPTY_ARRAY; 606 } 607 StringBuffer buffer = new StringBuffer (); 608 for (int i = 0; i < length; i++) { 609 if (i == 0) { 610 buffer.append(ARRAY_START); 611 } 612 else { 613 buffer.append(ARRAY_ELEMENT_SEPARATOR); 614 } 615 buffer.append(array[i]); 616 } 617 buffer.append(ARRAY_END); 618 return buffer.toString(); 619 } 620 621 630 public static String nullSafeToString(char[] array) { 631 if (array == null) { 632 return NULL_STRING; 633 } 634 int length = array.length; 635 if (length == 0) { 636 return EMPTY_ARRAY; 637 } 638 StringBuffer buffer = new StringBuffer (); 639 for (int i = 0; i < length; i++) { 640 if (i == 0) { 641 buffer.append(ARRAY_START); 642 } 643 else { 644 buffer.append(ARRAY_ELEMENT_SEPARATOR); 645 } 646 buffer.append("'").append(array[i]).append("'"); 647 } 648 buffer.append(ARRAY_END); 649 return buffer.toString(); 650 } 651 652 661 public static String nullSafeToString(double[] array) { 662 if (array == null) { 663 return NULL_STRING; 664 } 665 int length = array.length; 666 if (length == 0) { 667 return EMPTY_ARRAY; 668 } 669 StringBuffer buffer = new StringBuffer (); 670 for (int i = 0; i < length; i++) { 671 if (i == 0) { 672 buffer.append(ARRAY_START); 673 } 674 else { 675 buffer.append(ARRAY_ELEMENT_SEPARATOR); 676 } 677 678 buffer.append(array[i]); 679 } 680 buffer.append(ARRAY_END); 681 return buffer.toString(); 682 } 683 684 693 public static String nullSafeToString(float[] array) { 694 if (array == null) { 695 return NULL_STRING; 696 } 697 int length = array.length; 698 if (length == 0) { 699 return EMPTY_ARRAY; 700 } 701 StringBuffer buffer = new StringBuffer (); 702 for (int i = 0; i < length; i++) { 703 if (i == 0) { 704 buffer.append(ARRAY_START); 705 } 706 else { 707 buffer.append(ARRAY_ELEMENT_SEPARATOR); 708 } 709 710 buffer.append(array[i]); 711 } 712 buffer.append(ARRAY_END); 713 return buffer.toString(); 714 } 715 716 725 public static String nullSafeToString(int[] array) { 726 if (array == null) { 727 return NULL_STRING; 728 } 729 int length = array.length; 730 if (length == 0) { 731 return EMPTY_ARRAY; 732 } 733 StringBuffer buffer = new StringBuffer (); 734 for (int i = 0; i < length; i++) { 735 if (i == 0) { 736 buffer.append(ARRAY_START); 737 } 738 else { 739 buffer.append(ARRAY_ELEMENT_SEPARATOR); 740 } 741 buffer.append(array[i]); 742 } 743 buffer.append(ARRAY_END); 744 return buffer.toString(); 745 } 746 747 756 public static String nullSafeToString(long[] array) { 757 if (array == null) { 758 return NULL_STRING; 759 } 760 int length = array.length; 761 if (length == 0) { 762 return EMPTY_ARRAY; 763 } 764 StringBuffer buffer = new StringBuffer (); 765 for (int i = 0; i < length; i++) { 766 if (i == 0) { 767 buffer.append(ARRAY_START); 768 } 769 else { 770 buffer.append(ARRAY_ELEMENT_SEPARATOR); 771 } 772 buffer.append(array[i]); 773 } 774 buffer.append(ARRAY_END); 775 return buffer.toString(); 776 } 777 778 787 public static String nullSafeToString(short[] array) { 788 if (array == null) { 789 return NULL_STRING; 790 } 791 int length = array.length; 792 if (length == 0) { 793 return EMPTY_ARRAY; 794 } 795 StringBuffer buffer = new StringBuffer (); 796 for (int i = 0; i < length; i++) { 797 if (i == 0) { 798 buffer.append(ARRAY_START); 799 } 800 else { 801 buffer.append(ARRAY_ELEMENT_SEPARATOR); 802 } 803 buffer.append(array[i]); 804 } 805 buffer.append(ARRAY_END); 806 return buffer.toString(); 807 } 808 809 } 810 | Popular Tags |