KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > JBossObject


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util;
23
24 import java.lang.ref.SoftReference JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.jboss.logging.Logger;
29
30 /**
31  * Utility Class
32  *
33  * Utility Class that provides a Logger instance (log) and
34  * caching of toString() and hashCode() values.
35  *
36  * You most probably want to override the method that
37  * comes from JBossInterface:
38  *
39  * public void toShortString(StringBuffer buffer)
40  *
41  * to append to the buffer the key class properties, and
42  * also override the following methods to provide the
43  * hashCode and the class properties that should be cached:
44  *
45  * protected void toString(StringBuffer buffer)
46  * protected int getHashCode()
47  *
48  * Cached values can be flushed using flushJBossObjectCache()
49  *
50  * Caching can be disabled by simply overriding toString()
51  * and hashCode(), or returning false from methods:
52  *
53  * protected boolean cacheToString()
54  * protected boolean cacheGetHashCode()
55  *
56  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
57  * @version $Revision: 1958 $
58  */

59 public class JBossObject implements JBossInterface
60 {
61    /** The log */
62    protected Logger log;
63    
64    /** Cached toString */
65    protected transient SoftReference JavaDoc toString;
66    
67    /** Cached hashCode */
68    protected transient int hashCode = Integer.MIN_VALUE;
69
70    /**
71     * Safe equality check
72     *
73     * @param one an object
74     * @param two another object
75     */

76    public static boolean equals(Object JavaDoc one, Object JavaDoc 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    /**
86     * Safe inequality check
87     *
88     * @param one an object
89     * @param two another object
90     */

91    public static boolean notEqual(Object JavaDoc one, Object JavaDoc two)
92    {
93       return equals(one, two) == false;
94    }
95
96    /**
97     * List the set of JBossObjects
98     *
99     * @param buffer the buffer
100     * @param objects the collection of objects
101     */

102    public static void list(JBossStringBuilder buffer, Collection JavaDoc objects)
103    {
104       if (objects == null)
105          return;
106
107       buffer.append('[');
108       if (objects.isEmpty() == false)
109       {
110          for (Iterator JavaDoc i = objects.iterator(); i.hasNext();)
111          {
112             Object JavaDoc 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    /**
125     * Create a new object
126     */

127    public JBossObject()
128    {
129       log = Logger.getLogger(getClass());
130    }
131    
132    /**
133     * Create a new object using the specified Logger instace
134     *
135     * @param log the Logger instance to use
136     */

137    public JBossObject(Logger log)
138    {
139       this.log = (log != null) ? log : Logger.getLogger(getClass());
140    }
141    
142    /**
143     * Override toString to cache the value
144     *
145     * @return the String
146     */

147    public String JavaDoc toString()
148    {
149       if (cacheToString() == false)
150          return toStringImplementation();
151
152       String JavaDoc result = null;
153       if (toString != null)
154          result = (String JavaDoc) toString.get();
155
156       if (result == null)
157       {
158          result = toStringImplementation();
159          toString = new SoftReference JavaDoc(result);
160       }
161       return result;
162    }
163    
164    /**
165     * Override hashCode to cache the value
166     *
167     * @return the hashCode
168     */

169    public int hashCode()
170    {
171       if (hashCode == Integer.MIN_VALUE || cacheGetHashCode() == false)
172          hashCode = getHashCode();
173       return hashCode;
174    }
175    
176    public Object JavaDoc clone()
177    {
178       try
179       {
180          return super.clone();
181       }
182       catch (CloneNotSupportedException JavaDoc e)
183       {
184          throw new RuntimeException JavaDoc(e);
185       }
186    }
187
188    public String JavaDoc toShortString()
189    {
190       JBossStringBuilder buffer = new JBossStringBuilder();
191       toShortString(buffer);
192       return buffer.toString();
193    }
194    
195    /**
196     * Append the key class properties to the buffer
197     *
198     * @param buffer the buffer
199     */

200    public void toShortString(JBossStringBuilder buffer)
201    {
202    }
203    
204    /**
205     * Get the class short name
206     *
207     * @return the short name of the class
208     */

209    public String JavaDoc getClassShortName()
210    {
211       String JavaDoc longName = getClass().getName();
212       int dot = longName.lastIndexOf('.');
213       if (dot != -1)
214          return longName.substring(dot + 1);
215       return longName;
216    }
217
218    /**
219     * Implementation of String
220     *
221     * @return the string
222     */

223    protected String JavaDoc 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    /**
235     * Flush the JBossObject cached values
236     */

237    protected void flushJBossObjectCache()
238    {
239       toString = null;
240       hashCode = Integer.MIN_VALUE;
241    }
242    
243    /**
244     * Append the class properties to the buffer
245     *
246     * @param buffer the buffer
247     */

248    protected void toString(JBossStringBuilder buffer)
249    {
250    }
251    
252    /**
253     * Calculate the hashcode
254     *
255     * @return the hash code
256     */

257    protected int getHashCode()
258    {
259       return super.hashCode();
260    }
261    
262    /**
263     * Whether we should cache the result toString()
264     *
265     * @return true by default
266     */

267    protected boolean cacheToString()
268    {
269       return true;
270    }
271    
272    /**
273     * Whether we should cache the result hashCode()
274     *
275     * @return true by default
276     */

277    protected boolean cacheGetHashCode()
278    {
279       return true;
280    }
281 }
282
Popular Tags