1 16 package org.apache.commons.lang; 17 18 import java.io.Serializable ; 19 20 36 public class ObjectUtils { 37 38 54 public static final Null NULL = new Null(); 55 56 64 public ObjectUtils() { 65 } 66 67 85 public static Object defaultIfNull(Object object, Object defaultValue) { 86 return object != null ? object : defaultValue; 87 } 88 89 108 public static boolean equals(Object object1, Object object2) { 109 if (object1 == object2) { 110 return true; 111 } 112 if ((object1 == null) || (object2 == null)) { 113 return false; 114 } 115 return object1.equals(object2); 116 } 117 118 131 public static int hashCode(Object obj) { 132 return (obj == null) ? 0 : obj.hashCode(); 133 } 134 135 153 public static String identityToString(Object object) { 154 if (object == null) { 155 return null; 156 } 157 return appendIdentityToString(null, object).toString(); 158 } 159 160 178 public static StringBuffer appendIdentityToString(StringBuffer buffer, Object object) { 179 if (object == null) { 180 return null; 181 } 182 if (buffer == null) { 183 buffer = new StringBuffer (); 184 } 185 return buffer 186 .append(object.getClass().getName()) 187 .append('@') 188 .append(Integer.toHexString(System.identityHashCode(object))); 189 } 190 191 210 public static String toString(Object obj) { 211 return obj == null ? "" : obj.toString(); 212 } 213 214 233 public static String toString(Object obj, String nullStr) { 234 return obj == null ? nullStr : obj.toString(); 235 } 236 237 253 public static class Null implements Serializable { 254 private static final long serialVersionUID = 7092611880189329093L; 256 257 260 Null() { 261 } 262 263 268 private Object readResolve() { 269 return ObjectUtils.NULL; 270 } 271 } 272 273 } 274 | Popular Tags |