KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright 2000 Samuele Pedroni
2

3 package org.python.core;
4
5 import java.util.*;
6 import java.lang.ref.*;
7
8 public class InternalTables2 extends InternalTables1 {
9
10     protected static class TableProvid2 extends HashMap implements Table {
11     }
12
13     protected void commitTemp() {
14         ((TableProvid2)classes).putAll((TableProvid2)temp);
15         temp.clear();
16     }
17
18     protected WeakHashMap adapters;
19
20     protected Object JavaDoc getAdapter(Object JavaDoc o,String JavaDoc evc) {
21         HashMap ads = (HashMap)adapters.get(o);
22         if (ads == null) return null;
23         WeakReference adw = (WeakReference) ads.get(evc);
24         if (adw == null) return null;
25         return adw.get();
26     }
27
28     protected void putAdapter(Object JavaDoc o,String JavaDoc evc,Object JavaDoc ad) {
29         HashMap ads = (HashMap)adapters.get(o);
30         if (ads == null) {
31             ads = new HashMap();
32             adapters.put(o,ads);
33         }
34         ads.put(evc,new WeakReference(ad));
35     }
36
37     protected Iterator iter;
38     protected Iterator grand;
39
40     public void _beginCanonical() {
41         beginStable(JCSTABLE);
42         iter = ((TableProvid2)classes).values().iterator();
43         iterType = JCLASS;
44     }
45
46     public void _beginLazyCanonical() {
47         beginStable(GSTABLE);
48         iter = ((TableProvid2)lazyClasses).values().iterator();
49         iterType = LAZY_JCLASS;
50     }
51
52     public void _beginOverAdapterClasses() {
53         beginStable(GSTABLE);
54         iter = ((TableProvid2)adapterClasses).entrySet().iterator();
55         iterType = ADAPTER_CLASS;
56
57     }
58
59     public void _beginOverAdapters() {
60         beginStable((short)0);
61         grand = adapters.values().iterator();
62         iter = null;
63         iterType = ADAPTER;
64     }
65
66     public Object JavaDoc _next() {
67         if (iterType == ADAPTER) {
68             for(;;) {
69                 if (iter==null || !iter.hasNext() ) {
70                     if (grand.hasNext()) {
71                         cur = grand.next();
72                         iter = ((HashMap)cur).values().iterator();
73                     } else iter = null;
74                 }
75                 if (iter != null) {
76                     WeakReference adw = (WeakReference)iter.next();
77                     Object JavaDoc ad = adw.get();
78                     if (ad != null) return ad.getClass().getInterfaces()[0];
79                     else continue;
80                 }
81                 grand = null;
82                 break;
83             }
84         }
85         else if (iter.hasNext()) {
86             cur = iter.next();
87             switch(iterType) {
88             case JCLASS:
89                 return (PyJavaClass)cur;
90             case LAZY_JCLASS:
91                 PyJavaClass lazy = (PyJavaClass)cur;
92                 return new _LazyRep(lazy.__name__,lazy.__mgr__);
93             case ADAPTER_CLASS:
94                 Map.Entry entry = (Map.Entry)cur;
95                 return entry.getKey();
96             }
97         }
98         cur = null;
99         endStable();
100         iter = null;
101         return null;
102     }
103
104     public void _flushCurrent() {
105        iter.remove();
106        switch(iterType) {
107        case JCLASS:
108            classesDec(((PyJavaClass)cur).__name__);
109            break;
110        case ADAPTER:
111            if (((HashMap)cur).size() == 0) grand.remove();
112        }
113     }
114
115     public InternalTables2() {
116         super(true);
117
118         classes = new TableProvid2();
119         temp = new TableProvid2();
120         counters = new TableProvid2();
121         lazyClasses = new TableProvid2();
122
123         adapterClasses = new TableProvid2();
124
125         adapters = new WeakHashMap();
126     }
127 }
Popular Tags