KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright (c) Corporation for National Research Initiatives
2

3 package org.python.core;
4 import java.util.*;
5
6 class CollectionProxy {
7     public static final CollectionProxy NoProxy = new EnumerationProxy(null);
8
9     private static boolean checkedJava2 = false;
10     private static CollectionProxy java2Proxy = null;
11     public CollectionProxy instanceFindCollection(Object JavaDoc object) {
12         return null;
13     }
14
15     public static CollectionProxy findCollection(Object JavaDoc object) {
16         if (object == null) return NoProxy;
17
18         if (!checkedJava2) {
19             checkedJava2 = true;
20             try {
21                 Class JavaDoc c = Class.forName("org.python.core.CollectionProxy2");
22                 Class.forName("java.util.Collection");
23                 java2Proxy = (CollectionProxy)c.newInstance();
24             } catch (Throwable JavaDoc t) { }
25         }
26         if (java2Proxy != null) {
27             CollectionProxy ret = java2Proxy.instanceFindCollection(object);
28             if (ret != null) return ret;
29         }
30
31         if (object instanceof Vector) {
32             return new VectorProxy(((Vector)object));
33         }
34         if (object instanceof Enumeration) {
35             return new EnumerationProxy(((Enumeration)object));
36         }
37         if (object instanceof Dictionary) {
38             return new DictionaryProxy(((Dictionary)object));
39         }
40
41         return NoProxy;
42     }
43
44     /**The basic functions to implement a mapping**/
45     public int __len__() {
46         throw Py.AttributeError("__len__");
47     }
48
49     public PyObject __finditem__(int key) {
50         return __finditem__(new PyInteger(key));
51     }
52
53     public PyObject __finditem__(PyObject key) {
54         throw Py.AttributeError("__getitem__");
55     }
56
57     public PyObject __getitem__(int key) {
58         PyObject ret = __finditem__(key);
59         if (ret == null) throw Py.KeyError(""+key);
60         return ret;
61     }
62
63     public PyObject __getitem__(PyObject key) {
64         PyObject ret = __finditem__(key);
65         if (ret == null) throw Py.KeyError(key.toString());
66         return ret;
67     }
68
69     public void __setitem__(PyObject key, PyObject value) {
70         throw Py.AttributeError("__setitem__");
71     }
72     public void __delitem__(PyObject key) {
73         throw Py.AttributeError("__delitem__");
74     }
75 }
76
77 class EnumerationProxy extends CollectionProxy {
78     Enumeration proxy;
79     int counter;
80
81
82     public EnumerationProxy(Enumeration proxy) {
83         this.proxy = proxy;
84         counter=0;
85     }
86
87     public PyObject __finditem__(int key) {
88         if (key != counter) {
89             throw Py.ValueError(
90                 "enumeration indices must be consecutive ints starting at 0");
91         }
92         counter++;
93         if (proxy.hasMoreElements()) {
94             return Py.java2py(proxy.nextElement());
95         } else {
96             return null;
97         }
98     }
99
100     public PyObject __finditem__(PyObject key) {
101         if (key instanceof PyInteger) {
102             return __finditem__(((PyInteger)key).getValue());
103         } else {
104             throw Py.TypeError("only integer keys accepted");
105         }
106     }
107 }
108
109 class VectorProxy extends CollectionProxy {
110     Vector proxy;
111
112     public VectorProxy(Vector proxy) {
113         this.proxy = proxy;
114     }
115
116     public int __len__() {
117         return proxy.size();
118     }
119
120
121     public PyObject __finditem__(int key) {
122         try {
123             return Py.java2py(proxy.elementAt(key));
124         } catch (ArrayIndexOutOfBoundsException JavaDoc exc) {
125             return null;
126         }
127     }
128
129     public PyObject __finditem__(PyObject key) {
130         if (key instanceof PyInteger) {
131             return __finditem__(((PyInteger)key).getValue());
132         } else {
133             throw Py.TypeError("only integer keys accepted");
134         }
135     }
136
137     public void __setitem__(PyObject key, PyObject value) {
138         if (key instanceof PyInteger) {
139             proxy.setElementAt(Py.tojava(value, Object JavaDoc.class),
140                                ((PyInteger)key).getValue());
141         } else {
142             throw Py.TypeError("only integer keys accepted");
143         }
144     }
145     public void __delitem__(PyObject key) {
146         if (key instanceof PyInteger) {
147             proxy.removeElementAt(((PyInteger)key).getValue());
148         } else {
149             throw Py.TypeError("only integer keys accepted");
150         }
151     }
152 }
153
154 class DictionaryProxy extends CollectionProxy {
155     Dictionary proxy;
156
157     public DictionaryProxy(Dictionary proxy) {
158         this.proxy = proxy;
159     }
160
161     public int __len__() {
162         return proxy.size();
163     }
164
165     public PyObject __finditem__(int key) {
166         throw Py.TypeError("loop over non-sequence");
167     }
168
169     public PyObject __finditem__(PyObject key) {
170         return Py.java2py(proxy.get(Py.tojava(key, Object JavaDoc.class)));
171     }
172
173     public void __setitem__(PyObject key, PyObject value) {
174         proxy.put(Py.tojava(key, Object JavaDoc.class),
175                   Py.tojava(value, Object JavaDoc.class));
176     }
177
178     public void __delitem__(PyObject key) {
179         proxy.remove(Py.tojava(key, Object JavaDoc.class));
180     }
181 }
182
Popular Tags