Source Code Cross Referenced for ObjectUtils.java in  » J2EE » spring-framework-2.0.6 » org » springframework » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » spring framework 2.0.6 » org.springframework.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:             * Append the given Object to the given array, returning a new array
095:             * consisting of the input array contents plus the given Object.
096:             * @param array the array to append to (can be <code>null</code>)
097:             * @param obj the Object to append
098:             * @return the new array (of the same component type; never <code>null</code>)
099:             */
100:            public static Object[] addObjectToArray(Object[] array, Object obj) {
101:                Class compType = Object.class;
102:                if (array != null) {
103:                    compType = array.getClass().getComponentType();
104:                } else if (obj != null) {
105:                    compType = obj.getClass();
106:                }
107:                int newArrLength = (array != null ? array.length + 1 : 1);
108:                Object[] newArr = (Object[]) Array.newInstance(compType,
109:                        newArrLength);
110:                if (array != null) {
111:                    System.arraycopy(array, 0, newArr, 0, array.length);
112:                }
113:                newArr[newArr.length - 1] = obj;
114:                return newArr;
115:            }
116:
117:            /**
118:             * Convert the given array (which may be a primitive array) to an
119:             * object array (if necessary of primitive wrapper objects).
120:             * <p>A <code>null</code> source value will be converted to an
121:             * empty Object array.
122:             * @param source the (potentially primitive) array
123:             * @return the corresponding object array (never <code>null</code>)
124:             * @throws IllegalArgumentException if the parameter is not an array
125:             */
126:            public static Object[] toObjectArray(Object source) {
127:                if (source instanceof  Object[]) {
128:                    return (Object[]) source;
129:                }
130:                if (source == null) {
131:                    return new Object[0];
132:                }
133:                if (!source.getClass().isArray()) {
134:                    throw new IllegalArgumentException(
135:                            "Source is not an array: " + source);
136:                }
137:                int length = Array.getLength(source);
138:                if (length == 0) {
139:                    return new Object[0];
140:                }
141:                Class wrapperType = Array.get(source, 0).getClass();
142:                Object[] newArray = (Object[]) Array.newInstance(wrapperType,
143:                        length);
144:                for (int i = 0; i < length; i++) {
145:                    newArray[i] = Array.get(source, i);
146:                }
147:                return newArray;
148:            }
149:
150:            //---------------------------------------------------------------------
151:            // Convenience methods for content-based equality/hash-code handling
152:            //---------------------------------------------------------------------
153:
154:            /**
155:             * Determine if the given objects are equal, returning <code>true</code>
156:             * if both are <code>null</code> or <code>false</code> if only one is
157:             * <code>null</code>.
158:             * <p>Compares arrays with <code>Arrays.equals</code>, performing an equality
159:             * check based on the array elements rather than the array reference.
160:             * @param o1 first Object to compare
161:             * @param o2 second Object to compare
162:             * @return whether the given objects are equal
163:             * @see java.util.Arrays#equals
164:             */
165:            public static boolean nullSafeEquals(Object o1, Object o2) {
166:                if (o1 == o2) {
167:                    return true;
168:                }
169:                if (o1 == null || o2 == null) {
170:                    return false;
171:                }
172:                if (o1.equals(o2)) {
173:                    return true;
174:                }
175:                if (o1 instanceof  Object[] && o2 instanceof  Object[]) {
176:                    return Arrays.equals((Object[]) o1, (Object[]) o2);
177:                }
178:                if (o1 instanceof  boolean[] && o2 instanceof  boolean[]) {
179:                    return Arrays.equals((boolean[]) o1, (boolean[]) o2);
180:                }
181:                if (o1 instanceof  byte[] && o2 instanceof  byte[]) {
182:                    return Arrays.equals((byte[]) o1, (byte[]) o2);
183:                }
184:                if (o1 instanceof  char[] && o2 instanceof  char[]) {
185:                    return Arrays.equals((char[]) o1, (char[]) o2);
186:                }
187:                if (o1 instanceof  double[] && o2 instanceof  double[]) {
188:                    return Arrays.equals((double[]) o1, (double[]) o2);
189:                }
190:                if (o1 instanceof  float[] && o2 instanceof  float[]) {
191:                    return Arrays.equals((float[]) o1, (float[]) o2);
192:                }
193:                if (o1 instanceof  int[] && o2 instanceof  int[]) {
194:                    return Arrays.equals((int[]) o1, (int[]) o2);
195:                }
196:                if (o1 instanceof  long[] && o2 instanceof  long[]) {
197:                    return Arrays.equals((long[]) o1, (long[]) o2);
198:                }
199:                if (o1 instanceof  short[] && o2 instanceof  short[]) {
200:                    return Arrays.equals((short[]) o1, (short[]) o2);
201:                }
202:                return false;
203:            }
204:
205:            /**
206:             * Return as hash code for the given object; typically the value of
207:             * <code>{@link Object#hashCode()}</code>. If the object is an array,
208:             * this method will delegate to any of the <code>nullSafeHashCode</code>
209:             * methods for arrays in this class. If the object is <code>null</code>,
210:             * this method returns 0.
211:             * @see #nullSafeHashCode(Object[])
212:             * @see #nullSafeHashCode(boolean[])
213:             * @see #nullSafeHashCode(byte[])
214:             * @see #nullSafeHashCode(char[])
215:             * @see #nullSafeHashCode(double[])
216:             * @see #nullSafeHashCode(float[])
217:             * @see #nullSafeHashCode(int[])
218:             * @see #nullSafeHashCode(long[])
219:             * @see #nullSafeHashCode(short[])
220:             */
221:            public static int nullSafeHashCode(Object obj) {
222:                if (obj == null) {
223:                    return 0;
224:                }
225:                if (obj instanceof  Object[]) {
226:                    return nullSafeHashCode((Object[]) obj);
227:                }
228:                if (obj instanceof  boolean[]) {
229:                    return nullSafeHashCode((boolean[]) obj);
230:                }
231:                if (obj instanceof  byte[]) {
232:                    return nullSafeHashCode((byte[]) obj);
233:                }
234:                if (obj instanceof  char[]) {
235:                    return nullSafeHashCode((char[]) obj);
236:                }
237:                if (obj instanceof  double[]) {
238:                    return nullSafeHashCode((double[]) obj);
239:                }
240:                if (obj instanceof  float[]) {
241:                    return nullSafeHashCode((float[]) obj);
242:                }
243:                if (obj instanceof  int[]) {
244:                    return nullSafeHashCode((int[]) obj);
245:                }
246:                if (obj instanceof  long[]) {
247:                    return nullSafeHashCode((long[]) obj);
248:                }
249:                if (obj instanceof  short[]) {
250:                    return nullSafeHashCode((short[]) obj);
251:                }
252:                return obj.hashCode();
253:            }
254:
255:            /**
256:             * Return a hash code based on the contents of the specified array.
257:             * If <code>array</code> is <code>null</code>, this method returns 0.
258:             */
259:            public static int nullSafeHashCode(Object[] array) {
260:                if (array == null) {
261:                    return 0;
262:                }
263:                int hash = INITIAL_HASH;
264:                int arraySize = array.length;
265:                for (int i = 0; i < arraySize; i++) {
266:                    hash = MULTIPLIER * hash + nullSafeHashCode(array[i]);
267:                }
268:                return hash;
269:            }
270:
271:            /**
272:             * Return a hash code based on the contents of the specified array.
273:             * If <code>array</code> is <code>null</code>, this method returns 0.
274:             */
275:            public static int nullSafeHashCode(boolean[] array) {
276:                if (array == null) {
277:                    return 0;
278:                }
279:                int hash = INITIAL_HASH;
280:                int arraySize = array.length;
281:                for (int i = 0; i < arraySize; i++) {
282:                    hash = MULTIPLIER * hash + hashCode(array[i]);
283:                }
284:                return hash;
285:            }
286:
287:            /**
288:             * Return a hash code based on the contents of the specified array.
289:             * If <code>array</code> is <code>null</code>, this method returns 0.
290:             */
291:            public static int nullSafeHashCode(byte[] array) {
292:                if (array == null) {
293:                    return 0;
294:                }
295:                int hash = INITIAL_HASH;
296:                int arraySize = array.length;
297:                for (int i = 0; i < arraySize; i++) {
298:                    hash = MULTIPLIER * hash + array[i];
299:                }
300:                return hash;
301:            }
302:
303:            /**
304:             * Return a hash code based on the contents of the specified array.
305:             * If <code>array</code> is <code>null</code>, this method returns 0.
306:             */
307:            public static int nullSafeHashCode(char[] array) {
308:                if (array == null) {
309:                    return 0;
310:                }
311:                int hash = INITIAL_HASH;
312:                int arraySize = array.length;
313:                for (int i = 0; i < arraySize; i++) {
314:                    hash = MULTIPLIER * hash + array[i];
315:                }
316:                return hash;
317:            }
318:
319:            /**
320:             * Return a hash code based on the contents of the specified array.
321:             * If <code>array</code> is <code>null</code>, this method returns 0.
322:             */
323:            public static int nullSafeHashCode(double[] array) {
324:                if (array == null) {
325:                    return 0;
326:                }
327:                int hash = INITIAL_HASH;
328:                int arraySize = array.length;
329:                for (int i = 0; i < arraySize; i++) {
330:                    hash = MULTIPLIER * hash + hashCode(array[i]);
331:                }
332:                return hash;
333:            }
334:
335:            /**
336:             * Return a hash code based on the contents of the specified array.
337:             * If <code>array</code> is <code>null</code>, this method returns 0.
338:             */
339:            public static int nullSafeHashCode(float[] array) {
340:                if (array == null) {
341:                    return 0;
342:                }
343:                int hash = INITIAL_HASH;
344:                int arraySize = array.length;
345:                for (int i = 0; i < arraySize; i++) {
346:                    hash = MULTIPLIER * hash + hashCode(array[i]);
347:                }
348:                return hash;
349:            }
350:
351:            /**
352:             * Return a hash code based on the contents of the specified array.
353:             * If <code>array</code> is <code>null</code>, this method returns 0.
354:             */
355:            public static int nullSafeHashCode(int[] array) {
356:                if (array == null) {
357:                    return 0;
358:                }
359:                int hash = INITIAL_HASH;
360:                int arraySize = array.length;
361:                for (int i = 0; i < arraySize; i++) {
362:                    hash = MULTIPLIER * hash + array[i];
363:                }
364:                return hash;
365:            }
366:
367:            /**
368:             * Return a hash code based on the contents of the specified array.
369:             * If <code>array</code> is <code>null</code>, this method returns 0.
370:             */
371:            public static int nullSafeHashCode(long[] array) {
372:                if (array == null) {
373:                    return 0;
374:                }
375:                int hash = INITIAL_HASH;
376:                int arraySize = array.length;
377:                for (int i = 0; i < arraySize; i++) {
378:                    hash = MULTIPLIER * hash + hashCode(array[i]);
379:                }
380:                return hash;
381:            }
382:
383:            /**
384:             * Return a hash code based on the contents of the specified array.
385:             * If <code>array</code> is <code>null</code>, this method returns 0.
386:             */
387:            public static int nullSafeHashCode(short[] array) {
388:                if (array == null) {
389:                    return 0;
390:                }
391:                int hash = INITIAL_HASH;
392:                int arraySize = array.length;
393:                for (int i = 0; i < arraySize; i++) {
394:                    hash = MULTIPLIER * hash + array[i];
395:                }
396:                return hash;
397:            }
398:
399:            /**
400:             * Return the same value as <code>{@link Boolean#hashCode()}</code>.
401:             * @see Boolean#hashCode()
402:             */
403:            public static int hashCode(boolean bool) {
404:                return bool ? 1231 : 1237;
405:            }
406:
407:            /**
408:             * Return the same value as <code>{@link Double#hashCode()}</code>.
409:             * @see Double#hashCode()
410:             */
411:            public static int hashCode(double dbl) {
412:                long bits = Double.doubleToLongBits(dbl);
413:                return hashCode(bits);
414:            }
415:
416:            /**
417:             * Return the same value as <code>{@link Float#hashCode()}</code>.
418:             * @see Float#hashCode()
419:             */
420:            public static int hashCode(float flt) {
421:                return Float.floatToIntBits(flt);
422:            }
423:
424:            /**
425:             * Return the same value as <code>{@link Long#hashCode()}</code>.
426:             * @see Long#hashCode()
427:             */
428:            public static int hashCode(long lng) {
429:                return (int) (lng ^ (lng >>> 32));
430:            }
431:
432:            //---------------------------------------------------------------------
433:            // Convenience methods for toString output
434:            //---------------------------------------------------------------------
435:
436:            /**
437:             * Return a String representation of an object's overall identity.
438:             * @param obj the object (may be <code>null</code>)
439:             * @return the object's identity as String representation,
440:             * or an empty String if the object was <code>null</code>
441:             */
442:            public static String identityToString(Object obj) {
443:                if (obj == null) {
444:                    return EMPTY_STRING;
445:                }
446:                return obj.getClass().getName() + "@"
447:                        + getIdentityHexString(obj);
448:            }
449:
450:            /**
451:             * Return a hex String form of an object's identity hash code.
452:             * @param obj the object
453:             * @return the object's identity code in hex notation
454:             */
455:            public static String getIdentityHexString(Object obj) {
456:                return Integer.toHexString(System.identityHashCode(obj));
457:            }
458:
459:            /**
460:             * Return a content-based String representation if <code>obj</code> is
461:             * not <code>null</code>; otherwise returns an empty String.
462:             * <p>Differs from {@link #nullSafeToString(Object)} in that it returns
463:             * an empty String rather than "null" for a <code>null</code> value.
464:             * @param obj the object to build a display String for
465:             * @return a display String representation of <code>obj</code>
466:             * @see #nullSafeToString(Object)
467:             */
468:            public static String getDisplayString(Object obj) {
469:                if (obj == null) {
470:                    return EMPTY_STRING;
471:                }
472:                return nullSafeToString(obj);
473:            }
474:
475:            /**
476:             * Determine the class name for the given object.
477:             * <p>Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
478:             * @param obj the object to introspect (may be <code>null</code>)
479:             * @return the corresponding class name
480:             */
481:            public static String nullSafeClassName(Object obj) {
482:                return (obj != null ? obj.getClass().getName() : NULL_STRING);
483:            }
484:
485:            /**
486:             * Return a String representation of the specified Object.
487:             * <p>Builds a String representation of the contents in case of an array.
488:             * Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
489:             * @param obj the object to build a String representation for
490:             * @return a String representation of <code>obj</code>
491:             */
492:            public static String nullSafeToString(Object obj) {
493:                if (obj == null) {
494:                    return NULL_STRING;
495:                }
496:                if (obj instanceof  String) {
497:                    return (String) obj;
498:                }
499:                if (obj instanceof  Object[]) {
500:                    return nullSafeToString((Object[]) obj);
501:                }
502:                if (obj instanceof  boolean[]) {
503:                    return nullSafeToString((boolean[]) obj);
504:                }
505:                if (obj instanceof  byte[]) {
506:                    return nullSafeToString((byte[]) obj);
507:                }
508:                if (obj instanceof  char[]) {
509:                    return nullSafeToString((char[]) obj);
510:                }
511:                if (obj instanceof  double[]) {
512:                    return nullSafeToString((double[]) obj);
513:                }
514:                if (obj instanceof  float[]) {
515:                    return nullSafeToString((float[]) obj);
516:                }
517:                if (obj instanceof  int[]) {
518:                    return nullSafeToString((int[]) obj);
519:                }
520:                if (obj instanceof  long[]) {
521:                    return nullSafeToString((long[]) obj);
522:                }
523:                if (obj instanceof  short[]) {
524:                    return nullSafeToString((short[]) obj);
525:                }
526:                String str = obj.toString();
527:                return (str != null ? str : EMPTY_STRING);
528:            }
529:
530:            /**
531:             * Return a String representation of the contents of the specified array.
532:             * <p>The String representation consists of a list of the array's elements,
533:             * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
534:             * by the characters <code>", "</code> (a comma followed by a space). Returns
535:             * <code>"null"</code> if <code>array</code> is <code>null</code>.
536:             * @param array the array to build a String representation for
537:             * @return a String representation of <code>array</code>
538:             */
539:            public static String nullSafeToString(Object[] array) {
540:                if (array == null) {
541:                    return NULL_STRING;
542:                }
543:                int length = array.length;
544:                if (length == 0) {
545:                    return EMPTY_ARRAY;
546:                }
547:                StringBuffer buffer = new StringBuffer();
548:                for (int i = 0; i < length; i++) {
549:                    if (i == 0) {
550:                        buffer.append(ARRAY_START);
551:                    } else {
552:                        buffer.append(ARRAY_ELEMENT_SEPARATOR);
553:                    }
554:                    buffer.append(String.valueOf(array[i]));
555:                }
556:                buffer.append(ARRAY_END);
557:                return buffer.toString();
558:            }
559:
560:            /**
561:             * Return a String representation of the contents of the specified array.
562:             * <p>The String representation consists of a list of the array's elements,
563:             * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
564:             * by the characters <code>", "</code> (a comma followed by a space). Returns
565:             * <code>"null"</code> if <code>array</code> is <code>null</code>.
566:             * @param array the array to build a String representation for
567:             * @return a String representation of <code>array</code>
568:             */
569:            public static String nullSafeToString(boolean[] array) {
570:                if (array == null) {
571:                    return NULL_STRING;
572:                }
573:                int length = array.length;
574:                if (length == 0) {
575:                    return EMPTY_ARRAY;
576:                }
577:                StringBuffer buffer = new StringBuffer();
578:                for (int i = 0; i < length; i++) {
579:                    if (i == 0) {
580:                        buffer.append(ARRAY_START);
581:                    } else {
582:                        buffer.append(ARRAY_ELEMENT_SEPARATOR);
583:                    }
584:
585:                    buffer.append(array[i]);
586:                }
587:                buffer.append(ARRAY_END);
588:                return buffer.toString();
589:            }
590:
591:            /**
592:             * Return a String representation of the contents of the specified array.
593:             * <p>The String representation consists of a list of the array's elements,
594:             * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
595:             * by the characters <code>", "</code> (a comma followed by a space). Returns
596:             * <code>"null"</code> if <code>array</code> is <code>null</code>.
597:             * @param array the array to build a String representation for
598:             * @return a String representation of <code>array</code>
599:             */
600:            public static String nullSafeToString(byte[] array) {
601:                if (array == null) {
602:                    return NULL_STRING;
603:                }
604:                int length = array.length;
605:                if (length == 0) {
606:                    return EMPTY_ARRAY;
607:                }
608:                StringBuffer buffer = new StringBuffer();
609:                for (int i = 0; i < length; i++) {
610:                    if (i == 0) {
611:                        buffer.append(ARRAY_START);
612:                    } else {
613:                        buffer.append(ARRAY_ELEMENT_SEPARATOR);
614:                    }
615:                    buffer.append(array[i]);
616:                }
617:                buffer.append(ARRAY_END);
618:                return buffer.toString();
619:            }
620:
621:            /**
622:             * Return a String representation of the contents of the specified array.
623:             * <p>The String representation consists of a list of the array's elements,
624:             * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
625:             * by the characters <code>", "</code> (a comma followed by a space). Returns
626:             * <code>"null"</code> if <code>array</code> is <code>null</code>.
627:             * @param array the array to build a String representation for
628:             * @return a String representation of <code>array</code>
629:             */
630:            public static String nullSafeToString(char[] array) {
631:                if (array == null) {
632:                    return NULL_STRING;
633:                }
634:                int length = array.length;
635:                if (length == 0) {
636:                    return EMPTY_ARRAY;
637:                }
638:                StringBuffer buffer = new StringBuffer();
639:                for (int i = 0; i < length; i++) {
640:                    if (i == 0) {
641:                        buffer.append(ARRAY_START);
642:                    } else {
643:                        buffer.append(ARRAY_ELEMENT_SEPARATOR);
644:                    }
645:                    buffer.append("'").append(array[i]).append("'");
646:                }
647:                buffer.append(ARRAY_END);
648:                return buffer.toString();
649:            }
650:
651:            /**
652:             * Return a String representation of the contents of the specified array.
653:             * <p>The String representation consists of a list of the array's elements,
654:             * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
655:             * by the characters <code>", "</code> (a comma followed by a space). Returns
656:             * <code>"null"</code> if <code>array</code> is <code>null</code>.
657:             * @param array the array to build a String representation for
658:             * @return a String representation of <code>array</code>
659:             */
660:            public static String nullSafeToString(double[] array) {
661:                if (array == null) {
662:                    return NULL_STRING;
663:                }
664:                int length = array.length;
665:                if (length == 0) {
666:                    return EMPTY_ARRAY;
667:                }
668:                StringBuffer buffer = new StringBuffer();
669:                for (int i = 0; i < length; i++) {
670:                    if (i == 0) {
671:                        buffer.append(ARRAY_START);
672:                    } else {
673:                        buffer.append(ARRAY_ELEMENT_SEPARATOR);
674:                    }
675:
676:                    buffer.append(array[i]);
677:                }
678:                buffer.append(ARRAY_END);
679:                return buffer.toString();
680:            }
681:
682:            /**
683:             * Return a String representation of the contents of the specified array.
684:             * <p>The String representation consists of a list of the array's elements,
685:             * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
686:             * by the characters <code>", "</code> (a comma followed by a space). Returns
687:             * <code>"null"</code> if <code>array</code> is <code>null</code>.
688:             * @param array the array to build a String representation for
689:             * @return a String representation of <code>array</code>
690:             */
691:            public static String nullSafeToString(float[] array) {
692:                if (array == null) {
693:                    return NULL_STRING;
694:                }
695:                int length = array.length;
696:                if (length == 0) {
697:                    return EMPTY_ARRAY;
698:                }
699:                StringBuffer buffer = new StringBuffer();
700:                for (int i = 0; i < length; i++) {
701:                    if (i == 0) {
702:                        buffer.append(ARRAY_START);
703:                    } else {
704:                        buffer.append(ARRAY_ELEMENT_SEPARATOR);
705:                    }
706:
707:                    buffer.append(array[i]);
708:                }
709:                buffer.append(ARRAY_END);
710:                return buffer.toString();
711:            }
712:
713:            /**
714:             * Return a String representation of the contents of the specified array.
715:             * <p>The String representation consists of a list of the array's elements,
716:             * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
717:             * by the characters <code>", "</code> (a comma followed by a space). Returns
718:             * <code>"null"</code> if <code>array</code> is <code>null</code>.
719:             * @param array the array to build a String representation for
720:             * @return a String representation of <code>array</code>
721:             */
722:            public static String nullSafeToString(int[] array) {
723:                if (array == null) {
724:                    return NULL_STRING;
725:                }
726:                int length = array.length;
727:                if (length == 0) {
728:                    return EMPTY_ARRAY;
729:                }
730:                StringBuffer buffer = new StringBuffer();
731:                for (int i = 0; i < length; i++) {
732:                    if (i == 0) {
733:                        buffer.append(ARRAY_START);
734:                    } else {
735:                        buffer.append(ARRAY_ELEMENT_SEPARATOR);
736:                    }
737:                    buffer.append(array[i]);
738:                }
739:                buffer.append(ARRAY_END);
740:                return buffer.toString();
741:            }
742:
743:            /**
744:             * Return a String representation of the contents of the specified array.
745:             * <p>The String representation consists of a list of the array's elements,
746:             * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
747:             * by the characters <code>", "</code> (a comma followed by a space). Returns
748:             * <code>"null"</code> if <code>array</code> is <code>null</code>.
749:             * @param array the array to build a String representation for
750:             * @return a String representation of <code>array</code>
751:             */
752:            public static String nullSafeToString(long[] array) {
753:                if (array == null) {
754:                    return NULL_STRING;
755:                }
756:                int length = array.length;
757:                if (length == 0) {
758:                    return EMPTY_ARRAY;
759:                }
760:                StringBuffer buffer = new StringBuffer();
761:                for (int i = 0; i < length; i++) {
762:                    if (i == 0) {
763:                        buffer.append(ARRAY_START);
764:                    } else {
765:                        buffer.append(ARRAY_ELEMENT_SEPARATOR);
766:                    }
767:                    buffer.append(array[i]);
768:                }
769:                buffer.append(ARRAY_END);
770:                return buffer.toString();
771:            }
772:
773:            /**
774:             * Return a String representation of the contents of the specified array.
775:             * <p>The String representation consists of a list of the array's elements,
776:             * enclosed in curly braces (<code>"{}"</code>). Adjacent elements are separated
777:             * by the characters <code>", "</code> (a comma followed by a space). Returns
778:             * <code>"null"</code> if <code>array</code> is <code>null</code>.
779:             * @param array the array to build a String representation for
780:             * @return a String representation of <code>array</code>
781:             */
782:            public static String nullSafeToString(short[] array) {
783:                if (array == null) {
784:                    return NULL_STRING;
785:                }
786:                int length = array.length;
787:                if (length == 0) {
788:                    return EMPTY_ARRAY;
789:                }
790:                StringBuffer buffer = new StringBuffer();
791:                for (int i = 0; i < length; i++) {
792:                    if (i == 0) {
793:                        buffer.append(ARRAY_START);
794:                    } else {
795:                        buffer.append(ARRAY_ELEMENT_SEPARATOR);
796:                    }
797:                    buffer.append(array[i]);
798:                }
799:                buffer.append(ARRAY_END);
800:                return buffer.toString();
801:            }
802:
803:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.