Package: ObjectUtils

ObjectUtils

nameinstructionbranchcomplexitylinemethod
ObjectUtils()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
addObjectToArray(Object[], Object)
M: 0 C: 46
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
containsElement(Object[], Object)
M: 22 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getDisplayString(Object)
M: 7 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getIdentityHexString(Object)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hashCode(boolean)
M: 0 C: 6
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hashCode(double)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
hashCode(float)
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hashCode(long)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
identityToString(Object)
M: 0 C: 18
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
isCheckedException(Throwable)
M: 0 C: 10
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 1
100%
M: 0 C: 1
100%
isCompatibleWithThrowsClause(Throwable, Class[])
M: 0 C: 26
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
isEmpty(Object[])
M: 0 C: 9
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 1
100%
M: 0 C: 1
100%
nullSafeClassName(Object)
M: 8 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
nullSafeEquals(Object, Object)
M: 75 C: 60
44%
M: 31 C: 17
35%
M: 22 C: 3
12%
M: 12 C: 14
54%
M: 0 C: 1
100%
nullSafeHashCode(Object)
M: 0 C: 74
100%
M: 1 C: 21
95%
M: 1 C: 11
92%
M: 0 C: 22
100%
M: 0 C: 1
100%
nullSafeHashCode(Object[])
M: 0 C: 27
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
nullSafeHashCode(boolean[])
M: 0 C: 27
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
nullSafeHashCode(byte[])
M: 0 C: 26
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
nullSafeHashCode(char[])
M: 0 C: 26
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
nullSafeHashCode(double[])
M: 0 C: 27
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
nullSafeHashCode(float[])
M: 0 C: 27
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
nullSafeHashCode(int[])
M: 0 C: 26
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
nullSafeHashCode(long[])
M: 0 C: 27
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
nullSafeHashCode(short[])
M: 0 C: 26
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
nullSafeToString(Object)
M: 74 C: 8
10%
M: 22 C: 2
8%
M: 12 C: 1
8%
M: 21 C: 3
13%
M: 0 C: 1
100%
nullSafeToString(Object[])
M: 0 C: 47
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
nullSafeToString(boolean[])
M: 0 C: 46
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
nullSafeToString(byte[])
M: 0 C: 46
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
nullSafeToString(char[])
M: 0 C: 50
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
nullSafeToString(double[])
M: 0 C: 46
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
nullSafeToString(float[])
M: 0 C: 46
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
nullSafeToString(int[])
M: 0 C: 46
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
nullSafeToString(long[])
M: 0 C: 46
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
nullSafeToString(short[])
M: 0 C: 46
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
toObjectArray(Object)
M: 0 C: 59
100%
M: 0 C: 10
100%
M: 0 C: 6
100%
M: 0 C: 14
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.util.common;
13:
14: import java.lang.reflect.Array;
15: import java.util.Arrays;
16:
17: /**
18: * Miscellaneous object utility methods.
19: *
20: */
21: public abstract class ObjectUtils {
22:
23:         private static final int INITIAL_HASH = 7;
24:         private static final int MULTIPLIER = 31;
25:
26:         private static final String EMPTY_STRING = "";
27:         private static final String NULL_STRING = "null";
28:         private static final String ARRAY_START = "{";
29:         private static final String ARRAY_END = "}";
30:         private static final String EMPTY_ARRAY = ARRAY_START + ARRAY_END;
31:         private static final String ARRAY_ELEMENT_SEPARATOR = ", ";
32:
33:
34:         /**
35:          * Return whether the given throwable is a checked exception:
36:          * that is, neither a RuntimeException nor an Error.
37:          * @param ex the throwable to check
38:          * @return whether the throwable is a checked exception
39:          * @see java.lang.Exception
40:          * @see java.lang.RuntimeException
41:          * @see java.lang.Error
42:          */
43:         public static boolean isCheckedException(Throwable ex) {
44:•                return !(ex instanceof RuntimeException || ex instanceof Error);
45:         }
46:
47:         /**
48:          * Check whether the given exception is compatible with the exceptions
49:          * declared in a throws clause.
50:          * @param ex the exception to checked
51:          * @param declaredExceptions the exceptions declared in the throws clause
52:          * @return whether the given exception is compatible
53:          */
54:         public static boolean isCompatibleWithThrowsClause(Throwable ex, Class<? extends Throwable>[] declaredExceptions) {
55:•                if (!isCheckedException(ex)) {
56:                         return true;
57:                 }
58:•                if (declaredExceptions != null) {
59:•                        for (int i = 0; i < declaredExceptions.length; i++) {
60:•                                if (declaredExceptions[i].isAssignableFrom(ex.getClass())) {
61:                                         return true;
62:                                 }
63:                         }
64:                 }
65:                 return false;
66:         }
67:
68:         /**
69:          * Return whether the given array is empty: that is, <code>null</code>
70:          * or of zero length.
71:          * @param array the array to check
72:          * @return whether the given array is empty
73:          */
74:         public static boolean isEmpty(Object[] array) {
75:•                return (array == null || array.length == 0);
76:         }
77:
78:         /**
79:          * Check whether the given array contains the given element.
80:          * @param array the array to check (may be <code>null</code>,
81:          * in which case the return value will always be <code>false</code>)
82:          * @param element the element to check for
83:          * @return whether the element has been found in the given array
84:          */
85:         public static boolean containsElement(Object[] array, Object element) {
86:•                if (array == null) {
87:                         return false;
88:                 }
89:•                for (int i = 0; i < array.length; i++) {
90:•                        if (nullSafeEquals(array[i], element)) {
91:                                 return true;
92:                         }
93:                 }
94:                 return false;
95:         }
96:
97:         /**
98:          * Append the given Object to the given array, returning a new array
99:          * consisting of the input array contents plus the given Object.
100:          * @param array the array to append to (can be <code>null</code>)
101:          * @param obj the Object to append
102:          * @return the new array (of the same component type; never <code>null</code>)
103:          */
104:         public static Object[] addObjectToArray(Object[] array, Object obj) {
105:                 Class<?> compType = Object.class;
106:•                if (array != null) {
107:                         compType = array.getClass().getComponentType();
108:                 }
109:•                else if (obj != null) {
110:                         compType = obj.getClass();
111:                 }
112:•                int newArrLength = (array != null ? array.length + 1 : 1);
113:                 Object[] newArr = (Object[]) Array.newInstance(compType, newArrLength);
114:•                if (array != null) {
115:                         System.arraycopy(array, 0, newArr, 0, array.length);
116:                 }
117:                 newArr[newArr.length - 1] = obj;
118:                 return newArr;
119:         }
120:
121:         /**
122:          * Convert the given array (which may be a primitive array) to an
123:          * object array (if necessary of primitive wrapper objects).
124:          * <p>A <code>null</code> source value will be converted to an
125:          * empty Object array.
126:          * @param source the (potentially primitive) array
127:          * @return the corresponding object array (never <code>null</code>)
128:          * @throws IllegalArgumentException if the parameter is not an array
129:          */
130:         public static Object[] toObjectArray(Object source) {
131:•                if (source instanceof Object[]) {
132:                         return (Object[]) source;
133:                 }
134:•                if (source == null) {
135:                         return new Object[0];
136:                 }
137:•                if (!source.getClass().isArray()) {
138:                         throw new IllegalArgumentException("Source is not an array: " + source);
139:                 }
140:                 int length = Array.getLength(source);
141:•                if (length == 0) {
142:                         return new Object[0];
143:                 }
144:                 Class<?> wrapperType = Array.get(source, 0).getClass();
145:                 Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);
146:•                for (int i = 0; i < length; i++) {
147:                         newArray[i] = Array.get(source, i);
148:                 }
149:                 return newArray;
150:         }
151:
152:
153:         //---------------------------------------------------------------------
154:         // Convenience methods for content-based equality/hash-code handling
155:         //---------------------------------------------------------------------
156:
157:         /**
158:          * Determine if the given objects are equal, returning <code>true</code>
159:          * if both are <code>null</code> or <code>false</code> if only one is
160:          * <code>null</code>.
161:          * <p>Compares arrays with <code>Arrays.equals</code>, performing an equality
162:          * check based on the array elements rather than the array reference.
163:          * @param o1 first Object to compare
164:          * @param o2 second Object to compare
165:          * @return whether the given objects are equal
166:          * @see java.util.Arrays#equals
167:          */
168:         public static boolean nullSafeEquals(Object o1, Object o2) {
169:•                if (o1 == o2) {
170:                         return true;
171:                 }
172:•                if (o1 == null || o2 == null) {
173:                         return false;
174:                 }
175:•                if (o1.equals(o2)) {
176:                         return true;
177:                 }
178:•                if (o1.getClass().isArray() && o2.getClass().isArray()) {
179:•                        if (o1 instanceof Object[] && o2 instanceof Object[]) {
180:                                 return Arrays.equals((Object[]) o1, (Object[]) o2);
181:                         }
182:•                        if (o1 instanceof boolean[] && o2 instanceof boolean[]) {
183:                                 return Arrays.equals((boolean[]) o1, (boolean[]) o2);
184:                         }
185:•                        if (o1 instanceof byte[] && o2 instanceof byte[]) {
186:                                 return Arrays.equals((byte[]) o1, (byte[]) o2);
187:                         }
188:•                        if (o1 instanceof char[] && o2 instanceof char[]) {
189:                                 return Arrays.equals((char[]) o1, (char[]) o2);
190:                         }
191:•                        if (o1 instanceof double[] && o2 instanceof double[]) {
192:                                 return Arrays.equals((double[]) o1, (double[]) o2);
193:                         }
194:•                        if (o1 instanceof float[] && o2 instanceof float[]) {
195:                                 return Arrays.equals((float[]) o1, (float[]) o2);
196:                         }
197:•                        if (o1 instanceof int[] && o2 instanceof int[]) {
198:                                 return Arrays.equals((int[]) o1, (int[]) o2);
199:                         }
200:•                        if (o1 instanceof long[] && o2 instanceof long[]) {
201:                                 return Arrays.equals((long[]) o1, (long[]) o2);
202:                         }
203:•                        if (o1 instanceof short[] && o2 instanceof short[]) {
204:                                 return Arrays.equals((short[]) o1, (short[]) o2);
205:                         }
206:                 }
207:                 return false;
208:         }
209:
210:         /**
211:          * Return as hash code for the given object; typically the value of
212:          * <code>{@link Object#hashCode()}</code>. If the object is an array,
213:          * this method will delegate to any of the <code>nullSafeHashCode</code>
214:          * methods for arrays in this class. If the object is <code>null</code>,
215:          * this method returns 0.
216:          * @param obj object
217:          * @return hash code
218:          * @see #nullSafeHashCode(Object[])
219:          * @see #nullSafeHashCode(boolean[])
220:          * @see #nullSafeHashCode(byte[])
221:          * @see #nullSafeHashCode(char[])
222:          * @see #nullSafeHashCode(double[])
223:          * @see #nullSafeHashCode(float[])
224:          * @see #nullSafeHashCode(int[])
225:          * @see #nullSafeHashCode(long[])
226:          * @see #nullSafeHashCode(short[])
227:          */
228:         public static int nullSafeHashCode(Object obj) {
229:•                if (obj == null) {
230:                         return 0;
231:                 }
232:•                if (obj.getClass().isArray()) {
233:•                        if (obj instanceof Object[]) {
234:                                 return nullSafeHashCode((Object[]) obj);
235:                         }
236:•                        if (obj instanceof boolean[]) {
237:                                 return nullSafeHashCode((boolean[]) obj);
238:                         }
239:•                        if (obj instanceof byte[]) {
240:                                 return nullSafeHashCode((byte[]) obj);
241:                         }
242:•                        if (obj instanceof char[]) {
243:                                 return nullSafeHashCode((char[]) obj);
244:                         }
245:•                        if (obj instanceof double[]) {
246:                                 return nullSafeHashCode((double[]) obj);
247:                         }
248:•                        if (obj instanceof float[]) {
249:                                 return nullSafeHashCode((float[]) obj);
250:                         }
251:•                        if (obj instanceof int[]) {
252:                                 return nullSafeHashCode((int[]) obj);
253:                         }
254:•                        if (obj instanceof long[]) {
255:                                 return nullSafeHashCode((long[]) obj);
256:                         }
257:•                        if (obj instanceof short[]) {
258:                                 return nullSafeHashCode((short[]) obj);
259:                         }
260:                 }
261:                 return obj.hashCode();
262:         }
263:
264:         /**
265:          * Return a hash code based on the contents of the specified array.
266:          * If <code>array</code> is <code>null</code>, this method returns 0.
267:          * @param array of objects
268:          * @return hash code
269:          */
270:         public static int nullSafeHashCode(Object[] array) {
271:•                if (array == null) {
272:                         return 0;
273:                 }
274:                 int hash = INITIAL_HASH;
275:                 int arraySize = array.length;
276:•                for (int i = 0; i < arraySize; i++) {
277:                         hash = MULTIPLIER * hash + nullSafeHashCode(array[i]);
278:                 }
279:                 return hash;
280:         }
281:
282:         /**
283:          * Return a hash code based on the contents of the specified array.
284:          * If <code>array</code> is <code>null</code>, this method returns 0.
285:          * @param array of booleans
286:          * @return hash code
287:          */
288:         public static int nullSafeHashCode(boolean[] array) {
289:•                if (array == null) {
290:                         return 0;
291:                 }
292:                 int hash = INITIAL_HASH;
293:                 int arraySize = array.length;
294:•                for (int i = 0; i < arraySize; i++) {
295:                         hash = MULTIPLIER * hash + hashCode(array[i]);
296:                 }
297:                 return hash;
298:         }
299:
300:         /**
301:          * Return a hash code based on the contents of the specified array.
302:          * If <code>array</code> is <code>null</code>, this method returns 0.
303:          * @param array of bytes
304:          * @return hash code
305:          */
306:         public static int nullSafeHashCode(byte[] array) {
307:•                if (array == null) {
308:                         return 0;
309:                 }
310:                 int hash = INITIAL_HASH;
311:                 int arraySize = array.length;
312:•                for (int i = 0; i < arraySize; i++) {
313:                         hash = MULTIPLIER * hash + array[i];
314:                 }
315:                 return hash;
316:         }
317:
318:         /**
319:          * Return a hash code based on the contents of the specified array.
320:          * If <code>array</code> is <code>null</code>, this method returns 0.
321:          * @param array of chars
322:          * @return hash code
323:          */
324:         public static int nullSafeHashCode(char[] array) {
325:•                if (array == null) {
326:                         return 0;
327:                 }
328:                 int hash = INITIAL_HASH;
329:                 int arraySize = array.length;
330:•                for (int i = 0; i < arraySize; i++) {
331:                         hash = MULTIPLIER * hash + array[i];
332:                 }
333:                 return hash;
334:         }
335:
336:         /**
337:          * Return a hash code based on the contents of the specified array.
338:          * If <code>array</code> is <code>null</code>, this method returns 0.
339:          * @param array of doubles
340:          * @return hash code
341:          */
342:         public static int nullSafeHashCode(double[] array) {
343:•                if (array == null) {
344:                         return 0;
345:                 }
346:                 int hash = INITIAL_HASH;
347:                 int arraySize = array.length;
348:•                for (int i = 0; i < arraySize; i++) {
349:                         hash = MULTIPLIER * hash + hashCode(array[i]);
350:                 }
351:                 return hash;
352:         }
353:
354:         /**
355:          * Return a hash code based on the contents of the specified array.
356:          * If <code>array</code> is <code>null</code>, this method returns 0.
357:          * @param array of floats
358:          * @return hash code
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:          * @param array of ints
376:          * @return hash code
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:          * @param array of longs
394:          * @return hash codes
395:          */
396:         public static int nullSafeHashCode(long[] array) {
397:•                if (array == null) {
398:                         return 0;
399:                 }
400:                 int hash = INITIAL_HASH;
401:                 int arraySize = array.length;
402:•                for (int i = 0; i < arraySize; i++) {
403:                         hash = MULTIPLIER * hash + hashCode(array[i]);
404:                 }
405:                 return hash;
406:         }
407:
408:         /**
409:          * Return a hash code based on the contents of the specified array.
410:          * If <code>array</code> is <code>null</code>, this method returns 0.
411:          * @param array of shorts
412:          * @return hash code
413:          */
414:         public static int nullSafeHashCode(short[] array) {
415:•                if (array == null) {
416:                         return 0;
417:                 }
418:                 int hash = INITIAL_HASH;
419:                 int arraySize = array.length;
420:•                for (int i = 0; i < arraySize; i++) {
421:                         hash = MULTIPLIER * hash + array[i];
422:                 }
423:                 return hash;
424:         }
425:
426:         /**
427:          * Return the same value as <code>{@link Boolean#hashCode()}</code>.
428:          * @param bool boolean
429:          * @return hash code
430:          * @see Boolean#hashCode()
431:          */
432:         public static int hashCode(boolean bool) {
433:•                return bool ? 1231 : 1237;
434:         }
435:
436:         /**
437:          * Return the same value as <code>{@link Double#hashCode()}</code>.
438:          * @param dbl double
439:          * @return hash code
440:          * @see Double#hashCode()
441:          */
442:         public static int hashCode(double dbl) {
443:                 long bits = Double.doubleToLongBits(dbl);
444:                 return hashCode(bits);
445:         }
446:
447:         /**
448:          * Return the same value as <code>{@link Float#hashCode()}</code>.
449:          * @param flt float
450:          * @see Float#hashCode()
451: * @return hash code
452:          */
453:         public static int hashCode(float flt) {
454:                 return Float.floatToIntBits(flt);
455:         }
456:
457:         /**
458:          * Return the same value as <code>{@link Long#hashCode()}</code>.
459:          * @param lng long
460:          * @see Long#hashCode()
461: * @return hash code
462:          */
463:         public static int hashCode(long lng) {
464:                 return (int) (lng ^ (lng >>> 32));
465:         }
466:
467:
468:         //---------------------------------------------------------------------
469:         // Convenience methods for toString output
470:         //---------------------------------------------------------------------
471:
472:         /**
473:          * Return a String representation of an object's overall identity.
474:          * @param obj the object (may be <code>null</code>)
475:          * @return the object's identity as String representation,
476:          * or an empty String if the object was <code>null</code>
477:          */
478:         public static String identityToString(Object obj) {
479:•                if (obj == null) {
480:                         return EMPTY_STRING;
481:                 }
482:                 return obj.getClass().getName() + "@" + getIdentityHexString(obj);
483:         }
484:
485:         /**
486:          * Return a hex String form of an object's identity hash code.
487:          * @param obj the object
488:          * @return the object's identity code in hex notation
489:          */
490:         public static String getIdentityHexString(Object obj) {
491:                 return Integer.toHexString(System.identityHashCode(obj));
492:         }
493:
494:         /**
495:          * Return a content-based String representation if <code>obj</code> is
496:          * not <code>null</code> otherwise returns an empty String.
497:          * <p>Differs from {@link #nullSafeToString(Object)} in that it returns
498:          * an empty String rather than "null" for a <code>null</code> value.
499:          * @param obj the object to build a display String for
500:          * @return a display String representation of <code>obj</code>
501:          * @see #nullSafeToString(Object)
502:          */
503:         public static String getDisplayString(Object obj) {
504:•                if (obj == null) {
505:                         return EMPTY_STRING;
506:                 }
507:                 return nullSafeToString(obj);
508:         }
509:
510:         /**
511:          * Determine the class name for the given object.
512:          * <p>Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
513:          * @param obj the object to introspect (may be <code>null</code>)
514:          * @return the corresponding class name
515:          */
516:         public static String nullSafeClassName(Object obj) {
517:•                return (obj != null ? obj.getClass().getName() : NULL_STRING);
518:         }
519:
520:         /**
521:          * Return a String representation of the specified Object.
522:          * <p>Builds a String representation of the contents in case of an array.
523:          * Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
524:          * @param obj the object to build a String representation for
525:          * @return a String representation of <code>obj</code>
526:          */
527:         public static String nullSafeToString(Object obj) {
528:•                if (obj == null) {
529:                         return NULL_STRING;
530:                 }
531:•                if (obj instanceof String) {
532:                         return (String) obj;
533:                 }
534:•                if (obj instanceof Object[]) {
535:                         return nullSafeToString((Object[]) obj);
536:                 }
537:•                if (obj instanceof boolean[]) {
538:                         return nullSafeToString((boolean[]) obj);
539:                 }
540:•                if (obj instanceof byte[]) {
541:                         return nullSafeToString((byte[]) obj);
542:                 }
543:•                if (obj instanceof char[]) {
544:                         return nullSafeToString((char[]) obj);
545:                 }
546:•                if (obj instanceof double[]) {
547:                         return nullSafeToString((double[]) obj);
548:                 }
549:•                if (obj instanceof float[]) {
550:                         return nullSafeToString((float[]) obj);
551:                 }
552:•                if (obj instanceof int[]) {
553:                         return nullSafeToString((int[]) obj);
554:                 }
555:•                if (obj instanceof long[]) {
556:                         return nullSafeToString((long[]) obj);
557:                 }
558:•                if (obj instanceof short[]) {
559:                         return nullSafeToString((short[]) obj);
560:                 }
561:                 String str = obj.toString();
562:•                return (str != null ? str : EMPTY_STRING);
563:         }
564:
565:         /**
566:          * Return a String representation of the contents of the specified array.
567:          * <p>The String representation consists of a list of the array's elements,
568:          * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
569:          * by the characters <code>", "</code> (a comma followed by a space). Returns
570:          * <code>"null"</code> if <code>array</code> is <code>null</code>.
571:          * @param array the array to build a String representation for
572:          * @return a String representation of <code>array</code>
573:          */
574:         public static String nullSafeToString(Object[] array) {
575:•                if (array == null) {
576:                         return NULL_STRING;
577:                 }
578:                 int length = array.length;
579:•                if (length == 0) {
580:                         return EMPTY_ARRAY;
581:                 }
582:                 StringBuffer buffer = new StringBuffer();
583:•                for (int i = 0; i < length; i++) {
584:•                        if (i == 0) {
585:                                 buffer.append(ARRAY_START);
586:                         }
587:                         else {
588:                                 buffer.append(ARRAY_ELEMENT_SEPARATOR);
589:                         }
590:                         buffer.append(String.valueOf(array[i]));
591:                 }
592:                 buffer.append(ARRAY_END);
593:                 return buffer.toString();
594:         }
595:
596:         /**
597:          * Return a String representation of the contents of the specified array.
598:          * <p>The String representation consists of a list of the array's elements,
599:          * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
600:          * by the characters <code>", "</code> (a comma followed by a space). Returns
601:          * <code>"null"</code> if <code>array</code> is <code>null</code>.
602:          * @param array the array to build a String representation for
603:          * @return a String representation of <code>array</code>
604:          */
605:         public static String nullSafeToString(boolean[] array) {
606:•                if (array == null) {
607:                         return NULL_STRING;
608:                 }
609:                 int length = array.length;
610:•                if (length == 0) {
611:                         return EMPTY_ARRAY;
612:                 }
613:                 StringBuffer buffer = new StringBuffer();
614:•                for (int i = 0; i < length; i++) {
615:•                        if (i == 0) {
616:                                 buffer.append(ARRAY_START);
617:                         }
618:                         else {
619:                                 buffer.append(ARRAY_ELEMENT_SEPARATOR);
620:                         }
621:
622:                         buffer.append(array[i]);
623:                 }
624:                 buffer.append(ARRAY_END);
625:                 return buffer.toString();
626:         }
627:
628:         /**
629:          * Return a String representation of the contents of the specified array.
630:          * <p>The String representation consists of a list of the array's elements,
631:          * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
632:          * by the characters <code>", "</code> (a comma followed by a space). Returns
633:          * <code>"null"</code> if <code>array</code> is <code>null</code>.
634:          * @param array the array to build a String representation for
635:          * @return a String representation of <code>array</code>
636:          */
637:         public static String nullSafeToString(byte[] array) {
638:•                if (array == null) {
639:                         return NULL_STRING;
640:                 }
641:                 int length = array.length;
642:•                if (length == 0) {
643:                         return EMPTY_ARRAY;
644:                 }
645:                 StringBuffer buffer = new StringBuffer();
646:•                for (int i = 0; i < length; i++) {
647:•                        if (i == 0) {
648:                                 buffer.append(ARRAY_START);
649:                         }
650:                         else {
651:                                 buffer.append(ARRAY_ELEMENT_SEPARATOR);
652:                         }
653:                         buffer.append(array[i]);
654:                 }
655:                 buffer.append(ARRAY_END);
656:                 return buffer.toString();
657:         }
658:
659:         /**
660:          * Return a String representation of the contents of the specified array.
661:          * <p>The String representation consists of a list of the array's elements,
662:          * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
663:          * by the characters <code>", "</code> (a comma followed by a space). Returns
664:          * <code>"null"</code> if <code>array</code> is <code>null</code>.
665:          * @param array the array to build a String representation for
666:          * @return a String representation of <code>array</code>
667:          */
668:         public static String nullSafeToString(char[] array) {
669:•                if (array == null) {
670:                         return NULL_STRING;
671:                 }
672:                 int length = array.length;
673:•                if (length == 0) {
674:                         return EMPTY_ARRAY;
675:                 }
676:                 StringBuffer buffer = new StringBuffer();
677:•                for (int i = 0; i < length; i++) {
678:•                        if (i == 0) {
679:                                 buffer.append(ARRAY_START);
680:                         }
681:                         else {
682:                                 buffer.append(ARRAY_ELEMENT_SEPARATOR);
683:                         }
684:                         buffer.append("'").append(array[i]).append("'");
685:                 }
686:                 buffer.append(ARRAY_END);
687:                 return buffer.toString();
688:         }
689:
690:         /**
691:          * Return a String representation of the contents of the specified array.
692:          * <p>The String representation consists of a list of the array's elements,
693:          * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
694:          * by the characters <code>", "</code> (a comma followed by a space). Returns
695:          * <code>"null"</code> if <code>array</code> is <code>null</code>.
696:          * @param array the array to build a String representation for
697:          * @return a String representation of <code>array</code>
698:          */
699:         public static String nullSafeToString(double[] array) {
700:•                if (array == null) {
701:                         return NULL_STRING;
702:                 }
703:                 int length = array.length;
704:•                if (length == 0) {
705:                         return EMPTY_ARRAY;
706:                 }
707:                 StringBuffer buffer = new StringBuffer();
708:•                for (int i = 0; i < length; i++) {
709:•                        if (i == 0) {
710:                                 buffer.append(ARRAY_START);
711:                         }
712:                         else {
713:                                 buffer.append(ARRAY_ELEMENT_SEPARATOR);
714:                         }
715:
716:                         buffer.append(array[i]);
717:                 }
718:                 buffer.append(ARRAY_END);
719:                 return buffer.toString();
720:         }
721:
722:         /**
723:          * Return a String representation of the contents of the specified array.
724:          * <p>The String representation consists of a list of the array's elements,
725:          * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
726:          * by the characters <code>", "</code> (a comma followed by a space). Returns
727:          * <code>"null"</code> if <code>array</code> is <code>null</code>.
728:          * @param array the array to build a String representation for
729:          * @return a String representation of <code>array</code>
730:          */
731:         public static String nullSafeToString(float[] array) {
732:•                if (array == null) {
733:                         return NULL_STRING;
734:                 }
735:                 int length = array.length;
736:•                if (length == 0) {
737:                         return EMPTY_ARRAY;
738:                 }
739:                 StringBuffer buffer = new StringBuffer();
740:•                for (int i = 0; i < length; i++) {
741:•                        if (i == 0) {
742:                                 buffer.append(ARRAY_START);
743:                         }
744:                         else {
745:                                 buffer.append(ARRAY_ELEMENT_SEPARATOR);
746:                         }
747:
748:                         buffer.append(array[i]);
749:                 }
750:                 buffer.append(ARRAY_END);
751:                 return buffer.toString();
752:         }
753:
754:         /**
755:          * Return a String representation of the contents of the specified array.
756:          * <p>The String representation consists of a list of the array's elements,
757:          * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
758:          * by the characters <code>", "</code> (a comma followed by a space). Returns
759:          * <code>"null"</code> if <code>array</code> is <code>null</code>.
760:          * @param array the array to build a String representation for
761:          * @return a String representation of <code>array</code>
762:          */
763:         public static String nullSafeToString(int[] array) {
764:•                if (array == null) {
765:                         return NULL_STRING;
766:                 }
767:                 int length = array.length;
768:•                if (length == 0) {
769:                         return EMPTY_ARRAY;
770:                 }
771:                 StringBuffer buffer = new StringBuffer();
772:•                for (int i = 0; i < length; i++) {
773:•                        if (i == 0) {
774:                                 buffer.append(ARRAY_START);
775:                         }
776:                         else {
777:                                 buffer.append(ARRAY_ELEMENT_SEPARATOR);
778:                         }
779:                         buffer.append(array[i]);
780:                 }
781:                 buffer.append(ARRAY_END);
782:                 return buffer.toString();
783:         }
784:
785:         /**
786:          * Return a String representation of the contents of the specified array.
787:          * <p>The String representation consists of a list of the array's elements,
788:          * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
789:          * by the characters <code>", "</code> (a comma followed by a space). Returns
790:          * <code>"null"</code> if <code>array</code> is <code>null</code>.
791:          * @param array the array to build a String representation for
792:          * @return a String representation of <code>array</code>
793:          */
794:         public static String nullSafeToString(long[] array) {
795:•                if (array == null) {
796:                         return NULL_STRING;
797:                 }
798:                 int length = array.length;
799:•                if (length == 0) {
800:                         return EMPTY_ARRAY;
801:                 }
802:                 StringBuffer buffer = new StringBuffer();
803:•                for (int i = 0; i < length; i++) {
804:•                        if (i == 0) {
805:                                 buffer.append(ARRAY_START);
806:                         }
807:                         else {
808:                                 buffer.append(ARRAY_ELEMENT_SEPARATOR);
809:                         }
810:                         buffer.append(array[i]);
811:                 }
812:                 buffer.append(ARRAY_END);
813:                 return buffer.toString();
814:         }
815:
816:         /**
817:          * Return a String representation of the contents of the specified array.
818:          * <p>The String representation consists of a list of the array's elements,
819:          * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
820:          * by the characters <code>", "</code> (a comma followed by a space). Returns
821:          * <code>"null"</code> if <code>array</code> is <code>null</code>.
822:          * @param array the array to build a String representation for
823:          * @return a String representation of <code>array</code>
824:          */
825:         public static String nullSafeToString(short[] array) {
826:•                if (array == null) {
827:                         return NULL_STRING;
828:                 }
829:                 int length = array.length;
830:•                if (length == 0) {
831:                         return EMPTY_ARRAY;
832:                 }
833:                 StringBuffer buffer = new StringBuffer();
834:•                for (int i = 0; i < length; i++) {
835:•                        if (i == 0) {
836:                                 buffer.append(ARRAY_START);
837:                         }
838:                         else {
839:                                 buffer.append(ARRAY_ELEMENT_SEPARATOR);
840:                         }
841:                         buffer.append(array[i]);
842:                 }
843:                 buffer.append(ARRAY_END);
844:                 return buffer.toString();
845:         }
846:
847: }