KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.ref.ReferenceQueue JavaDoc;
26
27 /**
28  * Convenience class to wrap an <tt>Object</tt> into a <tt>SoftReference</tt>.
29  *
30  * <p>Modified from <tt>java.util.WeakHashMap.WeakKey</tt>.
31  *
32  * @version <tt>$Revision: 1958 $</tt>
33  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
34  */

35 public final class SoftObject
36    extends SoftReference JavaDoc
37 {
38    /** The hash code of the nested object */
39    protected final int hashCode;
40    
41    /**
42     * Construct a <tt>SoftObject</tt>.
43     *
44     * @param obj Object to reference.
45     */

46    public SoftObject(final Object JavaDoc obj) {
47       super(obj);
48       hashCode = obj.hashCode();
49    }
50    
51    /**
52     * Construct a <tt>SoftObject</tt>.
53     *
54     * @param obj Object to reference.
55     * @param queue Reference queue.
56     */

57    public SoftObject(final Object JavaDoc obj, final ReferenceQueue JavaDoc queue) {
58       super(obj, queue);
59       hashCode = obj.hashCode();
60    }
61    
62    /**
63     * Check the equality of an object with this.
64     *
65     * @param obj Object to test equality with.
66     * @return True if object is equal.
67     */

68    public boolean equals(final Object JavaDoc obj) {
69       if (obj == this) return true;
70
71       if (obj != null && obj.getClass() == getClass()) {
72          SoftObject soft = (SoftObject)obj;
73          
74          Object JavaDoc a = this.get();
75          Object JavaDoc b = soft.get();
76          if (a == null || b == null) return false;
77          if (a == b) return true;
78
79          return a.equals(b);
80       }
81
82       return false;
83    }
84    
85    /**
86     * Return the hash code of the nested object.
87     *
88     * @return The hash code of the nested object.
89     */

90    public int hashCode() {
91       return hashCode;
92    }
93
94
95    /////////////////////////////////////////////////////////////////////////
96
// Factory Methods //
97
/////////////////////////////////////////////////////////////////////////
98

99    /**
100     * Create a <tt>SoftObject</tt> for the given object.
101     *
102     * @param obj Object to reference.
103     * @return <tt>SoftObject</tt> or <tt>null</tt> if object is null.
104     */

105    public static SoftObject create(final Object JavaDoc obj) {
106       if (obj == null) return null;
107       else return new SoftObject(obj);
108    }
109    
110    /**
111     * Create a <tt>SoftObject</tt> for the given object.
112     *
113     * @param obj Object to reference.
114     * @param queue Reference queue.
115     * @return <tt>SoftObject</tt> or <tt>null</tt> if object is null.
116     */

117    public static SoftObject create(final Object JavaDoc obj,
118                                    final ReferenceQueue JavaDoc queue)
119    {
120       if (obj == null) return null;
121       else return new SoftObject(obj, queue);
122    }
123 }
124
Popular Tags