2 * Copyright 2002-2007 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.osgi
.boot
.internal
.springutil
;
19 import java
.lang
.reflect
.Array
;
20 import java
.util
.Arrays
;
23 * Miscellaneous object utility methods. Mainly for internal use within the
24 * framework; consider Jakarta's Commons Lang for a more comprehensive suite
25 * of object utilities.
27 * @author Juergen Hoeller
28 * @author Keith Donald
33 * @see org.apache.commons.lang.ObjectUtils
35 public abstract class ObjectUtils
{
37 private static final int INITIAL_HASH
= 7;
38 private static final int MULTIPLIER
= 31;
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
= ", ";
49 * Return whether the given throwable is a checked exception:
50 * that is, neither a RuntimeException nor an Error.
51 * @param ex the throwable to check
52 * @return whether the throwable is a checked exception
53 * @see java.lang.Exception
54 * @see java.lang.RuntimeException
55 * @see java.lang.Error
57 public static boolean isCheckedException(Throwable ex
) {
58 return !(ex
instanceof RuntimeException
|| ex
instanceof Error
);
62 * Check whether the given exception is compatible with the exceptions
63 * declared in a throws clause.
64 * @param ex the exception to checked
65 * @param declaredExceptions the exceptions declared in the throws clause
66 * @return whether the given exception is compatible
68 public static boolean isCompatibleWithThrowsClause(Throwable ex
, Class
[] declaredExceptions
) {
69 if (!isCheckedException(ex
)) {
72 if (declaredExceptions
!= null) {
73 for (int i
= 0; i
< declaredExceptions
.length
; i
++) {
74 if (declaredExceptions
[i
].isAssignableFrom(ex
.getClass())) {
83 * Return whether the given array is empty: that is, <code>null</code>
85 * @param array the array to check
86 * @return whether the given array is empty
88 public static boolean isEmpty(Object
[] array
) {
89 return (array
== null || array
.length
== 0);
93 * Check whether the given array contains the given element.
94 * @param array the array to check (may be <code>null</code>,
95 * in which case the return value will always be <code>false</code>)
96 * @param element the element to check for
97 * @return whether the element has been found in the given array
99 public static boolean containsElement(Object
[] array
, Object element
) {
103 for (int i
= 0; i
< array
.length
; i
++) {
104 if (nullSafeEquals(array
[i
], element
)) {
112 * Append the given Object to the given array, returning a new array
113 * consisting of the input array contents plus the given Object.
114 * @param array the array to append to (can be <code>null</code>)
115 * @param obj the Object to append
116 * @return the new array (of the same component type; never <code>null</code>)
118 public static Object
[] addObjectToArray(Object
[] array
, Object obj
) {
119 Class compType
= Object
.class;
121 compType
= array
.getClass().getComponentType();
123 else if (obj
!= null) {
124 compType
= obj
.getClass();
126 int newArrLength
= (array
!= null ? array
.length
+ 1 : 1);
127 Object
[] newArr
= (Object
[]) Array
.newInstance(compType
, newArrLength
);
129 System
.arraycopy(array
, 0, newArr
, 0, array
.length
);
131 newArr
[newArr
.length
- 1] = obj
;
136 * Convert the given array (which may be a primitive array) to an
137 * object array (if necessary of primitive wrapper objects).
138 * <p>A <code>null</code> source value will be converted to an
139 * empty Object array.
140 * @param source the (potentially primitive) array
141 * @return the corresponding object array (never <code>null</code>)
142 * @throws IllegalArgumentException if the parameter is not an array
144 public static Object
[] toObjectArray(Object source
) {
145 if (source
instanceof Object
[]) {
146 return (Object
[]) source
;
148 if (source
== null) {
149 return new Object
[0];
151 if (!source
.getClass().isArray()) {
152 throw new IllegalArgumentException("Source is not an array: " + source
);
154 int length
= Array
.getLength(source
);
156 return new Object
[0];
158 Class wrapperType
= Array
.get(source
, 0).getClass();
159 Object
[] newArray
= (Object
[]) Array
.newInstance(wrapperType
, length
);
160 for (int i
= 0; i
< length
; i
++) {
161 newArray
[i
] = Array
.get(source
, i
);
167 //---------------------------------------------------------------------
168 // Convenience methods for content-based equality/hash-code handling
169 //---------------------------------------------------------------------
172 * Determine if the given objects are equal, returning <code>true</code>
173 * if both are <code>null</code> or <code>false</code> if only one is
175 * <p>Compares arrays with <code>Arrays.equals</code>, performing an equality
176 * check based on the array elements rather than the array reference.
177 * @param o1 first Object to compare
178 * @param o2 second Object to compare
179 * @return whether the given objects are equal
180 * @see java.util.Arrays#equals
182 public static boolean nullSafeEquals(Object o1
, Object o2
) {
186 if (o1
== null || o2
== null) {
192 if (o1
.getClass().isArray() && o2
.getClass().isArray()) {
193 if (o1
instanceof Object
[] && o2
instanceof Object
[]) {
194 return Arrays
.equals((Object
[]) o1
, (Object
[]) o2
);
196 if (o1
instanceof boolean[] && o2
instanceof boolean[]) {
197 return Arrays
.equals((boolean[]) o1
, (boolean[]) o2
);
199 if (o1
instanceof byte[] && o2
instanceof byte[]) {
200 return Arrays
.equals((byte[]) o1
, (byte[]) o2
);
202 if (o1
instanceof char[] && o2
instanceof char[]) {
203 return Arrays
.equals((char[]) o1
, (char[]) o2
);
205 if (o1
instanceof double[] && o2
instanceof double[]) {
206 return Arrays
.equals((double[]) o1
, (double[]) o2
);
208 if (o1
instanceof float[] && o2
instanceof float[]) {
209 return Arrays
.equals((float[]) o1
, (float[]) o2
);
211 if (o1
instanceof int[] && o2
instanceof int[]) {
212 return Arrays
.equals((int[]) o1
, (int[]) o2
);
214 if (o1
instanceof long[] && o2
instanceof long[]) {
215 return Arrays
.equals((long[]) o1
, (long[]) o2
);
217 if (o1
instanceof short[] && o2
instanceof short[]) {
218 return Arrays
.equals((short[]) o1
, (short[]) o2
);
225 * Return as hash code for the given object; typically the value of
226 * <code>{@link Object#hashCode()}</code>. If the object is an array,
227 * this method will delegate to any of the <code>nullSafeHashCode</code>
228 * methods for arrays in this class. If the object is <code>null</code>,
229 * this method returns 0.
230 * @see #nullSafeHashCode(Object[])
231 * @see #nullSafeHashCode(boolean[])
232 * @see #nullSafeHashCode(byte[])
233 * @see #nullSafeHashCode(char[])
234 * @see #nullSafeHashCode(double[])
235 * @see #nullSafeHashCode(float[])
236 * @see #nullSafeHashCode(int[])
237 * @see #nullSafeHashCode(long[])
238 * @see #nullSafeHashCode(short[])
240 public static int nullSafeHashCode(Object obj
) {
244 if (obj
.getClass().isArray()) {
245 if (obj
instanceof Object
[]) {
246 return nullSafeHashCode((Object
[]) obj
);
248 if (obj
instanceof boolean[]) {
249 return nullSafeHashCode((boolean[]) obj
);
251 if (obj
instanceof byte[]) {
252 return nullSafeHashCode((byte[]) obj
);
254 if (obj
instanceof char[]) {
255 return nullSafeHashCode((char[]) obj
);
257 if (obj
instanceof double[]) {
258 return nullSafeHashCode((double[]) obj
);
260 if (obj
instanceof float[]) {
261 return nullSafeHashCode((float[]) obj
);
263 if (obj
instanceof int[]) {
264 return nullSafeHashCode((int[]) obj
);
266 if (obj
instanceof long[]) {
267 return nullSafeHashCode((long[]) obj
);
269 if (obj
instanceof short[]) {
270 return nullSafeHashCode((short[]) obj
);
273 return obj
.hashCode();
277 * Return a hash code based on the contents of the specified array.
278 * If <code>array</code> is <code>null</code>, this method returns 0.
280 public static int nullSafeHashCode(Object
[] array
) {
284 int hash
= INITIAL_HASH
;
285 int arraySize
= array
.length
;
286 for (int i
= 0; i
< arraySize
; i
++) {
287 hash
= MULTIPLIER
* hash
+ nullSafeHashCode(array
[i
]);
293 * Return a hash code based on the contents of the specified array.
294 * If <code>array</code> is <code>null</code>, this method returns 0.
296 public static int nullSafeHashCode(boolean[] array
) {
300 int hash
= INITIAL_HASH
;
301 int arraySize
= array
.length
;
302 for (int i
= 0; i
< arraySize
; i
++) {
303 hash
= MULTIPLIER
* hash
+ hashCode(array
[i
]);
309 * Return a hash code based on the contents of the specified array.
310 * If <code>array</code> is <code>null</code>, this method returns 0.
312 public static int nullSafeHashCode(byte[] array
) {
316 int hash
= INITIAL_HASH
;
317 int arraySize
= array
.length
;
318 for (int i
= 0; i
< arraySize
; i
++) {
319 hash
= MULTIPLIER
* hash
+ array
[i
];
325 * Return a hash code based on the contents of the specified array.
326 * If <code>array</code> is <code>null</code>, this method returns 0.
328 public static int nullSafeHashCode(char[] array
) {
332 int hash
= INITIAL_HASH
;
333 int arraySize
= array
.length
;
334 for (int i
= 0; i
< arraySize
; i
++) {
335 hash
= MULTIPLIER
* hash
+ array
[i
];
341 * Return a hash code based on the contents of the specified array.
342 * If <code>array</code> is <code>null</code>, this method returns 0.
344 public static int nullSafeHashCode(double[] array
) {
348 int hash
= INITIAL_HASH
;
349 int arraySize
= array
.length
;
350 for (int i
= 0; i
< arraySize
; i
++) {
351 hash
= MULTIPLIER
* hash
+ hashCode(array
[i
]);
357 * Return a hash code based on the contents of the specified array.
358 * If <code>array</code> is <code>null</code>, this method returns 0.
360 public static int nullSafeHashCode(float[] array
) {
364 int hash
= INITIAL_HASH
;
365 int arraySize
= array
.length
;
366 for (int i
= 0; i
< arraySize
; i
++) {
367 hash
= MULTIPLIER
* hash
+ hashCode(array
[i
]);
373 * Return a hash code based on the contents of the specified array.
374 * If <code>array</code> is <code>null</code>, this method returns 0.
376 public static int nullSafeHashCode(int[] array
) {
380 int hash
= INITIAL_HASH
;
381 int arraySize
= array
.length
;
382 for (int i
= 0; i
< arraySize
; i
++) {
383 hash
= MULTIPLIER
* hash
+ array
[i
];
389 * Return a hash code based on the contents of the specified array.
390 * If <code>array</code> is <code>null</code>, this method returns 0.
392 public static int nullSafeHashCode(long[] array
) {
396 int hash
= INITIAL_HASH
;
397 int arraySize
= array
.length
;
398 for (int i
= 0; i
< arraySize
; i
++) {
399 hash
= MULTIPLIER
* hash
+ hashCode(array
[i
]);
405 * Return a hash code based on the contents of the specified array.
406 * If <code>array</code> is <code>null</code>, this method returns 0.
408 public static int nullSafeHashCode(short[] array
) {
412 int hash
= INITIAL_HASH
;
413 int arraySize
= array
.length
;
414 for (int i
= 0; i
< arraySize
; i
++) {
415 hash
= MULTIPLIER
* hash
+ array
[i
];
421 * Return the same value as <code>{@link Boolean#hashCode()}</code>.
422 * @see Boolean#hashCode()
424 public static int hashCode(boolean bool
) {
425 return bool ?
1231 : 1237;
429 * Return the same value as <code>{@link Double#hashCode()}</code>.
430 * @see Double#hashCode()
432 public static int hashCode(double dbl
) {
433 long bits
= Double
.doubleToLongBits(dbl
);
434 return hashCode(bits
);
438 * Return the same value as <code>{@link Float#hashCode()}</code>.
439 * @see Float#hashCode()
441 public static int hashCode(float flt
) {
442 return Float
.floatToIntBits(flt
);
446 * Return the same value as <code>{@link Long#hashCode()}</code>.
447 * @see Long#hashCode()
449 public static int hashCode(long lng
) {
450 return (int) (lng ^
(lng
>>> 32));
454 //---------------------------------------------------------------------
455 // Convenience methods for toString output
456 //---------------------------------------------------------------------
459 * Return a String representation of an object's overall identity.
460 * @param obj the object (may be <code>null</code>)
461 * @return the object's identity as String representation,
462 * or an empty String if the object was <code>null</code>
464 public static String
identityToString(Object obj
) {
468 return obj
.getClass().getName() + "@" + getIdentityHexString(obj
);
472 * Return a hex String form of an object's identity hash code.
473 * @param obj the object
474 * @return the object's identity code in hex notation
476 public static String
getIdentityHexString(Object obj
) {
477 return Integer
.toHexString(System
.identityHashCode(obj
));
481 * Return a content-based String representation if <code>obj</code> is
482 * not <code>null</code>; otherwise returns an empty String.
483 * <p>Differs from {@link #nullSafeToString(Object)} in that it returns
484 * an empty String rather than "null" for a <code>null</code> value.
485 * @param obj the object to build a display String for
486 * @return a display String representation of <code>obj</code>
487 * @see #nullSafeToString(Object)
489 public static String
getDisplayString(Object obj
) {
493 return nullSafeToString(obj
);
497 * Determine the class name for the given object.
498 * <p>Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
499 * @param obj the object to introspect (may be <code>null</code>)
500 * @return the corresponding class name
502 public static String
nullSafeClassName(Object obj
) {
503 return (obj
!= null ? obj
.getClass().getName() : NULL_STRING
);
507 * Return a String representation of the specified Object.
508 * <p>Builds a String representation of the contents in case of an array.
509 * Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
510 * @param obj the object to build a String representation for
511 * @return a String representation of <code>obj</code>
513 public static String
nullSafeToString(Object obj
) {
517 if (obj
instanceof String
) {
520 if (obj
instanceof Object
[]) {
521 return nullSafeToString((Object
[]) obj
);
523 if (obj
instanceof boolean[]) {
524 return nullSafeToString((boolean[]) obj
);
526 if (obj
instanceof byte[]) {
527 return nullSafeToString((byte[]) obj
);
529 if (obj
instanceof char[]) {
530 return nullSafeToString((char[]) obj
);
532 if (obj
instanceof double[]) {
533 return nullSafeToString((double[]) obj
);
535 if (obj
instanceof float[]) {
536 return nullSafeToString((float[]) obj
);
538 if (obj
instanceof int[]) {
539 return nullSafeToString((int[]) obj
);
541 if (obj
instanceof long[]) {
542 return nullSafeToString((long[]) obj
);
544 if (obj
instanceof short[]) {
545 return nullSafeToString((short[]) obj
);
547 String str
= obj
.toString();
548 return (str
!= null ? str
: EMPTY_STRING
);
552 * Return a String representation of the contents of the specified array.
553 * <p>The String representation consists of a list of the array's elements,
554 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
555 * by the characters <code>", "</code> (a comma followed by a space). Returns
556 * <code>"null"</code> if <code>array</code> is <code>null</code>.
557 * @param array the array to build a String representation for
558 * @return a String representation of <code>array</code>
560 public static String
nullSafeToString(Object
[] array
) {
564 int length
= array
.length
;
568 StringBuffer buffer
= new StringBuffer();
569 for (int i
= 0; i
< length
; i
++) {
571 buffer
.append(ARRAY_START
);
574 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
576 buffer
.append(String
.valueOf(array
[i
]));
578 buffer
.append(ARRAY_END
);
579 return buffer
.toString();
583 * Return a String representation of the contents of the specified array.
584 * <p>The String representation consists of a list of the array's elements,
585 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
586 * by the characters <code>", "</code> (a comma followed by a space). Returns
587 * <code>"null"</code> if <code>array</code> is <code>null</code>.
588 * @param array the array to build a String representation for
589 * @return a String representation of <code>array</code>
591 public static String
nullSafeToString(boolean[] array
) {
595 int length
= array
.length
;
599 StringBuffer buffer
= new StringBuffer();
600 for (int i
= 0; i
< length
; i
++) {
602 buffer
.append(ARRAY_START
);
605 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
608 buffer
.append(array
[i
]);
610 buffer
.append(ARRAY_END
);
611 return buffer
.toString();
615 * Return a String representation of the contents of the specified array.
616 * <p>The String representation consists of a list of the array's elements,
617 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
618 * by the characters <code>", "</code> (a comma followed by a space). Returns
619 * <code>"null"</code> if <code>array</code> is <code>null</code>.
620 * @param array the array to build a String representation for
621 * @return a String representation of <code>array</code>
623 public static String
nullSafeToString(byte[] array
) {
627 int length
= array
.length
;
631 StringBuffer buffer
= new StringBuffer();
632 for (int i
= 0; i
< length
; i
++) {
634 buffer
.append(ARRAY_START
);
637 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
639 buffer
.append(array
[i
]);
641 buffer
.append(ARRAY_END
);
642 return buffer
.toString();
646 * Return a String representation of the contents of the specified array.
647 * <p>The String representation consists of a list of the array's elements,
648 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
649 * by the characters <code>", "</code> (a comma followed by a space). Returns
650 * <code>"null"</code> if <code>array</code> is <code>null</code>.
651 * @param array the array to build a String representation for
652 * @return a String representation of <code>array</code>
654 public static String
nullSafeToString(char[] array
) {
658 int length
= array
.length
;
662 StringBuffer buffer
= new StringBuffer();
663 for (int i
= 0; i
< length
; i
++) {
665 buffer
.append(ARRAY_START
);
668 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
670 buffer
.append("'").append(array
[i
]).append("'");
672 buffer
.append(ARRAY_END
);
673 return buffer
.toString();
677 * Return a String representation of the contents of the specified array.
678 * <p>The String representation consists of a list of the array's elements,
679 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
680 * by the characters <code>", "</code> (a comma followed by a space). Returns
681 * <code>"null"</code> if <code>array</code> is <code>null</code>.
682 * @param array the array to build a String representation for
683 * @return a String representation of <code>array</code>
685 public static String
nullSafeToString(double[] array
) {
689 int length
= array
.length
;
693 StringBuffer buffer
= new StringBuffer();
694 for (int i
= 0; i
< length
; i
++) {
696 buffer
.append(ARRAY_START
);
699 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
702 buffer
.append(array
[i
]);
704 buffer
.append(ARRAY_END
);
705 return buffer
.toString();
709 * Return a String representation of the contents of the specified array.
710 * <p>The String representation consists of a list of the array's elements,
711 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
712 * by the characters <code>", "</code> (a comma followed by a space). Returns
713 * <code>"null"</code> if <code>array</code> is <code>null</code>.
714 * @param array the array to build a String representation for
715 * @return a String representation of <code>array</code>
717 public static String
nullSafeToString(float[] array
) {
721 int length
= array
.length
;
725 StringBuffer buffer
= new StringBuffer();
726 for (int i
= 0; i
< length
; i
++) {
728 buffer
.append(ARRAY_START
);
731 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
734 buffer
.append(array
[i
]);
736 buffer
.append(ARRAY_END
);
737 return buffer
.toString();
741 * Return a String representation of the contents of the specified array.
742 * <p>The String representation consists of a list of the array's elements,
743 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
744 * by the characters <code>", "</code> (a comma followed by a space). Returns
745 * <code>"null"</code> if <code>array</code> is <code>null</code>.
746 * @param array the array to build a String representation for
747 * @return a String representation of <code>array</code>
749 public static String
nullSafeToString(int[] array
) {
753 int length
= array
.length
;
757 StringBuffer buffer
= new StringBuffer();
758 for (int i
= 0; i
< length
; i
++) {
760 buffer
.append(ARRAY_START
);
763 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
765 buffer
.append(array
[i
]);
767 buffer
.append(ARRAY_END
);
768 return buffer
.toString();
772 * Return a String representation of the contents of the specified array.
773 * <p>The String representation consists of a list of the array's elements,
774 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
775 * by the characters <code>", "</code> (a comma followed by a space). Returns
776 * <code>"null"</code> if <code>array</code> is <code>null</code>.
777 * @param array the array to build a String representation for
778 * @return a String representation of <code>array</code>
780 public static String
nullSafeToString(long[] array
) {
784 int length
= array
.length
;
788 StringBuffer buffer
= new StringBuffer();
789 for (int i
= 0; i
< length
; i
++) {
791 buffer
.append(ARRAY_START
);
794 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
796 buffer
.append(array
[i
]);
798 buffer
.append(ARRAY_END
);
799 return buffer
.toString();
803 * Return a String representation of the contents of the specified array.
804 * <p>The String representation consists of a list of the array's elements,
805 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
806 * by the characters <code>", "</code> (a comma followed by a space). Returns
807 * <code>"null"</code> if <code>array</code> is <code>null</code>.
808 * @param array the array to build a String representation for
809 * @return a String representation of <code>array</code>
811 public static String
nullSafeToString(short[] array
) {
815 int length
= array
.length
;
819 StringBuffer buffer
= new StringBuffer();
820 for (int i
= 0; i
< length
; i
++) {
822 buffer
.append(ARRAY_START
);
825 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
827 buffer
.append(array
[i
]);
829 buffer
.append(ARRAY_END
);
830 return buffer
.toString();