KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright 2000 Samuele Pedroni
2

3 package org.python.core;
4
5 import java.lang.ref.*;
6 import java.util.*;
7
8 public abstract class AutoInternalTables extends InternalTables2 {
9
10     protected ReferenceQueue queue = new ReferenceQueue();
11
12     protected abstract Reference newAutoRef(short type, Object JavaDoc key,
13                                             Object JavaDoc obj);
14     protected abstract short getAutoRefType(Reference ref);
15     protected abstract Object JavaDoc getAutoRefKey(Reference ref);
16
17     private synchronized void cleanup() {
18         if (keepstable >= GSTABLE)
19          return;
20         adapters.remove(null); // trick
21
Reference ref;
22         while ((ref = queue.poll()) != null) {
23             Object JavaDoc key = getAutoRefKey(ref);
24             switch(getAutoRefType(ref)) {
25             case JCLASS:
26                 Class JavaDoc cl = (Class JavaDoc)key;
27                 classes.remove(cl);
28                 classesDec(cl.getName());
29                 break;
30             case LAZY_JCLASS:
31                 lazyClasses.remove(key);
32                 break;
33             case ADAPTER_CLASS:
34                 adapterClasses.remove(key);
35             }
36         }
37     }
38
39
40     protected boolean queryCanonical(String JavaDoc name) {
41         cleanup();
42         return super.queryCanonical(name);
43     }
44
45     protected PyJavaClass getCanonical(Class JavaDoc c) {
46         cleanup();
47         Reference ref = (Reference)classesGet(c);
48         if (ref == null) return null;
49         return (PyJavaClass)ref.get();
50     }
51
52     protected PyJavaClass getLazyCanonical(String JavaDoc name) {
53         cleanup();
54         Reference ref = (Reference)lazyClasses.get(name);
55         if (ref == null) return null;
56         return (PyJavaClass)ref.get();
57     }
58
59     protected void putCanonical(Class JavaDoc c,PyJavaClass canonical) {
60         cleanup();
61         classesPut(c,newAutoRef(JCLASS,c,canonical));
62     }
63
64     protected void putLazyCanonical(String JavaDoc name,PyJavaClass canonical) {
65         cleanup();
66         lazyClasses.put(name,newAutoRef(LAZY_JCLASS,name,canonical));
67     }
68
69     protected Class JavaDoc getAdapterClass(Class JavaDoc c) {
70         cleanup();
71         Reference ref = (Reference)adapterClasses.get(c);
72         if (ref == null) return null;
73         return (Class JavaDoc)ref.get();
74     }
75
76     protected void putAdapterClass(Class JavaDoc c,Class JavaDoc ac) {
77         cleanup();
78         adapterClasses.put(c,newAutoRef(ADAPTER_CLASS,c,ac));
79     }
80
81     protected Object JavaDoc getAdapter(Object JavaDoc o,String JavaDoc evc) {
82         cleanup();
83         return super.getAdapter(o,evc);
84     }
85
86     protected void putAdapter(Object JavaDoc o,String JavaDoc evc,Object JavaDoc ad) {
87         cleanup();
88         super.putAdapter(o,evc,ad);
89     }
90
91
92     public boolean _doesSomeAutoUnload() { return true; }
93
94     public void _forceCleanup() { cleanup(); }
95
96     public void _beginCanonical() {
97         cleanup();
98         super._beginCanonical();
99     }
100
101     public void _beginLazyCanonical() {
102         cleanup();
103         super._beginLazyCanonical();
104     }
105
106     public void _beginOverAdapterClasses() {
107         cleanup();
108         super._beginOverAdapterClasses();
109
110     }
111
112     public void _beginOverAdapters() {
113         cleanup();
114         super._beginOverAdapters();
115     }
116
117     public Object JavaDoc _next() {
118         if (iterType == ADAPTER) {
119             Object JavaDoc ret = super._next();
120             if (ret != null) return ret;
121         } else {
122             while(iter.hasNext()) {
123                 cur = iter.next();
124                 switch(iterType) {
125                 case JCLASS:
126                     PyJavaClass jc = (PyJavaClass)((Reference)cur).get();
127                     if (jc == null ) continue;
128                     cur = jc;
129                     return jc;
130                 case LAZY_JCLASS:
131                     PyJavaClass lazy = (PyJavaClass)((Reference)cur).get();
132                     if (lazy == null) continue;
133                     return new _LazyRep(lazy.__name__,lazy.__mgr__);
134                 case ADAPTER_CLASS:
135                     Map.Entry entry = (Map.Entry)cur;
136                     if (((Reference)entry.getValue()).get() == null )
137                         continue;
138                     return entry.getKey();
139                 }
140             }
141             cur = null;
142             iter = null;
143             endStable();
144         }
145         cleanup();
146         return null;
147     }
148
149     public void _flush(PyJavaClass jc) {
150         cleanup();
151         super._flush(jc);
152     }
153
154 }
155
Popular Tags