KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v1 > identicator > WeakIdHashKey


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
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
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v1.identicator;
25
26 import java.lang.ref.*;
27
28 // revisit equals() if ever made non-final
29

30 final class WeakIdHashKey extends IdHashKey
31 {
32     Ref keyRef;
33     int hash;
34
35     public WeakIdHashKey(Object JavaDoc keyObj, Identicator id, ReferenceQueue rq)
36     {
37     super( id );
38
39     if (keyObj == null)
40         throw new UnsupportedOperationException JavaDoc("Collection does not accept nulls!");
41
42     this.keyRef = new Ref( keyObj, rq );
43     this.hash = id.hash( keyObj );
44     }
45
46     public Ref getInternalRef()
47     { return this.keyRef; }
48
49     public Object JavaDoc getKeyObj()
50     { return keyRef.get(); }
51
52     public boolean equals(Object JavaDoc o)
53     {
54     // fast type-exact match for final class
55
if (o instanceof WeakIdHashKey)
56         {
57         WeakIdHashKey other = (WeakIdHashKey) o;
58         if (this.keyRef == other.keyRef)
59             return true;
60         else
61             {
62             Object JavaDoc myKeyObj = this.keyRef.get();
63             Object JavaDoc oKeyObj = other.keyRef.get();
64             if (myKeyObj == null || oKeyObj == null)
65                 return false;
66             else
67                 return id.identical( myKeyObj, oKeyObj );
68             }
69         }
70     else
71         return false;
72     }
73
74     public int hashCode()
75     { return hash; }
76
77     class Ref extends WeakReference
78     {
79     public Ref( Object JavaDoc referant, ReferenceQueue rq )
80     { super( referant, rq ); }
81     
82     WeakIdHashKey getKey()
83     { return WeakIdHashKey.this; }
84     }
85 }
86
Popular Tags