]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.osgi.boot/src/org/argeo/osgi/boot/internal/springutil/ObjectUtils.java
Merge branch 'master' of https://mbaudier@code.argeo.org/git/apache2/argeo-commons.git
[lgpl/argeo-commons.git] / org.argeo.osgi.boot / src / org / argeo / osgi / boot / internal / springutil / ObjectUtils.java
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.argeo.osgi.boot.internal.springutil;
18
19 import java.lang.reflect.Array;
20 import java.util.Arrays;
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 @SuppressWarnings({ "rawtypes", "unchecked" })
36 public abstract class ObjectUtils {
37
38 private static final int INITIAL_HASH = 7;
39 private static final int MULTIPLIER = 31;
40
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 = ", ";
47
48
49 /**
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
57 */
58 public static boolean isCheckedException(Throwable ex) {
59 return !(ex instanceof RuntimeException || ex instanceof Error);
60 }
61
62 /**
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
68 */
69 public static boolean isCompatibleWithThrowsClause(Throwable ex, Class[] declaredExceptions) {
70 if (!isCheckedException(ex)) {
71 return true;
72 }
73 if (declaredExceptions != null) {
74 for (int i = 0; i < declaredExceptions.length; i++) {
75 if (declaredExceptions[i].isAssignableFrom(ex.getClass())) {
76 return true;
77 }
78 }
79 }
80 return false;
81 }
82
83 /**
84 * Return whether the given array is empty: that is, <code>null</code>
85 * or of zero length.
86 * @param array the array to check
87 * @return whether the given array is empty
88 */
89 public static boolean isEmpty(Object[] array) {
90 return (array == null || array.length == 0);
91 }
92
93 /**
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
99 */
100 public static boolean containsElement(Object[] array, Object element) {
101 if (array == null) {
102 return false;
103 }
104 for (int i = 0; i < array.length; i++) {
105 if (nullSafeEquals(array[i], element)) {
106 return true;
107 }
108 }
109 return false;
110 }
111
112 /**
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>)
118 */
119 public static Object[] addObjectToArray(Object[] array, Object obj) {
120 Class compType = Object.class;
121 if (array != null) {
122 compType = array.getClass().getComponentType();
123 }
124 else if (obj != null) {
125 compType = obj.getClass();
126 }
127 int newArrLength = (array != null ? array.length + 1 : 1);
128 Object[] newArr = (Object[]) Array.newInstance(compType, newArrLength);
129 if (array != null) {
130 System.arraycopy(array, 0, newArr, 0, array.length);
131 }
132 newArr[newArr.length - 1] = obj;
133 return newArr;
134 }
135
136 /**
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
144 */
145 public static Object[] toObjectArray(Object source) {
146 if (source instanceof Object[]) {
147 return (Object[]) source;
148 }
149 if (source == null) {
150 return new Object[0];
151 }
152 if (!source.getClass().isArray()) {
153 throw new IllegalArgumentException("Source is not an array: " + source);
154 }
155 int length = Array.getLength(source);
156 if (length == 0) {
157 return new Object[0];
158 }
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);
163 }
164 return newArray;
165 }
166
167
168 //---------------------------------------------------------------------
169 // Convenience methods for content-based equality/hash-code handling
170 //---------------------------------------------------------------------
171
172 /**
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
175 * <code>null</code>.
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
182 */
183 public static boolean nullSafeEquals(Object o1, Object o2) {
184 if (o1 == o2) {
185 return true;
186 }
187 if (o1 == null || o2 == null) {
188 return false;
189 }
190 if (o1.equals(o2)) {
191 return true;
192 }
193 if (o1.getClass().isArray() && o2.getClass().isArray()) {
194 if (o1 instanceof Object[] && o2 instanceof Object[]) {
195 return Arrays.equals((Object[]) o1, (Object[]) o2);
196 }
197 if (o1 instanceof boolean[] && o2 instanceof boolean[]) {
198 return Arrays.equals((boolean[]) o1, (boolean[]) o2);
199 }
200 if (o1 instanceof byte[] && o2 instanceof byte[]) {
201 return Arrays.equals((byte[]) o1, (byte[]) o2);
202 }
203 if (o1 instanceof char[] && o2 instanceof char[]) {
204 return Arrays.equals((char[]) o1, (char[]) o2);
205 }
206 if (o1 instanceof double[] && o2 instanceof double[]) {
207 return Arrays.equals((double[]) o1, (double[]) o2);
208 }
209 if (o1 instanceof float[] && o2 instanceof float[]) {
210 return Arrays.equals((float[]) o1, (float[]) o2);
211 }
212 if (o1 instanceof int[] && o2 instanceof int[]) {
213 return Arrays.equals((int[]) o1, (int[]) o2);
214 }
215 if (o1 instanceof long[] && o2 instanceof long[]) {
216 return Arrays.equals((long[]) o1, (long[]) o2);
217 }
218 if (o1 instanceof short[] && o2 instanceof short[]) {
219 return Arrays.equals((short[]) o1, (short[]) o2);
220 }
221 }
222 return false;
223 }
224
225 /**
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[])
240 */
241 public static int nullSafeHashCode(Object obj) {
242 if (obj == null) {
243 return 0;
244 }
245 if (obj.getClass().isArray()) {
246 if (obj instanceof Object[]) {
247 return nullSafeHashCode((Object[]) obj);
248 }
249 if (obj instanceof boolean[]) {
250 return nullSafeHashCode((boolean[]) obj);
251 }
252 if (obj instanceof byte[]) {
253 return nullSafeHashCode((byte[]) obj);
254 }
255 if (obj instanceof char[]) {
256 return nullSafeHashCode((char[]) obj);
257 }
258 if (obj instanceof double[]) {
259 return nullSafeHashCode((double[]) obj);
260 }
261 if (obj instanceof float[]) {
262 return nullSafeHashCode((float[]) obj);
263 }
264 if (obj instanceof int[]) {
265 return nullSafeHashCode((int[]) obj);
266 }
267 if (obj instanceof long[]) {
268 return nullSafeHashCode((long[]) obj);
269 }
270 if (obj instanceof short[]) {
271 return nullSafeHashCode((short[]) obj);
272 }
273 }
274 return obj.hashCode();
275 }
276
277 /**
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.
280 */
281 public static int nullSafeHashCode(Object[] array) {
282 if (array == null) {
283 return 0;
284 }
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]);
289 }
290 return hash;
291 }
292
293 /**
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.
296 */
297 public static int nullSafeHashCode(boolean[] array) {
298 if (array == null) {
299 return 0;
300 }
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]);
305 }
306 return hash;
307 }
308
309 /**
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.
312 */
313 public static int nullSafeHashCode(byte[] array) {
314 if (array == null) {
315 return 0;
316 }
317 int hash = INITIAL_HASH;
318 int arraySize = array.length;
319 for (int i = 0; i < arraySize; i++) {
320 hash = MULTIPLIER * hash + array[i];
321 }
322 return hash;
323 }
324
325 /**
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.
328 */
329 public static int nullSafeHashCode(char[] array) {
330 if (array == null) {
331 return 0;
332 }
333 int hash = INITIAL_HASH;
334 int arraySize = array.length;
335 for (int i = 0; i < arraySize; i++) {
336 hash = MULTIPLIER * hash + array[i];
337 }
338 return hash;
339 }
340
341 /**
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.
344 */
345 public static int nullSafeHashCode(double[] array) {
346 if (array == null) {
347 return 0;
348 }
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]);
353 }
354 return hash;
355 }
356
357 /**
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.
360 */
361 public static int nullSafeHashCode(float[] array) {
362 if (array == null) {
363 return 0;
364 }
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]);
369 }
370 return hash;
371 }
372
373 /**
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.
376 */
377 public static int nullSafeHashCode(int[] array) {
378 if (array == null) {
379 return 0;
380 }
381 int hash = INITIAL_HASH;
382 int arraySize = array.length;
383 for (int i = 0; i < arraySize; i++) {
384 hash = MULTIPLIER * hash + array[i];
385 }
386 return hash;
387 }
388
389 /**
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.
392 */
393 public static int nullSafeHashCode(long[] array) {
394 if (array == null) {
395 return 0;
396 }
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]);
401 }
402 return hash;
403 }
404
405 /**
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.
408 */
409 public static int nullSafeHashCode(short[] array) {
410 if (array == null) {
411 return 0;
412 }
413 int hash = INITIAL_HASH;
414 int arraySize = array.length;
415 for (int i = 0; i < arraySize; i++) {
416 hash = MULTIPLIER * hash + array[i];
417 }
418 return hash;
419 }
420
421 /**
422 * Return the same value as <code>{@link Boolean#hashCode()}</code>.
423 * @see Boolean#hashCode()
424 */
425 public static int hashCode(boolean bool) {
426 return bool ? 1231 : 1237;
427 }
428
429 /**
430 * Return the same value as <code>{@link Double#hashCode()}</code>.
431 * @see Double#hashCode()
432 */
433 public static int hashCode(double dbl) {
434 long bits = Double.doubleToLongBits(dbl);
435 return hashCode(bits);
436 }
437
438 /**
439 * Return the same value as <code>{@link Float#hashCode()}</code>.
440 * @see Float#hashCode()
441 */
442 public static int hashCode(float flt) {
443 return Float.floatToIntBits(flt);
444 }
445
446 /**
447 * Return the same value as <code>{@link Long#hashCode()}</code>.
448 * @see Long#hashCode()
449 */
450 public static int hashCode(long lng) {
451 return (int) (lng ^ (lng >>> 32));
452 }
453
454
455 //---------------------------------------------------------------------
456 // Convenience methods for toString output
457 //---------------------------------------------------------------------
458
459 /**
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>
464 */
465 public static String identityToString(Object obj) {
466 if (obj == null) {
467 return EMPTY_STRING;
468 }
469 return obj.getClass().getName() + "@" + getIdentityHexString(obj);
470 }
471
472 /**
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
476 */
477 public static String getIdentityHexString(Object obj) {
478 return Integer.toHexString(System.identityHashCode(obj));
479 }
480
481 /**
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)
489 */
490 public static String getDisplayString(Object obj) {
491 if (obj == null) {
492 return EMPTY_STRING;
493 }
494 return nullSafeToString(obj);
495 }
496
497 /**
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
502 */
503 public static String nullSafeClassName(Object obj) {
504 return (obj != null ? obj.getClass().getName() : NULL_STRING);
505 }
506
507 /**
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>
513 */
514 public static String nullSafeToString(Object obj) {
515 if (obj == null) {
516 return NULL_STRING;
517 }
518 if (obj instanceof String) {
519 return (String) obj;
520 }
521 if (obj instanceof Object[]) {
522 return nullSafeToString((Object[]) obj);
523 }
524 if (obj instanceof boolean[]) {
525 return nullSafeToString((boolean[]) obj);
526 }
527 if (obj instanceof byte[]) {
528 return nullSafeToString((byte[]) obj);
529 }
530 if (obj instanceof char[]) {
531 return nullSafeToString((char[]) obj);
532 }
533 if (obj instanceof double[]) {
534 return nullSafeToString((double[]) obj);
535 }
536 if (obj instanceof float[]) {
537 return nullSafeToString((float[]) obj);
538 }
539 if (obj instanceof int[]) {
540 return nullSafeToString((int[]) obj);
541 }
542 if (obj instanceof long[]) {
543 return nullSafeToString((long[]) obj);
544 }
545 if (obj instanceof short[]) {
546 return nullSafeToString((short[]) obj);
547 }
548 String str = obj.toString();
549 return (str != null ? str : EMPTY_STRING);
550 }
551
552 /**
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>
560 */
561 public static String nullSafeToString(Object[] array) {
562 if (array == null) {
563 return NULL_STRING;
564 }
565 int length = array.length;
566 if (length == 0) {
567 return EMPTY_ARRAY;
568 }
569 StringBuffer buffer = new StringBuffer();
570 for (int i = 0; i < length; i++) {
571 if (i == 0) {
572 buffer.append(ARRAY_START);
573 }
574 else {
575 buffer.append(ARRAY_ELEMENT_SEPARATOR);
576 }
577 buffer.append(String.valueOf(array[i]));
578 }
579 buffer.append(ARRAY_END);
580 return buffer.toString();
581 }
582
583 /**
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>
591 */
592 public static String nullSafeToString(boolean[] array) {
593 if (array == null) {
594 return NULL_STRING;
595 }
596 int length = array.length;
597 if (length == 0) {
598 return EMPTY_ARRAY;
599 }
600 StringBuffer buffer = new StringBuffer();
601 for (int i = 0; i < length; i++) {
602 if (i == 0) {
603 buffer.append(ARRAY_START);
604 }
605 else {
606 buffer.append(ARRAY_ELEMENT_SEPARATOR);
607 }
608
609 buffer.append(array[i]);
610 }
611 buffer.append(ARRAY_END);
612 return buffer.toString();
613 }
614
615 /**
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>
623 */
624 public static String nullSafeToString(byte[] array) {
625 if (array == null) {
626 return NULL_STRING;
627 }
628 int length = array.length;
629 if (length == 0) {
630 return EMPTY_ARRAY;
631 }
632 StringBuffer buffer = new StringBuffer();
633 for (int i = 0; i < length; i++) {
634 if (i == 0) {
635 buffer.append(ARRAY_START);
636 }
637 else {
638 buffer.append(ARRAY_ELEMENT_SEPARATOR);
639 }
640 buffer.append(array[i]);
641 }
642 buffer.append(ARRAY_END);
643 return buffer.toString();
644 }
645
646 /**
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>
654 */
655 public static String nullSafeToString(char[] array) {
656 if (array == null) {
657 return NULL_STRING;
658 }
659 int length = array.length;
660 if (length == 0) {
661 return EMPTY_ARRAY;
662 }
663 StringBuffer buffer = new StringBuffer();
664 for (int i = 0; i < length; i++) {
665 if (i == 0) {
666 buffer.append(ARRAY_START);
667 }
668 else {
669 buffer.append(ARRAY_ELEMENT_SEPARATOR);
670 }
671 buffer.append("'").append(array[i]).append("'");
672 }
673 buffer.append(ARRAY_END);
674 return buffer.toString();
675 }
676
677 /**
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>
685 */
686 public static String nullSafeToString(double[] array) {
687 if (array == null) {
688 return NULL_STRING;
689 }
690 int length = array.length;
691 if (length == 0) {
692 return EMPTY_ARRAY;
693 }
694 StringBuffer buffer = new StringBuffer();
695 for (int i = 0; i < length; i++) {
696 if (i == 0) {
697 buffer.append(ARRAY_START);
698 }
699 else {
700 buffer.append(ARRAY_ELEMENT_SEPARATOR);
701 }
702
703 buffer.append(array[i]);
704 }
705 buffer.append(ARRAY_END);
706 return buffer.toString();
707 }
708
709 /**
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>
717 */
718 public static String nullSafeToString(float[] array) {
719 if (array == null) {
720 return NULL_STRING;
721 }
722 int length = array.length;
723 if (length == 0) {
724 return EMPTY_ARRAY;
725 }
726 StringBuffer buffer = new StringBuffer();
727 for (int i = 0; i < length; i++) {
728 if (i == 0) {
729 buffer.append(ARRAY_START);
730 }
731 else {
732 buffer.append(ARRAY_ELEMENT_SEPARATOR);
733 }
734
735 buffer.append(array[i]);
736 }
737 buffer.append(ARRAY_END);
738 return buffer.toString();
739 }
740
741 /**
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>
749 */
750 public static String nullSafeToString(int[] array) {
751 if (array == null) {
752 return NULL_STRING;
753 }
754 int length = array.length;
755 if (length == 0) {
756 return EMPTY_ARRAY;
757 }
758 StringBuffer buffer = new StringBuffer();
759 for (int i = 0; i < length; i++) {
760 if (i == 0) {
761 buffer.append(ARRAY_START);
762 }
763 else {
764 buffer.append(ARRAY_ELEMENT_SEPARATOR);
765 }
766 buffer.append(array[i]);
767 }
768 buffer.append(ARRAY_END);
769 return buffer.toString();
770 }
771
772 /**
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>
780 */
781 public static String nullSafeToString(long[] array) {
782 if (array == null) {
783 return NULL_STRING;
784 }
785 int length = array.length;
786 if (length == 0) {
787 return EMPTY_ARRAY;
788 }
789 StringBuffer buffer = new StringBuffer();
790 for (int i = 0; i < length; i++) {
791 if (i == 0) {
792 buffer.append(ARRAY_START);
793 }
794 else {
795 buffer.append(ARRAY_ELEMENT_SEPARATOR);
796 }
797 buffer.append(array[i]);
798 }
799 buffer.append(ARRAY_END);
800 return buffer.toString();
801 }
802
803 /**
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>
811 */
812 public static String nullSafeToString(short[] array) {
813 if (array == null) {
814 return NULL_STRING;
815 }
816 int length = array.length;
817 if (length == 0) {
818 return EMPTY_ARRAY;
819 }
820 StringBuffer buffer = new StringBuffer();
821 for (int i = 0; i < length; i++) {
822 if (i == 0) {
823 buffer.append(ARRAY_START);
824 }
825 else {
826 buffer.append(ARRAY_ELEMENT_SEPARATOR);
827 }
828 buffer.append(array[i]);
829 }
830 buffer.append(ARRAY_END);
831 return buffer.toString();
832 }
833
834 }