001: // Copyright 2000 Samuele Pedroni
002:
003: package org.python.core;
004:
005: import java.util.*;
006: import java.lang.ref.*;
007:
008: public class InternalTables2 extends InternalTables1 {
009:
010: protected static class TableProvid2 extends HashMap implements
011: Table {
012: }
013:
014: protected void commitTemp() {
015: ((TableProvid2) this .classes).putAll((TableProvid2) this .temp);
016: this .temp.clear();
017: }
018:
019: protected WeakHashMap adapters;
020:
021: protected Object getAdapter(Object o, String evc) {
022: HashMap ads = (HashMap) this .adapters.get(o);
023: if (ads == null) {
024: return null;
025: }
026: WeakReference adw = (WeakReference) ads.get(evc);
027: if (adw == null) {
028: return null;
029: }
030: return adw.get();
031: }
032:
033: protected void putAdapter(Object o, String evc, Object ad) {
034: HashMap ads = (HashMap) this .adapters.get(o);
035: if (ads == null) {
036: ads = new HashMap();
037: this .adapters.put(o, ads);
038: }
039: ads.put(evc, new WeakReference(ad));
040: }
041:
042: protected Iterator iter;
043: protected Iterator grand;
044:
045: public void _beginCanonical() {
046: beginStable(this .JCSTABLE);
047: this .iter = ((TableProvid2) this .classes).values().iterator();
048: this .iterType = JCLASS;
049: }
050:
051: public void _beginLazyCanonical() {
052: beginStable(this .GSTABLE);
053: this .iter = ((TableProvid2) this .lazyClasses).values()
054: .iterator();
055: this .iterType = LAZY_JCLASS;
056: }
057:
058: public void _beginOverAdapterClasses() {
059: beginStable(this .GSTABLE);
060: this .iter = ((TableProvid2) this .adapterClasses).entrySet()
061: .iterator();
062: this .iterType = ADAPTER_CLASS;
063:
064: }
065:
066: public void _beginOverAdapters() {
067: beginStable((short) 0);
068: this .grand = this .adapters.values().iterator();
069: this .iter = null;
070: this .iterType = ADAPTER;
071: }
072:
073: public Object _next() {
074: if (this .iterType == ADAPTER) {
075: for (;;) {
076: if (this .iter == null || !this .iter.hasNext()) {
077: if (this .grand.hasNext()) {
078: this .cur = this .grand.next();
079: this .iter = ((HashMap) this .cur).values()
080: .iterator();
081: } else {
082: this .iter = null;
083: }
084: }
085: if (this .iter != null) {
086: WeakReference adw = (WeakReference) this .iter
087: .next();
088: Object ad = adw.get();
089: if (ad != null) {
090: return ad.getClass().getInterfaces()[0];
091: } else {
092: continue;
093: }
094: }
095: this .grand = null;
096: break;
097: }
098: } else if (this .iter.hasNext()) {
099: this .cur = this .iter.next();
100: switch (this .iterType) {
101: case JCLASS:
102: return (PyJavaClass) this .cur;
103: case LAZY_JCLASS:
104: PyJavaClass lazy = (PyJavaClass) this .cur;
105: return new _LazyRep(lazy.__name__, lazy.__mgr__);
106: case ADAPTER_CLASS:
107: Map.Entry entry = (Map.Entry) this .cur;
108: return entry.getKey();
109: }
110: }
111: this .cur = null;
112: endStable();
113: this .iter = null;
114: return null;
115: }
116:
117: public void _flushCurrent() {
118: this .iter.remove();
119: switch (this .iterType) {
120: case JCLASS:
121: classesDec(((PyJavaClass) this .cur).__name__);
122: break;
123: case ADAPTER:
124: if (((HashMap) this .cur).size() == 0)
125: this .grand.remove();
126: }
127: }
128:
129: public InternalTables2() {
130: super (true);
131:
132: this .classes = new TableProvid2();
133: this .temp = new TableProvid2();
134: this .counters = new TableProvid2();
135: this .lazyClasses = new TableProvid2();
136:
137: this .adapterClasses = new TableProvid2();
138:
139: this .adapters = new WeakHashMap();
140: }
141: }
|