KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright 2000 Samuele Pedroni
2

3 package org.python.core;
4
5 import java.util.*;
6
7
8 public class InternalTables1 extends InternalTables {
9
10     protected static interface Table {
11         public Object JavaDoc put(Object JavaDoc key,Object JavaDoc obj);
12         public Object JavaDoc get(Object JavaDoc key);
13         public Object JavaDoc remove(Object JavaDoc key);
14         public void clear();
15     }
16
17     private static class TableProvid1 extends Hashtable implements Table {
18     }
19
20     final protected static short JCLASS=0;
21     final protected static short LAZY_JCLASS=1;
22     final protected static short ADAPTER_CLASS=2;
23     final protected static short ADAPTER = 3;
24
25     protected Table classes;
26     protected Table temp;
27     protected Table counters;
28     protected Table lazyClasses;
29
30     protected Table adapterClasses;
31
32     protected final short GSTABLE=1;
33     protected final short JCSTABLE=2;
34
35     protected short keepstable;
36
37     protected void beginStable(short lvl) {
38         keepstable = lvl;
39     }
40
41     protected void classesPut(Class JavaDoc c,Object JavaDoc jc) {
42         if (keepstable == JCSTABLE) {
43             temp.put(c,jc);
44             // System.err.println("temp-defer-canonical: "+c.getName());
45
} else {
46             classes.put(c,jc);
47         }
48         String JavaDoc name = c.getName();
49         Integer JavaDoc cnt = (Integer JavaDoc)counters.get(name);
50         if (cnt == null) {
51             counters.put(name,new Integer JavaDoc(1));
52             lazyClasses.remove(name);
53         } else {
54             counters.put(name,new Integer JavaDoc(cnt.intValue()+1));
55         }
56     }
57
58     protected Object JavaDoc classesGet(Class JavaDoc c) {
59         Object JavaDoc o = classes.get(c);
60         if (o != null || keepstable != JCSTABLE) return o;
61         return temp.get(c);
62     }
63
64     protected void endStable() {
65         if (keepstable == JCSTABLE)
66           commitTemp();
67         keepstable = 0;
68     }
69
70     protected void classesDec(String JavaDoc name) {
71         int c = ((Integer JavaDoc)counters.get(name)).intValue();
72         if (c == 1)
73          counters.remove(name);
74         else
75          counters.put(name,new Integer JavaDoc(c-1));
76     }
77
78     protected void commitTemp() {
79         for(Enumeration e=((Hashtable)temp).keys();e.hasMoreElements();) {
80             Object JavaDoc c = e.nextElement();
81             classes.put(c,temp.get(c));
82         }
83         temp.clear();
84     }
85
86     protected boolean queryCanonical(String JavaDoc name) {
87         return counters.get(name) != null || lazyClasses.get(name) != null;
88     }
89
90     protected PyJavaClass getCanonical(Class JavaDoc c) {
91         return (PyJavaClass)classesGet(c);
92     }
93
94     protected PyJavaClass getLazyCanonical(String JavaDoc name) {
95         return (PyJavaClass)lazyClasses.get(name);
96     }
97
98     protected void putCanonical(Class JavaDoc c,PyJavaClass canonical) {
99         classesPut(c,canonical);
100     }
101
102     protected void putLazyCanonical(String JavaDoc name,PyJavaClass canonical) {
103         lazyClasses.put(name,canonical);
104     }
105
106     protected Class JavaDoc getAdapterClass(Class JavaDoc c) {
107         return (Class JavaDoc)adapterClasses.get(c);
108     }
109
110     protected void putAdapterClass(Class JavaDoc c,Class JavaDoc ac) {
111         adapterClasses.put(c,ac);
112     }
113
114     private Hashtable adapters;
115
116     protected Object JavaDoc getAdapter(Object JavaDoc o,String JavaDoc evc) {
117         return adapters.get(evc+'$'+System.identityHashCode(o));
118     }
119
120     protected void putAdapter(Object JavaDoc o,String JavaDoc evc,Object JavaDoc ad) {
121         adapters.put(evc+'$'+System.identityHashCode(o),ad);
122     }
123
124     protected short iterType;
125     protected Object JavaDoc cur;
126
127     private Enumeration enumm;
128     private Hashtable enumTable;
129
130     public void _beginCanonical() {
131         beginStable(JCSTABLE);
132         enumm = ((TableProvid1)classes).keys();
133         enumTable = (TableProvid1)classes;
134         iterType = JCLASS;
135     }
136
137     public void _beginLazyCanonical() {
138         enumm = ((TableProvid1)lazyClasses).keys();
139         enumTable = (TableProvid1)lazyClasses;
140         iterType = LAZY_JCLASS;
141     }
142
143     public void _beginOverAdapterClasses() {
144         enumm = ((TableProvid1)adapterClasses).keys();
145         enumTable = (TableProvid1)adapterClasses;
146         iterType = ADAPTER_CLASS;
147
148     }
149
150     public void _beginOverAdapters() {
151         enumm = adapters.keys();
152         enumTable = adapters;
153         iterType = ADAPTER;
154     }
155
156     public Object JavaDoc _next() {
157         if(enumm.hasMoreElements()) {
158             cur = enumm.nextElement();
159             switch(iterType) {
160             case JCLASS:
161                 return (PyJavaClass)classes.get(cur);
162             case LAZY_JCLASS:
163                 PyJavaClass lazy = (PyJavaClass)lazyClasses.get(cur);
164                 return new _LazyRep(lazy.__name__,lazy.__mgr__);
165             case ADAPTER_CLASS:
166                 return cur;
167             case ADAPTER:
168                 return adapters.get(cur).getClass().getInterfaces()[0];
169             }
170         }
171         cur = null;
172         enumm = null;
173         endStable();
174         return null;
175     }
176
177     public void _flushCurrent() {
178        enumTable.remove(cur);
179        if (iterType == JCLASS) classesDec(((Class JavaDoc)cur).getName());
180     }
181
182     public void _flush(PyJavaClass jc) {
183         Class JavaDoc c = jc.proxyClass;
184         if (c == null) {
185             lazyClasses.remove(jc.__name__);
186         } else {
187             classes.remove(c);
188             classesDec(jc.__name__);
189         }
190     }
191
192     protected InternalTables1(boolean fake) {
193     }
194
195     public InternalTables1() {
196         classes = new TableProvid1();
197         temp = new TableProvid1();
198         counters = new TableProvid1();
199         lazyClasses = new TableProvid1();
200
201         adapterClasses = new TableProvid1();
202
203         adapters = new Hashtable();
204     }
205 }
206
207
Popular Tags