1 22 package org.jboss.util; 23 24 import java.lang.ref.SoftReference ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 28 import org.jboss.logging.Logger; 29 30 59 public class JBossObject implements JBossInterface 60 { 61 62 protected Logger log; 63 64 65 protected transient SoftReference toString; 66 67 68 protected transient int hashCode = Integer.MIN_VALUE; 69 70 76 public static boolean equals(Object one, Object two) 77 { 78 if (one == null && two != null) 79 return false; 80 if (one != null && one.equals(two) == false) 81 return false; 82 return true; 83 } 84 85 91 public static boolean notEqual(Object one, Object two) 92 { 93 return equals(one, two) == false; 94 } 95 96 102 public static void list(JBossStringBuilder buffer, Collection objects) 103 { 104 if (objects == null) 105 return; 106 107 buffer.append('['); 108 if (objects.isEmpty() == false) 109 { 110 for (Iterator i = objects.iterator(); i.hasNext();) 111 { 112 Object object = i.next(); 113 if (object instanceof JBossObject) 114 ((JBossObject) object).toShortString(buffer); 115 else 116 buffer.append(object.toString()); 117 if (i.hasNext()) 118 buffer.append(", "); 119 } 120 } 121 buffer.append(']'); 122 } 123 124 127 public JBossObject() 128 { 129 log = Logger.getLogger(getClass()); 130 } 131 132 137 public JBossObject(Logger log) 138 { 139 this.log = (log != null) ? log : Logger.getLogger(getClass()); 140 } 141 142 147 public String toString() 148 { 149 if (cacheToString() == false) 150 return toStringImplementation(); 151 152 String result = null; 153 if (toString != null) 154 result = (String ) toString.get(); 155 156 if (result == null) 157 { 158 result = toStringImplementation(); 159 toString = new SoftReference (result); 160 } 161 return result; 162 } 163 164 169 public int hashCode() 170 { 171 if (hashCode == Integer.MIN_VALUE || cacheGetHashCode() == false) 172 hashCode = getHashCode(); 173 return hashCode; 174 } 175 176 public Object clone() 177 { 178 try 179 { 180 return super.clone(); 181 } 182 catch (CloneNotSupportedException e) 183 { 184 throw new RuntimeException (e); 185 } 186 } 187 188 public String toShortString() 189 { 190 JBossStringBuilder buffer = new JBossStringBuilder(); 191 toShortString(buffer); 192 return buffer.toString(); 193 } 194 195 200 public void toShortString(JBossStringBuilder buffer) 201 { 202 } 203 204 209 public String getClassShortName() 210 { 211 String longName = getClass().getName(); 212 int dot = longName.lastIndexOf('.'); 213 if (dot != -1) 214 return longName.substring(dot + 1); 215 return longName; 216 } 217 218 223 protected String toStringImplementation() 224 { 225 JBossStringBuilder buffer = new JBossStringBuilder(); 226 buffer.append(getClassShortName()).append('@'); 227 buffer.append(Integer.toHexString(System.identityHashCode(this))); 228 buffer.append('{'); 229 toString(buffer); 230 buffer.append('}'); 231 return buffer.toString(); 232 } 233 234 237 protected void flushJBossObjectCache() 238 { 239 toString = null; 240 hashCode = Integer.MIN_VALUE; 241 } 242 243 248 protected void toString(JBossStringBuilder buffer) 249 { 250 } 251 252 257 protected int getHashCode() 258 { 259 return super.hashCode(); 260 } 261 262 267 protected boolean cacheToString() 268 { 269 return true; 270 } 271 272 277 protected boolean cacheGetHashCode() 278 { 279 return true; 280 } 281 } 282 | Popular Tags |