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: }
|