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