001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.util;
018:
019: import java.lang.reflect.Array;
020: import java.util.Arrays;
021:
022: /**
023: * Miscellaneous object utility methods. Mainly for internal use within the
024: * framework; consider Jakarta's Commons Lang for a more comprehensive suite
025: * of object utilities.
026: *
027: * @author Juergen Hoeller
028: * @author Keith Donald
029: * @author Rod Johnson
030: * @author Rob Harrop
031: * @author Alex Ruiz
032: * @since 19.03.2004
033: * @see org.apache.commons.lang.ObjectUtils
034: */
035: public abstract class ObjectUtils {
036:
037: private static final int INITIAL_HASH = 7;
038: private static final int MULTIPLIER = 31;
039:
040: private static final String EMPTY_STRING = "";
041: private static final String NULL_STRING = "null";
042: private static final String ARRAY_START = "{";
043: private static final String ARRAY_END = "}";
044: private static final String EMPTY_ARRAY = ARRAY_START + ARRAY_END;
045: private static final String ARRAY_ELEMENT_SEPARATOR = ", ";
046:
047: /**
048: * Return whether the given throwable is a checked exception:
049: * that is, neither a RuntimeException nor an Error.
050: * @param ex the throwable to check
051: * @return whether the throwable is a checked exception
052: * @see java.lang.Exception
053: * @see java.lang.RuntimeException
054: * @see java.lang.Error
055: */
056: public static boolean isCheckedException(Throwable ex) {
057: return !(ex instanceof RuntimeException || ex instanceof Error);
058: }
059:
060: /**
061: * Check whether the given exception is compatible with the exceptions
062: * declared in a throws clause.
063: * @param ex the exception to checked
064: * @param declaredExceptions the exceptions declared in the throws clause
065: * @return whether the given exception is compatible
066: */
067: public static boolean isCompatibleWithThrowsClause(Throwable ex,
068: Class[] declaredExceptions) {
069: if (!isCheckedException(ex)) {
070: return true;
071: }
072: if (declaredExceptions != null) {
073: for (int i = 0; i < declaredExceptions.length; i++) {
074: if (declaredExceptions[i].isAssignableFrom(ex
075: .getClass())) {
076: return true;
077: }
078: }
079: }
080: return false;
081: }
082:
083: /**
084: * Return whether the given array is empty: that is, <code>null</code>
085: * or of zero length.
086: * @param array the array to check
087: * @return whether the given array is empty
088: */
089: public static boolean isEmpty(Object[] array) {
090: return (array == null || array.length == 0);
091: }
092:
093: /**
094: * Check whether the given array contains the given element.
095: * @param array the array to check (may be <code>null</code>,
096: * in which case the return value will always be <code>false</code>)
097: * @param element the element to check for
098: * @return whether the element has been found in the given array
099: */
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: } 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,
128: 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(
154: "Source is not an array: " + source);
155: }
156: int length = Array.getLength(source);
157: if (length == 0) {
158: return new Object[0];
159: }
160: Class wrapperType = Array.get(source, 0).getClass();
161: Object[] newArray = (Object[]) Array.newInstance(wrapperType,
162: length);
163: for (int i = 0; i < length; i++) {
164: newArray[i] = Array.get(source, i);
165: }
166: return newArray;
167: }
168:
169: //---------------------------------------------------------------------
170: // Convenience methods for content-based equality/hash-code handling
171: //---------------------------------------------------------------------
172:
173: /**
174: * Determine if the given objects are equal, returning <code>true</code>
175: * if both are <code>null</code> or <code>false</code> if only one is
176: * <code>null</code>.
177: * <p>Compares arrays with <code>Arrays.equals</code>, performing an equality
178: * check based on the array elements rather than the array reference.
179: * @param o1 first Object to compare
180: * @param o2 second Object to compare
181: * @return whether the given objects are equal
182: * @see java.util.Arrays#equals
183: */
184: public static boolean nullSafeEquals(Object o1, Object o2) {
185: if (o1 == o2) {
186: return true;
187: }
188: if (o1 == null || o2 == null) {
189: return false;
190: }
191: if (o1.equals(o2)) {
192: return true;
193: }
194: if (o1.getClass().isArray() && o2.getClass().isArray()) {
195: if (o1 instanceof Object[] && o2 instanceof Object[]) {
196: return Arrays.equals((Object[]) o1, (Object[]) o2);
197: }
198: if (o1 instanceof boolean[] && o2 instanceof boolean[]) {
199: return Arrays.equals((boolean[]) o1, (boolean[]) o2);
200: }
201: if (o1 instanceof byte[] && o2 instanceof byte[]) {
202: return Arrays.equals((byte[]) o1, (byte[]) o2);
203: }
204: if (o1 instanceof char[] && o2 instanceof char[]) {
205: return Arrays.equals((char[]) o1, (char[]) o2);
206: }
207: if (o1 instanceof double[] && o2 instanceof double[]) {
208: return Arrays.equals((double[]) o1, (double[]) o2);
209: }
210: if (o1 instanceof float[] && o2 instanceof float[]) {
211: return Arrays.equals((float[]) o1, (float[]) o2);
212: }
213: if (o1 instanceof int[] && o2 instanceof int[]) {
214: return Arrays.equals((int[]) o1, (int[]) o2);
215: }
216: if (o1 instanceof long[] && o2 instanceof long[]) {
217: return Arrays.equals((long[]) o1, (long[]) o2);
218: }
219: if (o1 instanceof short[] && o2 instanceof short[]) {
220: return Arrays.equals((short[]) o1, (short[]) o2);
221: }
222: }
223: return false;
224: }
225:
226: /**
227: * Return as hash code for the given object; typically the value of
228: * <code>{@link Object#hashCode()}</code>. If the object is an array,
229: * this method will delegate to any of the <code>nullSafeHashCode</code>
230: * methods for arrays in this class. If the object is <code>null</code>,
231: * this method returns 0.
232: * @see #nullSafeHashCode(Object[])
233: * @see #nullSafeHashCode(boolean[])
234: * @see #nullSafeHashCode(byte[])
235: * @see #nullSafeHashCode(char[])
236: * @see #nullSafeHashCode(double[])
237: * @see #nullSafeHashCode(float[])
238: * @see #nullSafeHashCode(int[])
239: * @see #nullSafeHashCode(long[])
240: * @see #nullSafeHashCode(short[])
241: */
242: public static int nullSafeHashCode(Object obj) {
243: if (obj == null) {
244: return 0;
245: }
246: if (obj.getClass().isArray()) {
247: if (obj instanceof Object[]) {
248: return nullSafeHashCode((Object[]) obj);
249: }
250: if (obj instanceof boolean[]) {
251: return nullSafeHashCode((boolean[]) obj);
252: }
253: if (obj instanceof byte[]) {
254: return nullSafeHashCode((byte[]) obj);
255: }
256: if (obj instanceof char[]) {
257: return nullSafeHashCode((char[]) obj);
258: }
259: if (obj instanceof double[]) {
260: return nullSafeHashCode((double[]) obj);
261: }
262: if (obj instanceof float[]) {
263: return nullSafeHashCode((float[]) obj);
264: }
265: if (obj instanceof int[]) {
266: return nullSafeHashCode((int[]) obj);
267: }
268: if (obj instanceof long[]) {
269: return nullSafeHashCode((long[]) obj);
270: }
271: if (obj instanceof short[]) {
272: return nullSafeHashCode((short[]) obj);
273: }
274: }
275: return obj.hashCode();
276: }
277:
278: /**
279: * Return a hash code based on the contents of the specified array.
280: * If <code>array</code> is <code>null</code>, this method returns 0.
281: */
282: public static int nullSafeHashCode(Object[] array) {
283: if (array == null) {
284: return 0;
285: }
286: int hash = INITIAL_HASH;
287: int arraySize = array.length;
288: for (int i = 0; i < arraySize; i++) {
289: hash = MULTIPLIER * hash + nullSafeHashCode(array[i]);
290: }
291: return hash;
292: }
293:
294: /**
295: * Return a hash code based on the contents of the specified array.
296: * If <code>array</code> is <code>null</code>, this method returns 0.
297: */
298: public static int nullSafeHashCode(boolean[] array) {
299: if (array == null) {
300: return 0;
301: }
302: int hash = INITIAL_HASH;
303: int arraySize = array.length;
304: for (int i = 0; i < arraySize; i++) {
305: hash = MULTIPLIER * hash + hashCode(array[i]);
306: }
307: return hash;
308: }
309:
310: /**
311: * Return a hash code based on the contents of the specified array.
312: * If <code>array</code> is <code>null</code>, this method returns 0.
313: */
314: public static int nullSafeHashCode(byte[] array) {
315: if (array == null) {
316: return 0;
317: }
318: int hash = INITIAL_HASH;
319: int arraySize = array.length;
320: for (int i = 0; i < arraySize; i++) {
321: hash = MULTIPLIER * hash + array[i];
322: }
323: return hash;
324: }
325:
326: /**
327: * Return a hash code based on the contents of the specified array.
328: * If <code>array</code> is <code>null</code>, this method returns 0.
329: */
330: public static int nullSafeHashCode(char[] array) {
331: if (array == null) {
332: return 0;
333: }
334: int hash = INITIAL_HASH;
335: int arraySize = array.length;
336: for (int i = 0; i < arraySize; i++) {
337: hash = MULTIPLIER * hash + array[i];
338: }
339: return hash;
340: }
341:
342: /**
343: * Return a hash code based on the contents of the specified array.
344: * If <code>array</code> is <code>null</code>, this method returns 0.
345: */
346: public static int nullSafeHashCode(double[] array) {
347: if (array == null) {
348: return 0;
349: }
350: int hash = INITIAL_HASH;
351: int arraySize = array.length;
352: for (int i = 0; i < arraySize; i++) {
353: hash = MULTIPLIER * hash + hashCode(array[i]);
354: }
355: return hash;
356: }
357:
358: /**
359: * Return a hash code based on the contents of the specified array.
360: * If <code>array</code> is <code>null</code>, this method returns 0.
361: */
362: public static int nullSafeHashCode(float[] array) {
363: if (array == null) {
364: return 0;
365: }
366: int hash = INITIAL_HASH;
367: int arraySize = array.length;
368: for (int i = 0; i < arraySize; i++) {
369: hash = MULTIPLIER * hash + hashCode(array[i]);
370: }
371: return hash;
372: }
373:
374: /**
375: * Return a hash code based on the contents of the specified array.
376: * If <code>array</code> is <code>null</code>, this method returns 0.
377: */
378: public static int nullSafeHashCode(int[] array) {
379: if (array == null) {
380: return 0;
381: }
382: int hash = INITIAL_HASH;
383: int arraySize = array.length;
384: for (int i = 0; i < arraySize; i++) {
385: hash = MULTIPLIER * hash + array[i];
386: }
387: return hash;
388: }
389:
390: /**
391: * Return a hash code based on the contents of the specified array.
392: * If <code>array</code> is <code>null</code>, this method returns 0.
393: */
394: public static int nullSafeHashCode(long[] array) {
395: if (array == null) {
396: return 0;
397: }
398: int hash = INITIAL_HASH;
399: int arraySize = array.length;
400: for (int i = 0; i < arraySize; i++) {
401: hash = MULTIPLIER * hash + hashCode(array[i]);
402: }
403: return hash;
404: }
405:
406: /**
407: * Return a hash code based on the contents of the specified array.
408: * If <code>array</code> is <code>null</code>, this method returns 0.
409: */
410: public static int nullSafeHashCode(short[] array) {
411: if (array == null) {
412: return 0;
413: }
414: int hash = INITIAL_HASH;
415: int arraySize = array.length;
416: for (int i = 0; i < arraySize; i++) {
417: hash = MULTIPLIER * hash + array[i];
418: }
419: return hash;
420: }
421:
422: /**
423: * Return the same value as <code>{@link Boolean#hashCode()}</code>.
424: * @see Boolean#hashCode()
425: */
426: public static int hashCode(boolean bool) {
427: return bool ? 1231 : 1237;
428: }
429:
430: /**
431: * Return the same value as <code>{@link Double#hashCode()}</code>.
432: * @see Double#hashCode()
433: */
434: public static int hashCode(double dbl) {
435: long bits = Double.doubleToLongBits(dbl);
436: return hashCode(bits);
437: }
438:
439: /**
440: * Return the same value as <code>{@link Float#hashCode()}</code>.
441: * @see Float#hashCode()
442: */
443: public static int hashCode(float flt) {
444: return Float.floatToIntBits(flt);
445: }
446:
447: /**
448: * Return the same value as <code>{@link Long#hashCode()}</code>.
449: * @see Long#hashCode()
450: */
451: public static int hashCode(long lng) {
452: return (int) (lng ^ (lng >>> 32));
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() + "@"
470: + getIdentityHexString(obj);
471: }
472:
473: /**
474: * Return a hex String form of an object's identity hash code.
475: * @param obj the object
476: * @return the object's identity code in hex notation
477: */
478: public static String getIdentityHexString(Object obj) {
479: return Integer.toHexString(System.identityHashCode(obj));
480: }
481:
482: /**
483: * Return a content-based String representation if <code>obj</code> is
484: * not <code>null</code>; otherwise returns an empty String.
485: * <p>Differs from {@link #nullSafeToString(Object)} in that it returns
486: * an empty String rather than "null" for a <code>null</code> value.
487: * @param obj the object to build a display String for
488: * @return a display String representation of <code>obj</code>
489: * @see #nullSafeToString(Object)
490: */
491: public static String getDisplayString(Object obj) {
492: if (obj == null) {
493: return EMPTY_STRING;
494: }
495: return nullSafeToString(obj);
496: }
497:
498: /**
499: * Determine the class name for the given object.
500: * <p>Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
501: * @param obj the object to introspect (may be <code>null</code>)
502: * @return the corresponding class name
503: */
504: public static String nullSafeClassName(Object obj) {
505: return (obj != null ? obj.getClass().getName() : NULL_STRING);
506: }
507:
508: /**
509: * Return a String representation of the specified Object.
510: * <p>Builds a String representation of the contents in case of an array.
511: * Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
512: * @param obj the object to build a String representation for
513: * @return a String representation of <code>obj</code>
514: */
515: public static String nullSafeToString(Object obj) {
516: if (obj == null) {
517: return NULL_STRING;
518: }
519: if (obj instanceof String) {
520: return (String) obj;
521: }
522: if (obj instanceof Object[]) {
523: return nullSafeToString((Object[]) obj);
524: }
525: if (obj instanceof boolean[]) {
526: return nullSafeToString((boolean[]) obj);
527: }
528: if (obj instanceof byte[]) {
529: return nullSafeToString((byte[]) obj);
530: }
531: if (obj instanceof char[]) {
532: return nullSafeToString((char[]) obj);
533: }
534: if (obj instanceof double[]) {
535: return nullSafeToString((double[]) obj);
536: }
537: if (obj instanceof float[]) {
538: return nullSafeToString((float[]) obj);
539: }
540: if (obj instanceof int[]) {
541: return nullSafeToString((int[]) obj);
542: }
543: if (obj instanceof long[]) {
544: return nullSafeToString((long[]) obj);
545: }
546: if (obj instanceof short[]) {
547: return nullSafeToString((short[]) obj);
548: }
549: String str = obj.toString();
550: return (str != null ? str : EMPTY_STRING);
551: }
552:
553: /**
554: * Return a String representation of the contents of the specified array.
555: * <p>The String representation consists of a list of the array's elements,
556: * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
557: * by the characters <code>", "</code> (a comma followed by a space). Returns
558: * <code>"null"</code> if <code>array</code> is <code>null</code>.
559: * @param array the array to build a String representation for
560: * @return a String representation of <code>array</code>
561: */
562: public static String nullSafeToString(Object[] array) {
563: if (array == null) {
564: return NULL_STRING;
565: }
566: int length = array.length;
567: if (length == 0) {
568: return EMPTY_ARRAY;
569: }
570: StringBuffer buffer = new StringBuffer();
571: for (int i = 0; i < length; i++) {
572: if (i == 0) {
573: buffer.append(ARRAY_START);
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: } 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: } else {
636: buffer.append(ARRAY_ELEMENT_SEPARATOR);
637: }
638: buffer.append(array[i]);
639: }
640: buffer.append(ARRAY_END);
641: return buffer.toString();
642: }
643:
644: /**
645: * Return a String representation of the contents of the specified array.
646: * <p>The String representation consists of a list of the array's elements,
647: * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
648: * by the characters <code>", "</code> (a comma followed by a space). Returns
649: * <code>"null"</code> if <code>array</code> is <code>null</code>.
650: * @param array the array to build a String representation for
651: * @return a String representation of <code>array</code>
652: */
653: public static String nullSafeToString(char[] array) {
654: if (array == null) {
655: return NULL_STRING;
656: }
657: int length = array.length;
658: if (length == 0) {
659: return EMPTY_ARRAY;
660: }
661: StringBuffer buffer = new StringBuffer();
662: for (int i = 0; i < length; i++) {
663: if (i == 0) {
664: buffer.append(ARRAY_START);
665: } else {
666: buffer.append(ARRAY_ELEMENT_SEPARATOR);
667: }
668: buffer.append("'").append(array[i]).append("'");
669: }
670: buffer.append(ARRAY_END);
671: return buffer.toString();
672: }
673:
674: /**
675: * Return a String representation of the contents of the specified array.
676: * <p>The String representation consists of a list of the array's elements,
677: * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
678: * by the characters <code>", "</code> (a comma followed by a space). Returns
679: * <code>"null"</code> if <code>array</code> is <code>null</code>.
680: * @param array the array to build a String representation for
681: * @return a String representation of <code>array</code>
682: */
683: public static String nullSafeToString(double[] array) {
684: if (array == null) {
685: return NULL_STRING;
686: }
687: int length = array.length;
688: if (length == 0) {
689: return EMPTY_ARRAY;
690: }
691: StringBuffer buffer = new StringBuffer();
692: for (int i = 0; i < length; i++) {
693: if (i == 0) {
694: buffer.append(ARRAY_START);
695: } else {
696: buffer.append(ARRAY_ELEMENT_SEPARATOR);
697: }
698:
699: buffer.append(array[i]);
700: }
701: buffer.append(ARRAY_END);
702: return buffer.toString();
703: }
704:
705: /**
706: * Return a String representation of the contents of the specified array.
707: * <p>The String representation consists of a list of the array's elements,
708: * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
709: * by the characters <code>", "</code> (a comma followed by a space). Returns
710: * <code>"null"</code> if <code>array</code> is <code>null</code>.
711: * @param array the array to build a String representation for
712: * @return a String representation of <code>array</code>
713: */
714: public static String nullSafeToString(float[] array) {
715: if (array == null) {
716: return NULL_STRING;
717: }
718: int length = array.length;
719: if (length == 0) {
720: return EMPTY_ARRAY;
721: }
722: StringBuffer buffer = new StringBuffer();
723: for (int i = 0; i < length; i++) {
724: if (i == 0) {
725: buffer.append(ARRAY_START);
726: } else {
727: buffer.append(ARRAY_ELEMENT_SEPARATOR);
728: }
729:
730: buffer.append(array[i]);
731: }
732: buffer.append(ARRAY_END);
733: return buffer.toString();
734: }
735:
736: /**
737: * Return a String representation of the contents of the specified array.
738: * <p>The String representation consists of a list of the array's elements,
739: * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
740: * by the characters <code>", "</code> (a comma followed by a space). Returns
741: * <code>"null"</code> if <code>array</code> is <code>null</code>.
742: * @param array the array to build a String representation for
743: * @return a String representation of <code>array</code>
744: */
745: public static String nullSafeToString(int[] array) {
746: if (array == null) {
747: return NULL_STRING;
748: }
749: int length = array.length;
750: if (length == 0) {
751: return EMPTY_ARRAY;
752: }
753: StringBuffer buffer = new StringBuffer();
754: for (int i = 0; i < length; i++) {
755: if (i == 0) {
756: buffer.append(ARRAY_START);
757: } else {
758: buffer.append(ARRAY_ELEMENT_SEPARATOR);
759: }
760: buffer.append(array[i]);
761: }
762: buffer.append(ARRAY_END);
763: return buffer.toString();
764: }
765:
766: /**
767: * Return a String representation of the contents of the specified array.
768: * <p>The String representation consists of a list of the array's elements,
769: * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
770: * by the characters <code>", "</code> (a comma followed by a space). Returns
771: * <code>"null"</code> if <code>array</code> is <code>null</code>.
772: * @param array the array to build a String representation for
773: * @return a String representation of <code>array</code>
774: */
775: public static String nullSafeToString(long[] array) {
776: if (array == null) {
777: return NULL_STRING;
778: }
779: int length = array.length;
780: if (length == 0) {
781: return EMPTY_ARRAY;
782: }
783: StringBuffer buffer = new StringBuffer();
784: for (int i = 0; i < length; i++) {
785: if (i == 0) {
786: buffer.append(ARRAY_START);
787: } else {
788: buffer.append(ARRAY_ELEMENT_SEPARATOR);
789: }
790: buffer.append(array[i]);
791: }
792: buffer.append(ARRAY_END);
793: return buffer.toString();
794: }
795:
796: /**
797: * Return a String representation of the contents of the specified array.
798: * <p>The String representation consists of a list of the array's elements,
799: * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
800: * by the characters <code>", "</code> (a comma followed by a space). Returns
801: * <code>"null"</code> if <code>array</code> is <code>null</code>.
802: * @param array the array to build a String representation for
803: * @return a String representation of <code>array</code>
804: */
805: public static String nullSafeToString(short[] array) {
806: if (array == null) {
807: return NULL_STRING;
808: }
809: int length = array.length;
810: if (length == 0) {
811: return EMPTY_ARRAY;
812: }
813: StringBuffer buffer = new StringBuffer();
814: for (int i = 0; i < length; i++) {
815: if (i == 0) {
816: buffer.append(ARRAY_START);
817: } else {
818: buffer.append(ARRAY_ELEMENT_SEPARATOR);
819: }
820: buffer.append(array[i]);
821: }
822: buffer.append(ARRAY_END);
823: return buffer.toString();
824: }
825:
826: }
|