1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowledgement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgement may appear in the software itself,
  24. * if and wherever such third-party acknowledgements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.commons.lang;
  55. import java.io.Serializable;
  56. /**
  57. * <p>Operations on <code>Object</code>.</p>
  58. *
  59. * <p>This class tries to handle <code>null</code> input gracefully.
  60. * An exception will generally not be thrown for a <code>null</code> input.
  61. * Each method documents its behaviour in more detail.</p>
  62. *
  63. * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
  64. * @author <a href="mailto:janekdb@yahoo.co.uk">Janek Bogucki</a>
  65. * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
  66. * @author Stephen Colebourne
  67. * @author Gary Gregory
  68. * @since 1.0
  69. * @version $Id: ObjectUtils.java,v 1.21 2003/08/22 17:25:33 ggregory Exp $
  70. */
  71. public class ObjectUtils {
  72. /**
  73. * <p>Singleton used as a <code>null</code> placeholder where
  74. * <code>null</code> has another meaning.</p>
  75. *
  76. * <p>For example, in a <code>HashMap</code> the
  77. * {@link java.util.HashMap#get(java.lang.Object)} method returns
  78. * <code>null</code> if the <code>Map</code> contains
  79. * <code>null</code> or if there is no matching key. The
  80. * <code>Null</code> placeholder can be used to distinguish between
  81. * these two cases.</p>
  82. *
  83. * <p>Another example is <code>Hashtable</code>, where <code>null</code>
  84. * cannot be stored.</p>
  85. *
  86. * <p>This instance is Serializable.</p>
  87. */
  88. public static final Null NULL = new Null();
  89. /**
  90. * <p><code>ObjectUtils</code> instances should NOT be constructed in
  91. * standard programming. Instead, the class should be used as
  92. * <code>ObjectUtils.defaultIfNull("a","b");</code>.</p>
  93. *
  94. * <p>This constructor is public to permit tools that require a JavaBean instance
  95. * to operate.</p>
  96. */
  97. public ObjectUtils() {
  98. }
  99. // Defaulting
  100. //-----------------------------------------------------------------------
  101. /**
  102. * <p>Returns a default value if the object passed is
  103. * <code>null</code>.</p>
  104. *
  105. * <pre>
  106. * ObjectUtils.defaultIfNull(null, null) = null
  107. * ObjectUtils.defaultIfNull(null, "") = ""
  108. * ObjectUtils.defaultIfNull(null, "zz") = "zz"
  109. * ObjectUtils.defaultIfNull("abc", *) = "abc"
  110. * ObjectUtils.defaultIfNull(Boolean.TRUE, *) = Boolean.TRUE
  111. * </pre>
  112. *
  113. * @param object the <code>Object</code> to test, may be <code>null</code>
  114. * @param defaultValue the default value to return, may be <code>null</code>
  115. * @return <code>object</code> if it is not <code>null</code>, defaultValue otherwise
  116. */
  117. public static Object defaultIfNull(Object object, Object defaultValue) {
  118. return (object != null ? object : defaultValue);
  119. }
  120. /**
  121. * <p>Compares two objects for equality, where either one or both
  122. * objects may be <code>null</code>.</p>
  123. *
  124. * <pre>
  125. * ObjectUtils.equals(null, null) = true
  126. * ObjectUtils.equals(null, "") = false
  127. * ObjectUtils.equals("", null) = false
  128. * ObjectUtils.equals("", "") = true
  129. * ObjectUtils.equals(Boolean.TRUE, null) = false
  130. * ObjectUtils.equals(Boolean.TRUE, "true") = false
  131. * ObjectUtils.equals(Boolean.TRUE, Boolean.TRUE) = true
  132. * ObjectUtils.equals(Boolean.TRUE, Boolean.FALSE) = false
  133. * </pre>
  134. *
  135. * @param object1 the first object, may be <code>null</code>
  136. * @param object2 the second object, may be <code>null</code>
  137. * @return <code>true</code> if the values of both objects are the same
  138. */
  139. public static boolean equals(Object object1, Object object2) {
  140. if (object1 == object2) {
  141. return true;
  142. }
  143. if ((object1 == null) || (object2 == null)) {
  144. return false;
  145. }
  146. return object1.equals(object2);
  147. }
  148. // Identity ToString
  149. //-----------------------------------------------------------------------
  150. /**
  151. * <p>Gets the toString that would be produced by <code>Object</code>
  152. * if a class did not override toString itself. <code>null</code>
  153. * will return <code>null</code>.</p>
  154. *
  155. * <pre>
  156. * ObjectUtils.identityToString(null) = null
  157. * ObjectUtils.identityToString("") = "java.lang.String@1e23"
  158. * ObjectUtils.identityToString(Boolean.TRUE) = "java.lang.Boolean@7fa"
  159. * </pre>
  160. *
  161. * @param object the object to create a toString for, may be
  162. * <code>null</code>
  163. * @return the default toString text, or <code>null</code> if
  164. * <code>null</code> passed in
  165. */
  166. public static String identityToString(Object object) {
  167. if (object == null) {
  168. return null;
  169. }
  170. return appendIdentityToString(null, object).toString();
  171. }
  172. /**
  173. * <p>Appends the toString that would be produced by <code>Object</code>
  174. * if a class did not override toString itself. <code>null</code>
  175. * will return <code>null</code>.</p>
  176. *
  177. * <pre>
  178. * ObjectUtils.appendIdentityToString(*, null) = null
  179. * ObjectUtils.appendIdentityToString(null, "") = "java.lang.String@1e23"
  180. * ObjectUtils.appendIdentityToString(null, Boolean.TRUE) = "java.lang.Boolean@7fa"
  181. * ObjectUtils.appendIdentityToString(buf, Boolean.TRUE) = buf.append("java.lang.Boolean@7fa")
  182. * </pre>
  183. *
  184. * @param buffer the buffer to append to, may be <code>null</code>
  185. * @param object the object to create a toString for, may be <code>null</code>
  186. * @return the default toString text, or <code>null</code> if
  187. * <code>null</code> passed in
  188. * @since 2.0
  189. */
  190. public static StringBuffer appendIdentityToString(StringBuffer buffer, Object object) {
  191. if (object == null) {
  192. return null;
  193. }
  194. if (buffer == null) {
  195. buffer = new StringBuffer();
  196. }
  197. return buffer
  198. .append(object.getClass().getName())
  199. .append('@')
  200. .append(Integer.toHexString(System.identityHashCode(object)));
  201. }
  202. // ToString
  203. //-----------------------------------------------------------------------
  204. /**
  205. * <p>Gets the <code>toString</code> of an <code>Object</code> returning
  206. * an empty string ("") if <code>null</code> input.</p>
  207. *
  208. * <pre>
  209. * ObjectUtils.toString(null) = ""
  210. * ObjectUtils.toString("") = ""
  211. * ObjectUtils.toString("bat") = "bat"
  212. * ObjectUtils.toString(Boolean.TRUE) = "true"
  213. * </pre>
  214. *
  215. * @see StringUtils#defaultString(String)
  216. * @see String#valueOf(Object)
  217. * @param obj the Object to <code>toString</code>, may be null
  218. * @return the passed in Object's toString, or nullStr if <code>null</code> input
  219. * @since 2.0
  220. */
  221. public static String toString(Object obj) {
  222. return (obj == null ? "" : obj.toString());
  223. }
  224. /**
  225. * <p>Gets the <code>toString</code> of an <code>Object</code> returning
  226. * a specified text if <code>null</code> input.</p>
  227. *
  228. * <pre>
  229. * ObjectUtils.toString(null, null) = null
  230. * ObjectUtils.toString(null, "null") = "null"
  231. * ObjectUtils.toString("", "null") = ""
  232. * ObjectUtils.toString("bat", "null") = "bat"
  233. * ObjectUtils.toString(Boolean.TRUE, "null") = "true"
  234. * </pre>
  235. *
  236. * @see StringUtils#defaultString(String,String)
  237. * @see String#valueOf(Object)
  238. * @param obj the Object to <code>toString</code>, may be null
  239. * @param nullStr the String to return if <code>null</code> input, may be null
  240. * @return the passed in Object's toString, or nullStr if <code>null</code> input
  241. * @since 2.0
  242. */
  243. public static String toString(Object obj, String nullStr) {
  244. return (obj == null ? nullStr : obj.toString());
  245. }
  246. // Null
  247. //-----------------------------------------------------------------------
  248. /**
  249. * <p>Class used as a null placeholder where <code>null</code>
  250. * has another meaning.</p>
  251. *
  252. * <p>For example, in a <code>HashMap</code> the
  253. * {@link java.util.HashMap#get(java.lang.Object)} method returns
  254. * <code>null</code> if the <code>Map</code> contains
  255. * <code>null</code> or if there is no matching key. The
  256. * <code>Null</code> placeholder can be used to distinguish between
  257. * these two cases.</p>
  258. *
  259. * <p>Another example is <code>Hashtable</code>, where <code>null</code>
  260. * cannot be stored.</p>
  261. */
  262. public static class Null implements Serializable {
  263. // declare serialization compatability with Commons Lang 1.0
  264. private static final long serialVersionUID = 7092611880189329093L;
  265. /**
  266. * Restricted constructor - singleton.
  267. */
  268. Null() {
  269. }
  270. /**
  271. * <p>Ensure singleton.</p>
  272. *
  273. * @return the singleton value
  274. */
  275. private Object readResolve() {
  276. return ObjectUtils.NULL;
  277. }
  278. }
  279. }