-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathAbstractDict.java
More file actions
379 lines (316 loc) · 10.7 KB
/
AbstractDict.java
File metadata and controls
379 lines (316 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package org.python.core;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentMap;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public abstract class AbstractDict extends PyObject {
public AbstractDict(PyType type) {
super(type);
}
public abstract void clear();
public abstract AbstractDict copy();
public abstract PyObject get(PyObject key);
public abstract PyObject get(PyObject key, PyObject defaultObj);
public abstract ConcurrentMap<? extends Object, PyObject> getMap();
public abstract boolean has_key(PyObject key);
public abstract PyList items();
public abstract PyObject iteritems();
public abstract PyObject iterkeys();
public abstract PyObject itervalues();
public abstract PyList keys();
public abstract void merge(PyObject other, boolean override);
public abstract void mergeFromKeys(PyObject other, PyObject keys, boolean override);
public abstract void mergeFromSeq(PyObject other, boolean override);
public abstract PyObject pop(PyObject key);
public abstract PyObject pop(PyObject key, PyObject defaultValue);
public abstract PyObject popitem();
public abstract PyObject setdefault(PyObject key);
public abstract PyObject setdefault(PyObject key, PyObject failobj);
public abstract void update(PyObject other);
public abstract Collection<? extends Object> values();
public abstract Set<PyObject> pyKeySet();
public abstract Set entrySet();
/**
* Returns a dict_keys on the dictionary's keys
*/
public PyObject viewkeys() {
return new PyDictionary.PyDictionaryViewKeys(this);
}
/**
* Returns a dict_items on the dictionary's items
*/
public PyObject viewitems() {
return new PyDictionary.PyDictionaryViewItems(this);
}
/**
* Returns a dict_values on the dictionary's values
*/
public PyObject viewvalues() {
return new PyDictionary.PyDictionaryViewValues(this);
}
/** Convert return values to java objects */
static Object tojava(Object val) {
return val == null ? null : ((PyObject)val).__tojava__(Object.class);
}
/**
* Only valid for T=PyObject.
* Subclasses can use a different T, but then have to
* override __iternext__() accordingly.
*/
static abstract class AbstractDictIter<T> extends PyIterator {
protected final Iterator<T> iterator;
protected final int size;
public AbstractDictIter(Collection<T> values) {
iterator = values.iterator();
size = values.size();
}
/**
* For use by subclasses.
*/
protected void check(int size) {
if (size != this.size) {
throw Py.RuntimeError("dictionary changed size during iteration");
}
}
/**
* Warning: This default implementation is only
* valid for T=PyObject.
*/
@Override
public PyObject __iternext__() {
if (!iterator.hasNext()) {
return null;
}
return (PyObject) iterator.next();
}
}
static class ValuesIter extends AbstractDictIter<PyObject>
{
public ValuesIter(Collection<PyObject> values) {
super(values);
}
}
/**
* Only valid for T=PyObject.
* Subclasses can use a different T, but then have to
* override __iternext__() accordingly.
*/
static class KeysIter<T> extends AbstractDictIter<T>
{
public KeysIter(Collection<T> keys) {
super(keys);
}
}
/**
* Only valid for T=PyObject.
* Subclasses can use a different T, but then have to
* override __iternext__() accordingly.
*/
static class ItemsIter<T> extends AbstractDictIter<Entry<T, PyObject>> {
public ItemsIter(Set<Entry<T, PyObject>> items) {
super(items);
}
@Override
public PyObject __iternext__() {
if (!iterator.hasNext()) {
return null;
}
Entry<T, PyObject> entry = iterator.next();
return new PyTuple((PyObject) entry.getKey(), entry.getValue());
}
}
}
/** Basic implementation of Entry that just holds onto a key and value and returns them. */
class SimpleEntry implements Entry {
protected Object key;
protected Object value;
public SimpleEntry(Object key, Object value) {
this.key = key;
this.value = value;
}
public Object getKey() {
return key;
}
public Object getValue() {
return value;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Map.Entry)) {
return false;
}
Map.Entry e = (Map.Entry)o;
return eq(key, e.getKey()) && eq(value, e.getValue());
}
private static boolean eq(Object o1, Object o2) {
return o1 == null ? o2 == null : o1.equals(o2);
}
@Override
public int hashCode() {
return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
}
@Override
public String toString() {
return key + "=" + value;
}
public Object setValue(Object val) {
throw new UnsupportedOperationException("Not supported by this view");
}
}
/**
* Wrapper for a Entry object returned from the java.util.Set object which in turn is returned by
* the entrySet method of java.util.Map. This is needed to correctly convert from PyObjects to java
* Objects. Note that we take care in the equals and hashCode methods to make sure these methods are
* consistent with Entry objects that contain java Objects for a value so that on the java side they
* can be reliable compared.
*/
class PyToJavaMapEntry extends SimpleEntry {
/** Create a copy of the Entry with Py.None converted to null */
PyToJavaMapEntry(Entry entry) {
super(entry.getKey(), entry.getValue());
}
@Override
public boolean equals(Object o) {
if (o == null || !(o instanceof Entry)) {
return false;
}
Entry me = new JavaToPyMapEntry((Entry)o);
return o.equals(me);
}
/* tojava is called in getKey and getValue so the raw key and value can be
used to create a new SimpleEntry in getEntry. */
@Override
public Object getKey() {
return AbstractDict.tojava(key);
}
@Override
public Object getValue() {
return AbstractDict.tojava(value);
}
/**
* @return an entry that returns the original values given to this entry.
*/
public Entry getEntry() {
return new SimpleEntry(key, value);
}
@Override
public int hashCode() {
return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
}
}
/**
* MapEntry Object for java MapEntry objects passed to the java.util.Set interface which is returned
* by the entrySet method of PyDictionary. Essentially like PyTojavaMapEntry, but going the other
* way converting java Objects to PyObjects.
*/
class JavaToPyMapEntry extends SimpleEntry {
public JavaToPyMapEntry(Entry entry) {
super(Py.java2py(entry.getKey()), Py.java2py(entry.getValue()));
}
}
/**
* Wrapper collection class for the keySet and values methods of java.util.Map
*/
class PyMapKeyValSet extends PyMapSet {
PyMapKeyValSet(Collection coll) {
super(coll);
}
@Override
Object toJava(Object o) {
return AbstractDict.tojava(o);
}
@Override
Object toPython(Object o) {
return Py.java2py(o);
}
}
/**
* Set wrapper for the entrySet method. Entry objects are wrapped further in JavaToPyMapEntry and
* PyToJavaMapEntry. Note - The set interface is reliable for standard objects like strings and
* integers, but may be inconsistent for other types of objects since the equals method may return
* false for Entry object that hold more elaborate PyObject types. However, we insure that this
* interface works when the Entry object originates from a Set object retrieved from a PyDictionary.
*/
class PyMapEntrySet extends PyMapSet {
PyMapEntrySet(Collection coll) {
super(coll);
}
/* We know that PyMapEntrySet will only contains entries, so if the object being passed in is
null or not an Entry, then return null which will match nothing for remove and contains
methods. */
@Override
Object toPython(Object o) {
if (o == null || !(o instanceof Entry)) {
return null;
}
if (o instanceof PyToJavaMapEntry) {
/* Use the original entry from PyDictionary */
return ((PyToJavaMapEntry)o).getEntry();
} else {
return new JavaToPyMapEntry((Entry)o);
}
}
@Override
Object toJava(Object o) {
return new PyToJavaMapEntry((Entry)o);
}
}
/**
* PyMapSet serves as a wrapper around Set Objects returned by the java.util.Map interface of
* PyDictionary. entrySet, values and keySet methods return this type for the java.util.Map
* implementation. This class is necessary as a wrapper to convert PyObjects to java Objects for
* methods that return values, and convert Objects to PyObjects for methods that take values. The
* translation is necessary to provide java access to jython dictionary objects. This wrapper also
* provides the expected backing functionality such that changes to the wrapper set or reflected in
* PyDictionary.
*/
abstract class PyMapSet extends AbstractSet {
private final Collection coll;
PyMapSet(Collection coll) {
this.coll = coll;
}
abstract Object toJava(Object obj);
abstract Object toPython(Object obj);
@Override
public int size() {
return coll.size();
}
@Override
public boolean contains(Object o) {
return coll.contains(toPython(o));
}
@Override
public boolean remove(Object o) {
return coll.remove(toPython(o));
}
@Override
public void clear() {
coll.clear();
}
/* Iterator wrapper class returned by the PyMapSet iterator
method. We need this wrapper to return PyToJavaMapEntry objects
for the 'next()' method. */
class PySetIter implements Iterator {
Iterator itr;
PySetIter(Iterator itr) {
this.itr = itr;
}
public boolean hasNext() {
return itr.hasNext();
}
public Object next() {
return toJava(itr.next());
}
public void remove() {
itr.remove();
}
}
@Override
public Iterator iterator() {
return new PySetIter(coll.iterator());
}
}