KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > ObjectUtils


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */

16
17 package org.springframework.util;
18
19 import java.lang.reflect.Array JavaDoc;
20 import java.util.Arrays JavaDoc;
21
22 /**
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.
26  *
27  * @author Juergen Hoeller
28  * @author Keith Donald
29  * @author Rod Johnson
30  * @author Rob Harrop
31  * @author Alex Ruiz
32  * @since 19.03.2004
33  * @see org.apache.commons.lang.ObjectUtils
34  */

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 JavaDoc EMPTY_STRING = "";
41     private static final String JavaDoc NULL_STRING = "null";
42     private static final String JavaDoc ARRAY_START = "{";
43     private static final String JavaDoc ARRAY_END = "}";
44     private static final String JavaDoc EMPTY_ARRAY = ARRAY_START + ARRAY_END;
45     private static final String JavaDoc ARRAY_ELEMENT_SEPARATOR = ", ";
46
47
48     /**
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
56      */

57     public static boolean isCheckedException(Throwable JavaDoc ex) {
58         return !(ex instanceof RuntimeException JavaDoc || ex instanceof Error JavaDoc);
59     }
60
61     /**
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
67      */

68     public static boolean isCompatibleWithThrowsClause(Throwable JavaDoc ex, Class JavaDoc[] 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     /**
83      * Return whether the given array is empty: that is, <code>null</code>
84      * or of zero length.
85      * @param array the array to check
86      * @return whether the given array is empty
87      */

88     public static boolean isEmpty(Object JavaDoc[] array) {
89         return (array == null || array.length == 0);
90     }
91
92     /**
93      * Append the given Object to the given array, returning a new array
94      * consisting of the input array contents plus the given Object.
95      * @param array the array to append to (can be <code>null</code>)
96      * @param obj the Object to append
97      * @return the new array (of the same component type; never <code>null</code>)
98      */

99     public static Object JavaDoc[] addObjectToArray(Object JavaDoc[] array, Object JavaDoc obj) {
100         Class JavaDoc compType = Object JavaDoc.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 JavaDoc[] newArr = (Object JavaDoc[]) 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     /**
117      * Convert the given array (which may be a primitive array) to an
118      * object array (if necessary of primitive wrapper objects).
119      * <p>A <code>null</code> source value will be converted to an
120      * empty Object array.
121      * @param source the (potentially primitive) array
122      * @return the corresponding object array (never <code>null</code>)
123      * @throws IllegalArgumentException if the parameter is not an array
124      */

125     public static Object JavaDoc[] toObjectArray(Object JavaDoc source) {
126         if (source instanceof Object JavaDoc[]) {
127             return (Object JavaDoc[]) source;
128         }
129         if (source == null) {
130             return new Object JavaDoc[0];
131         }
132         if (!source.getClass().isArray()) {
133             throw new IllegalArgumentException JavaDoc("Source is not an array: " + source);
134         }
135         int length = Array.getLength(source);
136         if (length == 0) {
137             return new Object JavaDoc[0];
138         }
139         Class JavaDoc wrapperType = Array.get(source, 0).getClass();
140         Object JavaDoc[] newArray = (Object JavaDoc[]) 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     //---------------------------------------------------------------------
149
// Convenience methods for content-based equality/hash-code handling
150
//---------------------------------------------------------------------
151

152     /**
153      * Determine if the given objects are equal, returning <code>true</code>
154      * if both are <code>null</code> or <code>false</code> if only one is
155      * <code>null</code>.
156      * <p>Compares arrays with <code>Arrays.equals</code>, performing an equality
157      * check based on the array elements rather than the array reference.
158      * @param o1 first Object to compare
159      * @param o2 second Object to compare
160      * @return whether the given objects are equal
161      * @see java.util.Arrays#equals
162      */

163     public static boolean nullSafeEquals(Object JavaDoc o1, Object JavaDoc 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 JavaDoc[] && o2 instanceof Object JavaDoc[]) {
174             return Arrays.equals((Object JavaDoc[]) o1, (Object JavaDoc[]) 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     /**
204      * Return as hash code for the given object; typically the value of
205      * <code>{@link Object#hashCode()}</code>. If the object is an array,
206      * this method will delegate to any of the <code>nullSafeHashCode</code>
207      * methods for arrays in this class. If the object is <code>null</code>,
208      * this method returns 0.
209      * @see #nullSafeHashCode(Object[])
210      * @see #nullSafeHashCode(boolean[])
211      * @see #nullSafeHashCode(byte[])
212      * @see #nullSafeHashCode(char[])
213      * @see #nullSafeHashCode(double[])
214      * @see #nullSafeHashCode(float[])
215      * @see #nullSafeHashCode(int[])
216      * @see #nullSafeHashCode(long[])
217      * @see #nullSafeHashCode(short[])
218      */

219     public static int nullSafeHashCode(Object JavaDoc obj) {
220         if (obj == null) {
221             return 0;
222         }
223         if (obj instanceof Object JavaDoc[]) {
224             return nullSafeHashCode((Object JavaDoc[]) 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     /**
254      * Return a hash code based on the contents of the specified array.
255      * If <code>array</code> is <code>null</code>, this method returns 0.
256      */

257     public static int nullSafeHashCode(Object JavaDoc[] 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     /**
270      * Return a hash code based on the contents of the specified array.
271      * If <code>array</code> is <code>null</code>, this method returns 0.
272      */

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     /**
286      * Return a hash code based on the contents of the specified array.
287      * If <code>array</code> is <code>null</code>, this method returns 0.
288      */

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     /**
302      * Return a hash code based on the contents of the specified array.
303      * If <code>array</code> is <code>null</code>, this method returns 0.
304      */

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     /**
318      * Return a hash code based on the contents of the specified array.
319      * If <code>array</code> is <code>null</code>, this method returns 0.
320      */

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     /**
334      * Return a hash code based on the contents of the specified array.
335      * If <code>array</code> is <code>null</code>, this method returns 0.
336      */

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     /**
350      * Return a hash code based on the contents of the specified array.
351      * If <code>array</code> is <code>null</code>, this method returns 0.
352      */

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     /**
366      * Return a hash code based on the contents of the specified array.
367      * If <code>array</code> is <code>null</code>, this method returns 0.
368      */

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     /**
382      * Return a hash code based on the contents of the specified array.
383      * If <code>array</code> is <code>null</code>, this method returns 0.
384      */

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     /**
398      * Return the same value as <code>{@link Boolean#hashCode()}</code>.
399      * @see Boolean#hashCode()
400      */

401     public static int hashCode(boolean bool) {
402         return bool ? 1231 : 1237;
403     }
404
405     /**
406      * Return the same value as <code>{@link Double#hashCode()}</code>.
407      * @see Double#hashCode()
408      */

409     public static int hashCode(double dbl) {
410         long bits = Double.doubleToLongBits(dbl);
411         return hashCode(bits);
412     }
413
414     /**
415      * Return the same value as <code>{@link Float#hashCode()}</code>.
416      * @see Float#hashCode()
417      */

418     public static int hashCode(float flt) {
419         return Float.floatToIntBits(flt);
420     }
421
422     /**
423      * Return the same value as <code>{@link Long#hashCode()}</code>.
424      * @see Long#hashCode()
425      */

426     public static int hashCode(long lng) {
427         return (int) (lng ^ (lng >>> 32));
428     }
429
430
431     //---------------------------------------------------------------------
432
// Convenience methods for toString output
433
//---------------------------------------------------------------------
434

435     /**
436      * Return a String representation of an object's overall identity.
437      * @param obj the object (may be <code>null</code>)
438      * @return the object's identity as String representation,
439      * or <code>null</code> if the object was <code>null</code>
440      */

441     public static String JavaDoc identityToString(Object JavaDoc obj) {
442         if (obj == null) {
443             return EMPTY_STRING;
444         }
445         return obj.getClass().getName() + "@" + getIdentityHexString(obj);
446     }
447
448     /**
449      * Return a hex String form of an object's identity hash code.
450      * @param obj the object
451      * @return the object's identity code in hex notation
452      */

453     public static String JavaDoc getIdentityHexString(Object JavaDoc obj) {
454         return Integer.toHexString(System.identityHashCode(obj));
455     }
456
457     /**
458      * Return a content-based String representation if <code>obj</code> is
459      * not <code>null</code>; otherwise returns an empty String.
460      * <p>Differs from {@link #nullSafeToString(Object)} in that it returns
461      * an empty String rather than "null" for a <code>null</code> value.
462      * @param obj the object to build a display String for
463      * @return a display String representation of <code>obj</code>
464      * @see #nullSafeToString(Object)
465      */

466     public static String JavaDoc getDisplayString(Object JavaDoc obj) {
467         if (obj == null) {
468             return EMPTY_STRING;
469         }
470         return nullSafeToString(obj);
471     }
472
473     /**
474      * Determine the class name for the given object.
475      * <p>Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
476      * @param obj the object to introspect (may be <code>null</code>)
477      * @return the corresponding class name
478      */

479     public static String JavaDoc nullSafeClassName(Object JavaDoc obj) {
480         return (obj != null ? obj.getClass().getName() : NULL_STRING);
481     }
482
483     /**
484      * Return a String representation of the specified Object.
485      * <p>Builds a String representation of the contents in case of an array.
486      * Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
487      * @param obj the object to build a String representation for
488      * @return a String representation of <code>obj</code>
489      */

490     public static String JavaDoc nullSafeToString(Object JavaDoc obj) {
491         if (obj == null) {
492             return NULL_STRING;
493         }
494         if (obj instanceof String JavaDoc) {
495             return (String JavaDoc) obj;
496         }
497         if (obj instanceof Object JavaDoc[]) {
498             return nullSafeToString((Object JavaDoc[]) 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     /**
528      * Return a String representation of the contents of the specified array.
529      * <p>The String representation consists of a list of the array's elements,
530      * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
531      * by the characters <code>", "</code> (a comma followed by a space). Returns
532      * <code>"null"</code> if <code>array</code> is <code>null</code>.
533      * @param array the array to build a String representation for
534      * @return a String representation of <code>array</code>
535      */

536     public static String JavaDoc nullSafeToString(Object JavaDoc[] 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 JavaDoc buffer = new StringBuffer JavaDoc();
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     /**
559      * Return a String representation of the contents of the specified array.
560      * <p>The String representation consists of a list of the array's elements,
561      * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
562      * by the characters <code>", "</code> (a comma followed by a space). Returns
563      * <code>"null"</code> if <code>array</code> is <code>null</code>.
564      * @param array the array to build a String representation for
565      * @return a String representation of <code>array</code>
566      */

567     public static String JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc();
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     /**
591      * Return a String representation of the contents of the specified array.
592      * <p>The String representation consists of a list of the array's elements,
593      * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
594      * by the characters <code>", "</code> (a comma followed by a space). Returns
595      * <code>"null"</code> if <code>array</code> is <code>null</code>.
596      * @param array the array to build a String representation for
597      * @return a String representation of <code>array</code>
598      */

599     public static String JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc();
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     /**
622      * Return a String representation of the contents of the specified array.
623      * <p>The String representation consists of a list of the array's elements,
624      * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
625      * by the characters <code>", "</code> (a comma followed by a space). Returns
626      * <code>"null"</code> if <code>array</code> is <code>null</code>.
627      * @param array the array to build a String representation for
628      * @return a String representation of <code>array</code>
629      */

630     public static String JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc();
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     /**
653      * Return a String representation of the contents of the specified array.
654      * <p>The String representation consists of a list of the array's elements,
655      * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
656      * by the characters <code>", "</code> (a comma followed by a space). Returns
657      * <code>"null"</code> if <code>array</code> is <code>null</code>.
658      * @param array the array to build a String representation for
659      * @return a String representation of <code>array</code>
660      */

661     public static String JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc();
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     /**
685      * Return a String representation of the contents of the specified array.
686      * <p>The String representation consists of a list of the array's elements,
687      * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
688      * by the characters <code>", "</code> (a comma followed by a space). Returns
689      * <code>"null"</code> if <code>array</code> is <code>null</code>.
690      * @param array the array to build a String representation for
691      * @return a String representation of <code>array</code>
692      */

693     public static String JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc();
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     /**
717      * Return a String representation of the contents of the specified array.
718      * <p>The String representation consists of a list of the array's elements,
719      * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
720      * by the characters <code>", "</code> (a comma followed by a space). Returns
721      * <code>"null"</code> if <code>array</code> is <code>null</code>.
722      * @param array the array to build a String representation for
723      * @return a String representation of <code>array</code>
724      */

725     public static String JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc();
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     /**
748      * Return a String representation of the contents of the specified array.
749      * <p>The String representation consists of a list of the array's elements,
750      * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
751      * by the characters <code>", "</code> (a comma followed by a space). Returns
752      * <code>"null"</code> if <code>array</code> is <code>null</code>.
753      * @param array the array to build a String representation for
754      * @return a String representation of <code>array</code>
755      */

756     public static String JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc();
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     /**
779      * Return a String representation of the contents of the specified array.
780      * <p>The String representation consists of a list of the array's elements,
781      * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
782      * by the characters <code>", "</code> (a comma followed by a space). Returns
783      * <code>"null"</code> if <code>array</code> is <code>null</code>.
784      * @param array the array to build a String representation for
785      * @return a String representation of <code>array</code>
786      */

787     public static String JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc();
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