Source Code Cross Referenced for ObjectUtils.java in  » ESB » mule » org » mule » 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 » ESB » mule » org.mule.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: ObjectUtils.java 9865 2007-11-26 12:52:41Z rossmason $
003:         * --------------------------------------------------------------------------------------
004:         * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
005:         *
006:         * The software in this package is published under the terms of the CPAL v1.0
007:         * license, a copy of which has been included with this distribution in the
008:         * LICENSE.txt file.
009:         */
010:
011:        package org.mule.util;
012:
013:        import org.apache.commons.logging.Log;
014:        import org.apache.commons.logging.LogFactory;
015:
016:        public class ObjectUtils extends org.apache.commons.lang.ObjectUtils {
017:
018:            /** logger used by this class */
019:            protected static final Log logger = LogFactory
020:                    .getLog(ObjectUtils.class);
021:
022:            /**
023:             * Like {@link #identityToString(Object)} but without the object's full package
024:             * name.
025:             *
026:             * @param obj the object for which the identity description is to be generated
027:             * @return the object's identity description in the form of
028:             *         "ClassName@IdentityCode" or "null" if the argument was null.
029:             */
030:            public static String identityToShortString(Object obj) {
031:                if (obj == null) {
032:                    return "null";
033:                } else {
034:                    return new StringBuffer(40).append(
035:                            ClassUtils.getSimpleName(obj.getClass())).append(
036:                            '@').append(
037:                            Integer.toHexString(System.identityHashCode(obj)))
038:                            .toString();
039:                }
040:            }
041:
042:            /**
043:             * Gets a String from a value in a null-safe manner.
044:             * <p/>
045:             *
046:             * @param answer       the object value
047:             * @param defaultValue the default to use if null or of incorrect type
048:             * @return the value as a String, or the defaultValue
049:             */
050:            public static String getString(final Object answer,
051:                    String defaultValue) {
052:                if (answer != null) {
053:                    return answer.toString();
054:                } else {
055:                    return defaultValue;
056:                }
057:            }
058:
059:            /**
060:             * Gets a boolean from a value in a null-safe manner.
061:             * <p/>
062:             *
063:             * @param answer       the object value
064:             * @param defaultValue the default to use if null or of incorrect type
065:             * @return the value as a boolean, or the defaultValue
066:             */
067:            public static boolean getBoolean(final Object answer,
068:                    boolean defaultValue) {
069:                if (answer != null) {
070:                    if (answer instanceof  Boolean) {
071:                        return ((Boolean) answer).booleanValue();
072:
073:                    } else if (answer instanceof  String) {
074:                        return Boolean.valueOf((String) answer).booleanValue();
075:
076:                    } else if (answer instanceof  Number) {
077:                        Number n = (Number) answer;
078:                        return ((n.intValue() > 0) ? Boolean.TRUE
079:                                : Boolean.FALSE).booleanValue();
080:                    } else {
081:                        if (logger.isWarnEnabled()) {
082:                            logger
083:                                    .warn("Value exists but cannot be converted to boolean: "
084:                                            + answer
085:                                            + ", returning default value: "
086:                                            + defaultValue);
087:                        }
088:                        return defaultValue;
089:                    }
090:                }
091:                return defaultValue;
092:            }
093:
094:            /**
095:             * Gets a byte from a value in a null-safe manner.
096:             * <p/>
097:             *
098:             * @param answer       the object value
099:             * @param defaultValue the default to use if null or of incorrect type
100:             * @return the value as a byte, or the defaultValue
101:             */
102:            public static byte getByte(final Object answer, byte defaultValue) {
103:                if (answer == null) {
104:                    return defaultValue;
105:                } else if (answer instanceof  Number) {
106:                    return ((Number) answer).byteValue();
107:                } else if (answer instanceof  String) {
108:                    try {
109:                        return Byte.valueOf((String) answer).byteValue();
110:                    } catch (NumberFormatException e) {
111:                        //handled below
112:                    }
113:                }
114:                if (logger.isWarnEnabled()) {
115:                    logger
116:                            .warn("Value exists but cannot be converted to byte: "
117:                                    + answer
118:                                    + ", returning default value: "
119:                                    + defaultValue);
120:                }
121:                return defaultValue;
122:            }
123:
124:            /**
125:             * Gets a short from a value in a null-safe manner.
126:             * <p/>
127:             *
128:             * @param answer       the object value
129:             * @param defaultValue the default to use if null or of incorrect type
130:             * @return the value as a short, or the defaultValue
131:             */
132:            public static short getShort(final Object answer, short defaultValue) {
133:                if (answer == null) {
134:                    return defaultValue;
135:                } else if (answer instanceof  Number) {
136:                    return ((Number) answer).shortValue();
137:                } else if (answer instanceof  String) {
138:                    try {
139:                        return Short.valueOf((String) answer).shortValue();
140:                    } catch (NumberFormatException e) {
141:                        //handled below
142:                    }
143:                }
144:                if (logger.isWarnEnabled()) {
145:                    logger
146:                            .warn("Value exists but cannot be converted to short: "
147:                                    + answer
148:                                    + ", returning default value: "
149:                                    + defaultValue);
150:                }
151:                return defaultValue;
152:            }
153:
154:            /**
155:             * Gets a int from a value in a null-safe manner.
156:             * <p/>
157:             *
158:             * @param answer       the object value
159:             * @param defaultValue the default to use if null or of incorrect type
160:             * @return the value as a int, or the defaultValue
161:             */
162:            public static int getInt(final Object answer, int defaultValue) {
163:                if (answer == null) {
164:                    return defaultValue;
165:                } else if (answer instanceof  Number) {
166:                    return ((Number) answer).intValue();
167:                } else if (answer instanceof  String) {
168:                    try {
169:                        return Integer.valueOf((String) answer).intValue();
170:                    } catch (NumberFormatException e) {
171:                        //handled below
172:                    }
173:                }
174:                if (logger.isWarnEnabled()) {
175:                    logger.warn("Value exists but cannot be converted to int: "
176:                            + answer + ", returning default value: "
177:                            + defaultValue);
178:                }
179:                return defaultValue;
180:            }
181:
182:            /**
183:             * Gets a long from a value in a null-safe manner.
184:             * <p/>
185:             *
186:             * @param answer       the object value
187:             * @param defaultValue the default to use if null or of incorrect type
188:             * @return the value as a long, or the defaultValue
189:             */
190:            public static long getLong(final Object answer, long defaultValue) {
191:                if (answer == null) {
192:                    return defaultValue;
193:                } else if (answer instanceof  Number) {
194:                    return ((Number) answer).longValue();
195:                } else if (answer instanceof  String) {
196:                    try {
197:                        return Long.valueOf((String) answer).longValue();
198:                    } catch (NumberFormatException e) {
199:                        //handled below
200:
201:                    }
202:                }
203:                if (logger.isWarnEnabled()) {
204:                    logger
205:                            .warn("Value exists but cannot be converted to long: "
206:                                    + answer
207:                                    + ", returning default value: "
208:                                    + defaultValue);
209:                }
210:                return defaultValue;
211:            }
212:
213:            /**
214:             * Gets a float from a value in a null-safe manner.
215:             * <p/>
216:             *
217:             * @param answer       the object value
218:             * @param defaultValue the default to use if null or of incorrect type
219:             * @return the value as a float, or the defaultValue
220:             */
221:            public static float getFloat(final Object answer, float defaultValue) {
222:                if (answer == null) {
223:                    return defaultValue;
224:                } else if (answer instanceof  Number) {
225:                    return ((Number) answer).floatValue();
226:                } else if (answer instanceof  String) {
227:                    try {
228:                        return Float.valueOf((String) answer).floatValue();
229:                    } catch (NumberFormatException e) {
230:                        //handled below
231:
232:                    }
233:                }
234:                if (logger.isWarnEnabled()) {
235:                    logger
236:                            .warn("Value exists but cannot be converted to float: "
237:                                    + answer
238:                                    + ", returning default value: "
239:                                    + defaultValue);
240:                }
241:                return defaultValue;
242:            }
243:
244:            /**
245:             * Gets a double from a value in a null-safe manner.
246:             * <p/>
247:             *
248:             * @param answer       the object value
249:             * @param defaultValue the default to use if null or of incorrect type
250:             * @return the value as a double, or the defaultValue
251:             */
252:            public static double getDouble(final Object answer,
253:                    double defaultValue) {
254:                if (answer == null) {
255:                    return defaultValue;
256:                } else if (answer instanceof  Number) {
257:                    return ((Number) answer).doubleValue();
258:                } else if (answer instanceof  String) {
259:                    try {
260:                        return Double.valueOf((String) answer).doubleValue();
261:                    } catch (NumberFormatException e) {
262:                        //handled below
263:                    }
264:                }
265:                if (logger.isWarnEnabled()) {
266:                    logger
267:                            .warn("Value exists but cannot be converted to double: "
268:                                    + answer
269:                                    + ", returning default value: "
270:                                    + defaultValue);
271:                }
272:                return defaultValue;
273:            }
274:
275:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.