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 @SuppressWarnings({ "rawtypes", "unchecked" })
36 public abstract class ObjectUtils
{
38 private static final int INITIAL_HASH
= 7;
39 private static final int MULTIPLIER
= 31;
41 private static final String EMPTY_STRING
= "";
42 private static final String NULL_STRING
= "null";
43 private static final String ARRAY_START
= "{";
44 private static final String ARRAY_END
= "}";
45 private static final String EMPTY_ARRAY
= ARRAY_START
+ ARRAY_END
;
46 private static final String ARRAY_ELEMENT_SEPARATOR
= ", ";
50 * Return whether the given throwable is a checked exception:
51 * that is, neither a RuntimeException nor an Error.
52 * @param ex the throwable to check
53 * @return whether the throwable is a checked exception
54 * @see java.lang.Exception
55 * @see java.lang.RuntimeException
56 * @see java.lang.Error
58 public static boolean isCheckedException(Throwable ex
) {
59 return !(ex
instanceof RuntimeException
|| ex
instanceof Error
);
63 * Check whether the given exception is compatible with the exceptions
64 * declared in a throws clause.
65 * @param ex the exception to checked
66 * @param declaredExceptions the exceptions declared in the throws clause
67 * @return whether the given exception is compatible
69 public static boolean isCompatibleWithThrowsClause(Throwable ex
, Class
[] declaredExceptions
) {
70 if (!isCheckedException(ex
)) {
73 if (declaredExceptions
!= null) {
74 for (int i
= 0; i
< declaredExceptions
.length
; i
++) {
75 if (declaredExceptions
[i
].isAssignableFrom(ex
.getClass())) {
84 * Return whether the given array is empty: that is, <code>null</code>
86 * @param array the array to check
87 * @return whether the given array is empty
89 public static boolean isEmpty(Object
[] array
) {
90 return (array
== null || array
.length
== 0);
94 * Check whether the given array contains the given element.
95 * @param array the array to check (may be <code>null</code>,
96 * in which case the return value will always be <code>false</code>)
97 * @param element the element to check for
98 * @return whether the element has been found in the given array
100 public static boolean containsElement(Object
[] array
, Object element
) {
104 for (int i
= 0; i
< array
.length
; i
++) {
105 if (nullSafeEquals(array
[i
], element
)) {
113 * Append the given Object to the given array, returning a new array
114 * consisting of the input array contents plus the given Object.
115 * @param array the array to append to (can be <code>null</code>)
116 * @param obj the Object to append
117 * @return the new array (of the same component type; never <code>null</code>)
119 public static Object
[] addObjectToArray(Object
[] array
, Object obj
) {
120 Class compType
= Object
.class;
122 compType
= array
.getClass().getComponentType();
124 else if (obj
!= null) {
125 compType
= obj
.getClass();
127 int newArrLength
= (array
!= null ? array
.length
+ 1 : 1);
128 Object
[] newArr
= (Object
[]) Array
.newInstance(compType
, newArrLength
);
130 System
.arraycopy(array
, 0, newArr
, 0, array
.length
);
132 newArr
[newArr
.length
- 1] = obj
;
137 * Convert the given array (which may be a primitive array) to an
138 * object array (if necessary of primitive wrapper objects).
139 * <p>A <code>null</code> source value will be converted to an
140 * empty Object array.
141 * @param source the (potentially primitive) array
142 * @return the corresponding object array (never <code>null</code>)
143 * @throws IllegalArgumentException if the parameter is not an array
145 public static Object
[] toObjectArray(Object source
) {
146 if (source
instanceof Object
[]) {
147 return (Object
[]) source
;
149 if (source
== null) {
150 return new Object
[0];
152 if (!source
.getClass().isArray()) {
153 throw new IllegalArgumentException("Source is not an array: " + source
);
155 int length
= Array
.getLength(source
);
157 return new Object
[0];
159 Class wrapperType
= Array
.get(source
, 0).getClass();
160 Object
[] newArray
= (Object
[]) Array
.newInstance(wrapperType
, length
);
161 for (int i
= 0; i
< length
; i
++) {
162 newArray
[i
] = Array
.get(source
, i
);
168 //---------------------------------------------------------------------
169 // Convenience methods for content-based equality/hash-code handling
170 //---------------------------------------------------------------------
173 * Determine if the given objects are equal, returning <code>true</code>
174 * if both are <code>null</code> or <code>false</code> if only one is
176 * <p>Compares arrays with <code>Arrays.equals</code>, performing an equality
177 * check based on the array elements rather than the array reference.
178 * @param o1 first Object to compare
179 * @param o2 second Object to compare
180 * @return whether the given objects are equal
181 * @see java.util.Arrays#equals
183 public static boolean nullSafeEquals(Object o1
, Object o2
) {
187 if (o1
== null || o2
== null) {
193 if (o1
.getClass().isArray() && o2
.getClass().isArray()) {
194 if (o1
instanceof Object
[] && o2
instanceof Object
[]) {
195 return Arrays
.equals((Object
[]) o1
, (Object
[]) o2
);
197 if (o1
instanceof boolean[] && o2
instanceof boolean[]) {
198 return Arrays
.equals((boolean[]) o1
, (boolean[]) o2
);
200 if (o1
instanceof byte[] && o2
instanceof byte[]) {
201 return Arrays
.equals((byte[]) o1
, (byte[]) o2
);
203 if (o1
instanceof char[] && o2
instanceof char[]) {
204 return Arrays
.equals((char[]) o1
, (char[]) o2
);
206 if (o1
instanceof double[] && o2
instanceof double[]) {
207 return Arrays
.equals((double[]) o1
, (double[]) o2
);
209 if (o1
instanceof float[] && o2
instanceof float[]) {
210 return Arrays
.equals((float[]) o1
, (float[]) o2
);
212 if (o1
instanceof int[] && o2
instanceof int[]) {
213 return Arrays
.equals((int[]) o1
, (int[]) o2
);
215 if (o1
instanceof long[] && o2
instanceof long[]) {
216 return Arrays
.equals((long[]) o1
, (long[]) o2
);
218 if (o1
instanceof short[] && o2
instanceof short[]) {
219 return Arrays
.equals((short[]) o1
, (short[]) o2
);
226 * Return as hash code for the given object; typically the value of
227 * <code>{@link Object#hashCode()}</code>. If the object is an array,
228 * this method will delegate to any of the <code>nullSafeHashCode</code>
229 * methods for arrays in this class. If the object is <code>null</code>,
230 * this method returns 0.
231 * @see #nullSafeHashCode(Object[])
232 * @see #nullSafeHashCode(boolean[])
233 * @see #nullSafeHashCode(byte[])
234 * @see #nullSafeHashCode(char[])
235 * @see #nullSafeHashCode(double[])
236 * @see #nullSafeHashCode(float[])
237 * @see #nullSafeHashCode(int[])
238 * @see #nullSafeHashCode(long[])
239 * @see #nullSafeHashCode(short[])
241 public static int nullSafeHashCode(Object obj
) {
245 if (obj
.getClass().isArray()) {
246 if (obj
instanceof Object
[]) {
247 return nullSafeHashCode((Object
[]) obj
);
249 if (obj
instanceof boolean[]) {
250 return nullSafeHashCode((boolean[]) obj
);
252 if (obj
instanceof byte[]) {
253 return nullSafeHashCode((byte[]) obj
);
255 if (obj
instanceof char[]) {
256 return nullSafeHashCode((char[]) obj
);
258 if (obj
instanceof double[]) {
259 return nullSafeHashCode((double[]) obj
);
261 if (obj
instanceof float[]) {
262 return nullSafeHashCode((float[]) obj
);
264 if (obj
instanceof int[]) {
265 return nullSafeHashCode((int[]) obj
);
267 if (obj
instanceof long[]) {
268 return nullSafeHashCode((long[]) obj
);
270 if (obj
instanceof short[]) {
271 return nullSafeHashCode((short[]) obj
);
274 return obj
.hashCode();
278 * Return a hash code based on the contents of the specified array.
279 * If <code>array</code> is <code>null</code>, this method returns 0.
281 public static int nullSafeHashCode(Object
[] array
) {
285 int hash
= INITIAL_HASH
;
286 int arraySize
= array
.length
;
287 for (int i
= 0; i
< arraySize
; i
++) {
288 hash
= MULTIPLIER
* hash
+ nullSafeHashCode(array
[i
]);
294 * Return a hash code based on the contents of the specified array.
295 * If <code>array</code> is <code>null</code>, this method returns 0.
297 public static int nullSafeHashCode(boolean[] array
) {
301 int hash
= INITIAL_HASH
;
302 int arraySize
= array
.length
;
303 for (int i
= 0; i
< arraySize
; i
++) {
304 hash
= MULTIPLIER
* hash
+ hashCode(array
[i
]);
310 * Return a hash code based on the contents of the specified array.
311 * If <code>array</code> is <code>null</code>, this method returns 0.
313 public static int nullSafeHashCode(byte[] array
) {
317 int hash
= INITIAL_HASH
;
318 int arraySize
= array
.length
;
319 for (int i
= 0; i
< arraySize
; i
++) {
320 hash
= MULTIPLIER
* hash
+ array
[i
];
326 * Return a hash code based on the contents of the specified array.
327 * If <code>array</code> is <code>null</code>, this method returns 0.
329 public static int nullSafeHashCode(char[] array
) {
333 int hash
= INITIAL_HASH
;
334 int arraySize
= array
.length
;
335 for (int i
= 0; i
< arraySize
; i
++) {
336 hash
= MULTIPLIER
* hash
+ array
[i
];
342 * Return a hash code based on the contents of the specified array.
343 * If <code>array</code> is <code>null</code>, this method returns 0.
345 public static int nullSafeHashCode(double[] array
) {
349 int hash
= INITIAL_HASH
;
350 int arraySize
= array
.length
;
351 for (int i
= 0; i
< arraySize
; i
++) {
352 hash
= MULTIPLIER
* hash
+ hashCode(array
[i
]);
358 * Return a hash code based on the contents of the specified array.
359 * If <code>array</code> is <code>null</code>, this method returns 0.
361 public static int nullSafeHashCode(float[] array
) {
365 int hash
= INITIAL_HASH
;
366 int arraySize
= array
.length
;
367 for (int i
= 0; i
< arraySize
; i
++) {
368 hash
= MULTIPLIER
* hash
+ hashCode(array
[i
]);
374 * Return a hash code based on the contents of the specified array.
375 * If <code>array</code> is <code>null</code>, this method returns 0.
377 public static int nullSafeHashCode(int[] array
) {
381 int hash
= INITIAL_HASH
;
382 int arraySize
= array
.length
;
383 for (int i
= 0; i
< arraySize
; i
++) {
384 hash
= MULTIPLIER
* hash
+ array
[i
];
390 * Return a hash code based on the contents of the specified array.
391 * If <code>array</code> is <code>null</code>, this method returns 0.
393 public static int nullSafeHashCode(long[] array
) {
397 int hash
= INITIAL_HASH
;
398 int arraySize
= array
.length
;
399 for (int i
= 0; i
< arraySize
; i
++) {
400 hash
= MULTIPLIER
* hash
+ hashCode(array
[i
]);
406 * Return a hash code based on the contents of the specified array.
407 * If <code>array</code> is <code>null</code>, this method returns 0.
409 public static int nullSafeHashCode(short[] array
) {
413 int hash
= INITIAL_HASH
;
414 int arraySize
= array
.length
;
415 for (int i
= 0; i
< arraySize
; i
++) {
416 hash
= MULTIPLIER
* hash
+ array
[i
];
422 * Return the same value as <code>{@link Boolean#hashCode()}</code>.
423 * @see Boolean#hashCode()
425 public static int hashCode(boolean bool
) {
426 return bool ?
1231 : 1237;
430 * Return the same value as <code>{@link Double#hashCode()}</code>.
431 * @see Double#hashCode()
433 public static int hashCode(double dbl
) {
434 long bits
= Double
.doubleToLongBits(dbl
);
435 return hashCode(bits
);
439 * Return the same value as <code>{@link Float#hashCode()}</code>.
440 * @see Float#hashCode()
442 public static int hashCode(float flt
) {
443 return Float
.floatToIntBits(flt
);
447 * Return the same value as <code>{@link Long#hashCode()}</code>.
448 * @see Long#hashCode()
450 public static int hashCode(long lng
) {
451 return (int) (lng ^
(lng
>>> 32));
455 //---------------------------------------------------------------------
456 // Convenience methods for toString output
457 //---------------------------------------------------------------------
460 * Return a String representation of an object's overall identity.
461 * @param obj the object (may be <code>null</code>)
462 * @return the object's identity as String representation,
463 * or an empty String if the object was <code>null</code>
465 public static String
identityToString(Object obj
) {
469 return obj
.getClass().getName() + "@" + getIdentityHexString(obj
);
473 * Return a hex String form of an object's identity hash code.
474 * @param obj the object
475 * @return the object's identity code in hex notation
477 public static String
getIdentityHexString(Object obj
) {
478 return Integer
.toHexString(System
.identityHashCode(obj
));
482 * Return a content-based String representation if <code>obj</code> is
483 * not <code>null</code>; otherwise returns an empty String.
484 * <p>Differs from {@link #nullSafeToString(Object)} in that it returns
485 * an empty String rather than "null" for a <code>null</code> value.
486 * @param obj the object to build a display String for
487 * @return a display String representation of <code>obj</code>
488 * @see #nullSafeToString(Object)
490 public static String
getDisplayString(Object obj
) {
494 return nullSafeToString(obj
);
498 * Determine the class name for the given object.
499 * <p>Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
500 * @param obj the object to introspect (may be <code>null</code>)
501 * @return the corresponding class name
503 public static String
nullSafeClassName(Object obj
) {
504 return (obj
!= null ? obj
.getClass().getName() : NULL_STRING
);
508 * Return a String representation of the specified Object.
509 * <p>Builds a String representation of the contents in case of an array.
510 * Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
511 * @param obj the object to build a String representation for
512 * @return a String representation of <code>obj</code>
514 public static String
nullSafeToString(Object obj
) {
518 if (obj
instanceof String
) {
521 if (obj
instanceof Object
[]) {
522 return nullSafeToString((Object
[]) obj
);
524 if (obj
instanceof boolean[]) {
525 return nullSafeToString((boolean[]) obj
);
527 if (obj
instanceof byte[]) {
528 return nullSafeToString((byte[]) obj
);
530 if (obj
instanceof char[]) {
531 return nullSafeToString((char[]) obj
);
533 if (obj
instanceof double[]) {
534 return nullSafeToString((double[]) obj
);
536 if (obj
instanceof float[]) {
537 return nullSafeToString((float[]) obj
);
539 if (obj
instanceof int[]) {
540 return nullSafeToString((int[]) obj
);
542 if (obj
instanceof long[]) {
543 return nullSafeToString((long[]) obj
);
545 if (obj
instanceof short[]) {
546 return nullSafeToString((short[]) obj
);
548 String str
= obj
.toString();
549 return (str
!= null ? str
: EMPTY_STRING
);
553 * Return a String representation of the contents of the specified array.
554 * <p>The String representation consists of a list of the array's elements,
555 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
556 * by the characters <code>", "</code> (a comma followed by a space). Returns
557 * <code>"null"</code> if <code>array</code> is <code>null</code>.
558 * @param array the array to build a String representation for
559 * @return a String representation of <code>array</code>
561 public static String
nullSafeToString(Object
[] array
) {
565 int length
= array
.length
;
569 StringBuffer buffer
= new StringBuffer();
570 for (int i
= 0; i
< length
; i
++) {
572 buffer
.append(ARRAY_START
);
575 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
577 buffer
.append(String
.valueOf(array
[i
]));
579 buffer
.append(ARRAY_END
);
580 return buffer
.toString();
584 * Return a String representation of the contents of the specified array.
585 * <p>The String representation consists of a list of the array's elements,
586 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
587 * by the characters <code>", "</code> (a comma followed by a space). Returns
588 * <code>"null"</code> if <code>array</code> is <code>null</code>.
589 * @param array the array to build a String representation for
590 * @return a String representation of <code>array</code>
592 public static String
nullSafeToString(boolean[] array
) {
596 int length
= array
.length
;
600 StringBuffer buffer
= new StringBuffer();
601 for (int i
= 0; i
< length
; i
++) {
603 buffer
.append(ARRAY_START
);
606 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
609 buffer
.append(array
[i
]);
611 buffer
.append(ARRAY_END
);
612 return buffer
.toString();
616 * Return a String representation of the contents of the specified array.
617 * <p>The String representation consists of a list of the array's elements,
618 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
619 * by the characters <code>", "</code> (a comma followed by a space). Returns
620 * <code>"null"</code> if <code>array</code> is <code>null</code>.
621 * @param array the array to build a String representation for
622 * @return a String representation of <code>array</code>
624 public static String
nullSafeToString(byte[] array
) {
628 int length
= array
.length
;
632 StringBuffer buffer
= new StringBuffer();
633 for (int i
= 0; i
< length
; i
++) {
635 buffer
.append(ARRAY_START
);
638 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
640 buffer
.append(array
[i
]);
642 buffer
.append(ARRAY_END
);
643 return buffer
.toString();
647 * Return a String representation of the contents of the specified array.
648 * <p>The String representation consists of a list of the array's elements,
649 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
650 * by the characters <code>", "</code> (a comma followed by a space). Returns
651 * <code>"null"</code> if <code>array</code> is <code>null</code>.
652 * @param array the array to build a String representation for
653 * @return a String representation of <code>array</code>
655 public static String
nullSafeToString(char[] array
) {
659 int length
= array
.length
;
663 StringBuffer buffer
= new StringBuffer();
664 for (int i
= 0; i
< length
; i
++) {
666 buffer
.append(ARRAY_START
);
669 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
671 buffer
.append("'").append(array
[i
]).append("'");
673 buffer
.append(ARRAY_END
);
674 return buffer
.toString();
678 * Return a String representation of the contents of the specified array.
679 * <p>The String representation consists of a list of the array's elements,
680 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
681 * by the characters <code>", "</code> (a comma followed by a space). Returns
682 * <code>"null"</code> if <code>array</code> is <code>null</code>.
683 * @param array the array to build a String representation for
684 * @return a String representation of <code>array</code>
686 public static String
nullSafeToString(double[] array
) {
690 int length
= array
.length
;
694 StringBuffer buffer
= new StringBuffer();
695 for (int i
= 0; i
< length
; i
++) {
697 buffer
.append(ARRAY_START
);
700 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
703 buffer
.append(array
[i
]);
705 buffer
.append(ARRAY_END
);
706 return buffer
.toString();
710 * Return a String representation of the contents of the specified array.
711 * <p>The String representation consists of a list of the array's elements,
712 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
713 * by the characters <code>", "</code> (a comma followed by a space). Returns
714 * <code>"null"</code> if <code>array</code> is <code>null</code>.
715 * @param array the array to build a String representation for
716 * @return a String representation of <code>array</code>
718 public static String
nullSafeToString(float[] array
) {
722 int length
= array
.length
;
726 StringBuffer buffer
= new StringBuffer();
727 for (int i
= 0; i
< length
; i
++) {
729 buffer
.append(ARRAY_START
);
732 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
735 buffer
.append(array
[i
]);
737 buffer
.append(ARRAY_END
);
738 return buffer
.toString();
742 * Return a String representation of the contents of the specified array.
743 * <p>The String representation consists of a list of the array's elements,
744 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
745 * by the characters <code>", "</code> (a comma followed by a space). Returns
746 * <code>"null"</code> if <code>array</code> is <code>null</code>.
747 * @param array the array to build a String representation for
748 * @return a String representation of <code>array</code>
750 public static String
nullSafeToString(int[] array
) {
754 int length
= array
.length
;
758 StringBuffer buffer
= new StringBuffer();
759 for (int i
= 0; i
< length
; i
++) {
761 buffer
.append(ARRAY_START
);
764 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
766 buffer
.append(array
[i
]);
768 buffer
.append(ARRAY_END
);
769 return buffer
.toString();
773 * Return a String representation of the contents of the specified array.
774 * <p>The String representation consists of a list of the array's elements,
775 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
776 * by the characters <code>", "</code> (a comma followed by a space). Returns
777 * <code>"null"</code> if <code>array</code> is <code>null</code>.
778 * @param array the array to build a String representation for
779 * @return a String representation of <code>array</code>
781 public static String
nullSafeToString(long[] array
) {
785 int length
= array
.length
;
789 StringBuffer buffer
= new StringBuffer();
790 for (int i
= 0; i
< length
; i
++) {
792 buffer
.append(ARRAY_START
);
795 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
797 buffer
.append(array
[i
]);
799 buffer
.append(ARRAY_END
);
800 return buffer
.toString();
804 * Return a String representation of the contents of the specified array.
805 * <p>The String representation consists of a list of the array's elements,
806 * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
807 * by the characters <code>", "</code> (a comma followed by a space). Returns
808 * <code>"null"</code> if <code>array</code> is <code>null</code>.
809 * @param array the array to build a String representation for
810 * @return a String representation of <code>array</code>
812 public static String
nullSafeToString(short[] array
) {
816 int length
= array
.length
;
820 StringBuffer buffer
= new StringBuffer();
821 for (int i
= 0; i
< length
; i
++) {
823 buffer
.append(ARRAY_START
);
826 buffer
.append(ARRAY_ELEMENT_SEPARATOR
);
828 buffer
.append(array
[i
]);
830 buffer
.append(ARRAY_END
);
831 return buffer
.toString();