-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathJavaProxyList.java
More file actions
690 lines (635 loc) · 23.9 KB
/
JavaProxyList.java
File metadata and controls
690 lines (635 loc) · 23.9 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
package org.python.core;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.python.util.Generic;
/**
* Proxy Java objects implementing java.util.List with Python methods corresponding to the standard
* list type
*/
class JavaProxyList {
@Untraversable
private static class ListMethod extends PyBuiltinMethodNarrow {
protected ListMethod(String name, int numArgs) {
super(name, numArgs);
}
protected ListMethod(String name, int minArgs, int maxArgs) {
super(name, minArgs, maxArgs);
}
protected List asList() {
return (List) self.getJavaProxy();
}
protected List newList() {
try {
return (List) asList().getClass().getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException | SecurityException
| IllegalArgumentException e) {
throw Py.JavaError(e);
}
}
/**
* Compares this object with other to check for equality. Used to implement __eq __ and
* __ne__. May return null if the other object cannot be compared i.e. is not a Python list
* or Java List.
*
* @param other The object to compare to this
* @return true is equal, false if not equal and null if we can't compare
*/
protected PyBoolean isEqual(PyObject other) {
if (isPyList(other)) {
// Being compared to a Python list
PyList oList = (PyList) other;
List jList = asList();
if (jList.size() != oList.size()) {
// Size mismatched so not equal
return Py.False;
}
for (int i = 0; i < jList.size(); i++) {
// Do element by element comparison, if any elements are not equal return false
if (!Py.java2py(jList.get(i))._eq(oList.pyget(i)).__nonzero__()) {
return Py.False;
}
}
// All elements are equal so the lists are equal
return Py.True;
} else {
// Being compared to something that is not a Python list
Object oj = other.getJavaProxy();
if (oj instanceof List) {
// Being compared to a Java List
List oList = (List) oj;
List jList = asList();
if (jList.size() != oList.size()) {
// Size mismatched so not equal
return Py.False;
}
for (int i = 0; i < jList.size(); i++) {
/*
* Do element by element comparison, if any elements are not equal return
* false.
*/
if (!Py.java2py(jList.get(i))._eq(Py.java2py(oList.get(i))).__nonzero__()) {
return Py.False;
}
}
// All elements are equal so the lists are equal
return Py.True;
} else {
/*
* other is not a Python or Java list, so we don't know if were equal therefore
* return null.
*/
return null;
}
}
}
private boolean isPyList(PyObject object) {
return object.getType().isSubType(PyList.TYPE);
}
}
protected static class ListIndexDelegate extends SequenceIndexDelegate {
private final List list;
public ListIndexDelegate(List list) {
this.list = list;
}
@Override
public void delItem(int idx) {
list.remove(idx);
}
@Override
public PyObject getItem(int idx) {
return Py.java2py(list.get(idx));
}
@Override
public PyObject getSlice(int start, int stop, int step) {
if (step > 0 && stop < start) {
stop = start;
}
int n = PySequence.sliceLength(start, stop, step);
List newList;
try {
newList = list.getClass().getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException | SecurityException
| IllegalArgumentException e) {
throw Py.JavaError(e);
}
int j = 0;
for (int i = start; j < n; i += step) {
newList.add(list.get(i));
j++;
}
return Py.java2py(newList);
}
@Override
public String getTypeName() {
return list.getClass().getName();
}
@Override
public int len() {
return list.size();
}
protected int fixBoundIndex(PyObject index) {
PyInteger length = Py.newInteger(len());
if (index._lt(Py.Zero).__nonzero__()) {
index = index._add(length);
if (index._lt(Py.Zero).__nonzero__()) {
index = Py.Zero;
}
} else if (index._gt(length).__nonzero__()) {
index = length;
}
int i = index.asIndex();
assert i >= 0;
return i;
}
@Override
public void setItem(int idx, PyObject value) {
list.set(idx, value.__tojava__(Object.class));
}
@Override
public void setSlice(int start, int stop, int step, PyObject value) {
if (stop < start) {
stop = start;
}
if (JyAttribute.getAttr(value, JyAttribute.JAVA_PROXY_ATTR) == this.list) {
List xs = Generic.list();
xs.addAll(this.list);
setsliceList(start, stop, step, xs);
} else if (value instanceof PyList) {
setslicePyList(start, stop, step, (PyList) value);
} else {
Object valueList = value.__tojava__(List.class);
if (valueList != null && valueList != Py.NoConversion) {
setsliceList(start, stop, step, (List) valueList);
} else {
setsliceIterator(start, stop, step, value.asIterable().iterator());
}
}
}
final private void setsliceList(int start, int stop, int step, List value) {
if (step == 1) {
list.subList(start, stop).clear();
list.addAll(start, value);
} else {
int size = list.size();
Iterator<Object> iter = value.listIterator();
for (int j = start; iter.hasNext(); j += step) {
Object item = iter.next();
if (j >= size) {
list.add(item);
} else {
list.set(j, item);
}
}
}
}
final private void setsliceIterator(int start, int stop, int step, Iterator<PyObject> iter) {
if (step == 1) {
List insertion = new ArrayList();
if (iter != null) {
while (iter.hasNext()) {
insertion.add(iter.next().__tojava__(Object.class));
}
}
list.subList(start, stop).clear();
list.addAll(start, insertion);
} else {
int size = list.size();
for (int j = start; iter.hasNext(); j += step) {
Object item = iter.next().__tojava__(Object.class);
if (j >= size) {
list.add(item);
} else {
list.set(j, item);
}
}
}
}
final private void setslicePyList(int start, int stop, int step, PyList value) {
if (step == 1) {
list.subList(start, stop).clear();
int n = value.getList().size();
for (int i = 0, j = start; i < n; i++, j++) {
Object item = value.getList().get(i).__tojava__(Object.class);
list.add(j, item);
}
} else {
int size = list.size();
Iterator<PyObject> iter = value.getList().listIterator();
for (int j = start; iter.hasNext(); j += step) {
Object item = iter.next().__tojava__(Object.class);
if (j >= size) {
list.add(item);
} else {
list.set(j, item);
}
}
}
}
@Override
public void delItems(int start, int stop) {
int n = stop - start;
while (n-- > 0) {
delItem(start);
}
}
}
@Untraversable
private static class ListMulProxyClass extends ListMethod {
protected ListMulProxyClass(String name, int numArgs) {
super(name, numArgs);
}
@Override
public PyObject __call__(PyObject obj) {
List jList = asList();
int mult = obj.asInt();
List newList = null;
// anything below 0 multiplier, we return an empty list
if (mult > 0) {
try {
newList = new ArrayList(jList.size() * mult);
// otherwise, extend it x times, where x is int-cast from obj
for (; mult > 0; mult--) {
for (Object entry : jList) {
newList.add(entry);
}
}
} catch (OutOfMemoryError t) {
throw Py.MemoryError("");
}
} else {
newList = Collections.EMPTY_LIST;
}
return Py.java2py(newList);
}
}
private static class KV {
private final PyObject key;
private final Object value;
KV(PyObject key, Object value) {
this.key = key;
this.value = value;
}
}
private static class KVComparator implements Comparator<KV> {
private final PyObject cmp;
KVComparator(PyObject cmp) {
this.cmp = cmp;
}
@Override
public int compare(KV o1, KV o2) {
int result;
if (cmp != null && cmp != Py.None) {
PyObject pyresult = cmp.__call__(o1.key, o2.key);
if (pyresult instanceof PyInteger || pyresult instanceof PyLong) {
return pyresult.asInt();
} else {
throw Py.TypeError(
String.format("comparison function must return int, not %.200s",
pyresult.getType().fastGetName()));
}
} else {
result = o1.key._cmp(o2.key);
}
return result;
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof KVComparator) {
return cmp.equals(((KVComparator) o).cmp);
}
return false;
}
}
private synchronized static void list_sort(List list, PyObject cmp, PyObject key, boolean reverse) {
int size = list.size();
final ArrayList<KV> decorated = new ArrayList(size);
for (Object value : list) {
PyObject pyvalue = Py.java2py(value);
if (key == null || key == Py.None) {
decorated.add(new KV(pyvalue, value));
} else {
decorated.add(new KV(key.__call__(pyvalue), value));
}
}
// we will rebuild the list from the decorated version
list.clear();
KVComparator c = new KVComparator(cmp);
if (reverse) {
Collections.reverse(decorated); // maintain stability of sort by reversing first
}
Collections.sort(decorated, c);
if (reverse) {
Collections.reverse(decorated);
}
boolean modified = list.size() > 0;
for (KV kv : decorated) {
list.add(kv.value);
}
if (modified) {
throw Py.ValueError("list modified during sort");
}
}
private static final PyBuiltinMethodNarrow listGetProxy = new ListMethod("__getitem__", 1) {
@Override
public PyObject __call__(PyObject key) {
return new ListIndexDelegate(asList()).checkIdxAndGetItem(key);
}
};
private static final PyBuiltinMethodNarrow listSetProxy = new ListMethod("__setitem__", 2) {
@Override
public PyObject __call__(PyObject key, PyObject value) {
new ListIndexDelegate(asList()).checkIdxAndSetItem(key, value);
return Py.None;
}
};
private static final PyBuiltinMethodNarrow listRemoveProxy = new ListMethod("__delitem__", 1) {
@Override
public PyObject __call__(PyObject key) {
new ListIndexDelegate(asList()).checkIdxAndDelItem(key);
return Py.None;
}
};
private static final PyBuiltinMethodNarrow listEqProxy = new ListMethod("__eq__", 1) {
@Override
public PyObject __call__(PyObject other) {
return isEqual(other);
}
};
private static final PyBuiltinMethodNarrow listNeProxy = new ListMethod("__ne__", 1) {
@Override
public PyObject __call__(PyObject other) {
// isEqual may return null if we don't know how to compare to other.
PyBoolean equal = isEqual(other);
if (equal != null) {
// implement NOT equal by the inverse of equal
return equal.__not__();
}
return null;
}
};
private static final PyBuiltinMethodNarrow listAppendProxy = new ListMethod("append", 1) {
@Override
public PyObject __call__(PyObject value) {
asList().add(value);
return Py.None;
}
};
private static final PyBuiltinMethodNarrow listExtendProxy = new ListMethod("extend", 1) {
@Override
public PyObject __call__(PyObject obj) {
List jList = asList();
List extension = new ArrayList();
// Extra step to build the extension list is necessary
// in case of adding to oneself
for (PyObject item : obj.asIterable()) {
extension.add(item);
}
jList.addAll(extension);
return Py.None;
}
};
private static final PyBuiltinMethodNarrow listInsertProxy = new ListMethod("insert", 2) {
@Override
public PyObject __call__(PyObject index, PyObject object) {
List jlist = asList();
ListIndexDelegate lid = new ListIndexDelegate(jlist);
int idx = lid.fixBoundIndex(index);
jlist.add(idx, object);
return Py.None;
}
};
private static final PyBuiltinMethodNarrow listPopProxy = new ListMethod("pop", 0, 1) {
@Override
public PyObject __call__() {
return __call__(Py.newInteger(-1));
}
@Override
public PyObject __call__(PyObject index) {
List jlist = asList();
if (jlist.isEmpty()) {
throw Py.IndexError("pop from empty list");
}
ListIndexDelegate ldel = new ListIndexDelegate(jlist);
PyObject item = ldel.checkIdxAndFindItem(index.asInt());
if (item == null) {
throw Py.IndexError("pop index out of range");
} else {
ldel.checkIdxAndDelItem(index);
return item;
}
}
};
private static final PyBuiltinMethodNarrow listIndexProxy = new ListMethod("index", 1, 3) {
@Override
public PyObject __call__(PyObject object) {
return __call__(object, Py.newInteger(0), Py.newInteger(asList().size()));
}
@Override
public PyObject __call__(PyObject object, PyObject start) {
return __call__(object, start, Py.newInteger(asList().size()));
}
@Override
public PyObject __call__(PyObject object, PyObject start, PyObject end) {
List jlist = asList();
ListIndexDelegate lid = new ListIndexDelegate(jlist);
int start_index = lid.fixBoundIndex(start);
int end_index = lid.fixBoundIndex(end);
int i = start_index;
try {
for (ListIterator it = jlist.listIterator(start_index); it.hasNext(); i++) {
if (i == end_index) {
break;
}
Object jobj = it.next();
if (Py.java2py(jobj)._eq(object).__nonzero__()) {
return Py.newInteger(i);
}
}
} catch (ConcurrentModificationException e) {
throw Py.ValueError(object.toString() + " is not in list");
}
throw Py.ValueError(object.toString() + " is not in list");
}
};
private static final PyBuiltinMethodNarrow listCountProxy = new ListMethod("count", 1) {
@Override
public PyObject __call__(PyObject object) {
int count = 0;
List jlist = asList();
for (int i = 0; i < jlist.size(); i++) {
Object jobj = jlist.get(i);
if (Py.java2py(jobj)._eq(object).__nonzero__()) {
++count;
}
}
return Py.newInteger(count);
}
};
private static final PyBuiltinMethodNarrow listReverseProxy = new ListMethod("reverse", 0) {
@Override
public PyObject __call__() {
List jlist = asList();
Collections.reverse(jlist);
return Py.None;
}
};
private static final PyBuiltinMethodNarrow listRemoveOverrideProxy = new ListMethod("remove", 1) {
@Override
public PyObject __call__(PyObject object) {
List jlist = asList();
for (int i = 0; i < jlist.size(); i++) {
Object jobj = jlist.get(i);
if (Py.java2py(jobj)._eq(object).__nonzero__()) {
jlist.remove(i);
return Py.None;
}
}
if (object.isIndex()) {
// this op is still O(n), but with also the extra O(n) above
ListIndexDelegate ldel = new ListIndexDelegate(jlist);
ldel.checkIdxAndDelItem(object);
return Py.None;
}
throw Py.ValueError(object.toString() + " is not in list");
}
};
private static final PyBuiltinMethodNarrow listRAddProxy = new ListMethod("__radd__", 1) {
@Override
public PyObject __call__(PyObject obj) {
// first, clone the self list
List jList = asList();
List jClone;
try {
jClone = (List) jList.getClass().getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException | SecurityException
| IllegalArgumentException e) {
throw Py.JavaError(e);
}
for (Object entry : jList) {
jClone.add(entry);
}
// then, extend it with elements from the other list
// (but, since this is reverse add, we are technically
// pre-pending the clone with elements from the other list)
if (obj instanceof Collection) {
jClone.addAll(0, (Collection) obj);
} else {
int i = 0;
for (PyObject item : obj.asIterable()) {
jClone.add(i, item);
i++;
}
}
return Py.java2py(jClone);
}
};
private static final PyBuiltinMethodNarrow listIAddProxy = new ListMethod("__iadd__", 1) {
@Override
public PyObject __call__(PyObject obj) {
List jList = asList();
if (obj instanceof Collection) {
jList.addAll((Collection) obj);
} else {
for (PyObject item : obj.asIterable()) {
jList.add(item);
}
}
return self;
}
};
private static final PyBuiltinMethodNarrow listIMulProxy = new ListMethod("__imul__", 1) {
@Override
public PyObject __call__(PyObject obj) {
List jList = asList();
int mult = obj.asInt();
// anything below 0 multiplier, we clear the list
if (mult <= 0) {
jList.clear();
} else {
try {
if (jList instanceof ArrayList) {
((ArrayList) jList).ensureCapacity(jList.size() * (mult - 1));
}
// otherwise, extend it (in-place) x times, where x is int-cast from obj
int originalSize = jList.size();
for (mult = mult - 1; mult > 0; mult--) {
for (int i = 0; i < originalSize; i++) {
jList.add(jList.get(i));
}
}
} catch (OutOfMemoryError t) {
throw Py.MemoryError("");
}
}
return self;
}
};
private static final PyBuiltinMethodNarrow listSortProxy = new ListMethod("sort", 0, 3) {
@Override
public PyObject __call__() {
list_sort(asList(), Py.None, Py.None, false);
return Py.None;
}
@Override
public PyObject __call__(PyObject cmp) {
list_sort(asList(), cmp, Py.None, false);
return Py.None;
}
@Override
public PyObject __call__(PyObject cmp, PyObject key) {
list_sort(asList(), cmp, key, false);
return Py.None;
}
@Override
public PyObject __call__(PyObject cmp, PyObject key, PyObject reverse) {
list_sort(asList(), cmp, key, reverse.__nonzero__());
return Py.None;
}
@Override
public PyObject __call__(PyObject[] args, String[] kwds) {
ArgParser ap = new ArgParser("list", args, kwds, new String[]{
"cmp", "key", "reverse"}, 0);
PyObject cmp = ap.getPyObject(0, Py.None);
PyObject key = ap.getPyObject(1, Py.None);
PyObject reverse = ap.getPyObject(2, Py.False);
list_sort(asList(), cmp, key, reverse.__nonzero__());
return Py.None;
}
};
static PyBuiltinMethod[] getProxyMethods() {
return new PyBuiltinMethod[]{
listGetProxy,
listSetProxy,
listEqProxy,
listNeProxy,
listRemoveProxy,
listAppendProxy,
listExtendProxy,
listInsertProxy,
listPopProxy,
listIndexProxy,
listCountProxy,
listReverseProxy,
listRAddProxy,
listIAddProxy,
new ListMulProxyClass("__mul__", 1),
new ListMulProxyClass("__rmul__", 1),
listIMulProxy
};
}
static PyBuiltinMethod[] getPostProxyMethods() {
return new PyBuiltinMethod[]{
listSortProxy,
listRemoveOverrideProxy
};
}
}