KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 /* Support for java-1.2 collections
4 This will only compile with jdk-1.2 or later.
5 The rest of JPython works just fine with out it, so feel free to
6 just leave this uncompiled if you're using jdk-1.1.
7 */

8
9 package org.python.core;
10 import java.util.*;
11
12 class CollectionProxy2 extends CollectionProxy
13 {
14     public CollectionProxy instanceFindCollection(Object JavaDoc object) {
15         if (object instanceof List) {
16             return new ListProxy(((List)object));
17         }
18         if (object instanceof Map) {
19             return new MapProxy(((Map)object));
20         }
21         if (object instanceof Collection) {
22             return new IteratorProxy(((Collection)object).iterator());
23         }
24         if (object instanceof Iterator) {
25             return new IteratorProxy(((Iterator)object));
26         }
27
28         return null;
29     }
30 }
31
32 class ListProxy extends CollectionProxy
33 {
34     List proxy;
35
36     public ListProxy(List proxy) {
37         this.proxy = proxy;
38     }
39
40     public int __len__() {
41         return proxy.size();
42     }
43
44     public PyObject __finditem__(int key) {
45         try {
46             return Py.java2py(proxy.get(key));
47         } catch (IndexOutOfBoundsException JavaDoc exc) {
48             return null;
49         }
50     }
51
52     public PyObject __finditem__(PyObject key) {
53         if (key instanceof PyInteger) {
54             return __finditem__(((PyInteger)key).getValue());
55         } else {
56             throw Py.TypeError("only integer keys accepted");
57         }
58     }
59
60     public void __setitem__(int key, PyObject value) {
61         proxy.set(key, Py.tojava(value, Object JavaDoc.class));
62     }
63
64     public void __setitem__(PyObject key, PyObject value) {
65         if (key instanceof PyInteger) {
66             __setitem__(((PyInteger)key).getValue(), value);
67         } else {
68             throw Py.TypeError("only integer keys accepted");
69         }
70     }
71
72     public void __delitem__(int key) {
73         proxy.remove(key);
74     }
75
76     public void __delitem__(PyObject key) {
77         if (key instanceof PyInteger) {
78             __delitem__(((PyInteger)key).getValue());
79         } else {
80             throw Py.TypeError("only integer keys accepted");
81         }
82     }
83 }
84
85 class MapProxy extends CollectionProxy
86 {
87     Map proxy;
88
89     public MapProxy(Map proxy) {
90         this.proxy = proxy;
91     }
92
93     public int __len__() {
94         return proxy.size();
95     }
96
97     public PyObject __finditem__(int key) {
98         throw Py.TypeError("loop over non-sequence");
99     }
100
101     public PyObject __finditem__(PyObject key) {
102         return Py.java2py(proxy.get(Py.tojava(key, Object JavaDoc.class)));
103     }
104
105     public void __setitem__(PyObject key, PyObject value) {
106         proxy.put(Py.tojava(key, Object JavaDoc.class),
107                   Py.tojava(value, Object JavaDoc.class));
108     }
109
110     public void __delitem__(PyObject key) {
111         proxy.remove(Py.tojava(key, Object JavaDoc.class));
112     }
113 }
114
115
116 class IteratorProxy extends CollectionProxy
117 {
118     Iterator proxy;
119     int counter;
120
121     public IteratorProxy(Iterator proxy) {
122         this.proxy = proxy;
123         counter=0;
124     }
125
126     public PyObject __finditem__(int key) {
127         if (key != counter) {
128             throw Py.ValueError(
129                 "iterator indices must be consecutive ints starting at 0");
130         }
131         counter++;
132         if (proxy.hasNext()) {
133             return Py.java2py(proxy.next());
134         } else {
135             return null;
136         }
137     }
138
139     public PyObject __finditem__(PyObject key) {
140         if (key instanceof PyInteger) {
141             return __finditem__(((PyInteger)key).getValue());
142         } else {
143             throw Py.TypeError("only integer keys accepted");
144         }
145     }
146 }
147
Popular Tags