KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > IdImpl2


1 package org.python.core;
2
3 import java.lang.ref.WeakReference JavaDoc;
4 import java.lang.ref.ReferenceQueue JavaDoc;
5 import java.util.HashMap JavaDoc;
6
7 public class IdImpl2 extends IdImpl {
8     
9     public static class WeakIdentityMap {
10         
11         private ReferenceQueue JavaDoc refqueue = new ReferenceQueue JavaDoc();
12         private HashMap JavaDoc hashmap = new HashMap JavaDoc();
13         
14         private void cleanup() {
15             Object JavaDoc k;
16             while ((k = refqueue.poll()) != null) {
17                 hashmap.remove(k);
18             }
19         }
20                 
21         private class WeakIdKey extends WeakReference JavaDoc {
22             private int hashcode;
23             
24             WeakIdKey(Object JavaDoc obj) {
25                 super(obj,refqueue);
26                 hashcode = System.identityHashCode(obj);
27             }
28             
29             public int hashCode() {
30                 return hashcode;
31             }
32             
33             public boolean equals(Object JavaDoc other) {
34                 Object JavaDoc obj = this.get();
35                 if (obj != null) {
36                     return obj == ((WeakIdKey)other).get();
37                 } else {
38                     return this == other;
39                 }
40             }
41         }
42         
43         public int _internal_map_size() {
44             return hashmap.size();
45         }
46         
47         public void put(Object JavaDoc key,Object JavaDoc val) {
48             cleanup();
49             hashmap.put(new WeakIdKey(key),val);
50         }
51         
52         public Object JavaDoc get(Object JavaDoc key) {
53             cleanup();
54             return hashmap.get(new WeakIdKey(key));
55         }
56
57         public void remove(Object JavaDoc key) {
58             cleanup();
59             hashmap.remove(new WeakIdKey(key));
60         }
61
62     }
63
64     private WeakIdentityMap id_map = new WeakIdentityMap();
65     private long sequential_id = 0;
66
67     public long id(PyObject o) {
68         if (o instanceof PyJavaInstance) {
69             return java_obj_id(((PyJavaInstance)o).javaProxy);
70         } else {
71             return java_obj_id(o);
72         }
73     }
74
75     // XXX maybe should display both this id and identityHashCode
76
// XXX preserve the old "at ###" style?
77
public String JavaDoc idstr(PyObject o) {
78         return Long.toString(id(o));
79     }
80
81     public synchronized long java_obj_id(Object JavaDoc o) {
82         Long JavaDoc cand = (Long JavaDoc)id_map.get(o);
83         if (cand == null) {
84             sequential_id++;
85             long new_id = sequential_id;
86             id_map.put(o,new Long JavaDoc(new_id));
87             return new_id;
88         }
89         return cand.longValue();
90     }
91     
92 }
93
Popular Tags