-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathPyDictionary.java
More file actions
1233 lines (1059 loc) · 38.4 KB
/
PyDictionary.java
File metadata and controls
1233 lines (1059 loc) · 38.4 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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) Corporation for National Research Initiatives
* Copyright (c) Jython Developers
*/
package org.python.core;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import org.python.core.AbstractDict.ValuesIter;
import org.python.core.AbstractDict.KeysIter;
import org.python.core.AbstractDict.ItemsIter;
import org.python.expose.ExposedClassMethod;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
import org.python.expose.MethodType;
import org.python.util.Generic;
/**
* A builtin python dictionary.
*/
@ExposedType(name = "dict", base = PyObject.class, doc = BuiltinDocs.dict_doc)
public class PyDictionary extends AbstractDict implements ConcurrentMap, Traverseproc {
public static final PyType TYPE = PyType.fromClass(PyDictionary.class);
{
/* Ensure dict is not Hashable */
TYPE.object___setattr__("__hash__", Py.None);
}
private final ConcurrentMap<PyObject, PyObject> internalMap;
public ConcurrentMap<PyObject, PyObject> getMap() {
return internalMap;
}
/**
* Create an empty dictionary.
*/
public PyDictionary() {
this(TYPE);
}
/**
* Create a dictionary of type with the specified initial capacity.
*/
public PyDictionary(PyType type, int capacity) {
super(type);
internalMap = new ConcurrentHashMap<PyObject,PyObject>(capacity, Generic.CHM_LOAD_FACTOR,
Generic.CHM_CONCURRENCY_LEVEL);
}
/**
* For derived types
*/
public PyDictionary(PyType type) {
super(type);
internalMap = Generic.concurrentMap();
}
/**
* Create a new dictionary which is based on given map.
*/
public PyDictionary(Map<PyObject, PyObject> map) {
this(TYPE, map);
}
public PyDictionary(ConcurrentMap<PyObject, PyObject> backingMap, boolean useBackingMap) {
super(TYPE);
internalMap = backingMap;
}
public PyDictionary(PyType type, ConcurrentMap<PyObject, PyObject> backingMap, boolean useBackingMap) {
super(type);
internalMap = backingMap;
}
/**
* Create a new dictionary which is populated with entries the given map.
*/
public PyDictionary(PyType type, Map<PyObject, PyObject> map) {
this(type, Math.max((int) (map.size() / Generic.CHM_LOAD_FACTOR) + 1,
Generic.CHM_INITIAL_CAPACITY));
getMap().putAll(map);
}
/**
* Create a new dictionary without initializing table. Used for dictionary
* factories, with different backing maps, at the cost that it prevents us from making table be final.
*/
/* TODO we may want to revisit this API, but our chain calling of super makes this tough */
protected PyDictionary(PyType type, boolean initializeBacking) {
super(type);
if (initializeBacking) {
internalMap = Generic.concurrentMap();
} else {
internalMap = null; /* for later initialization */
}
}
/**
* Create a new dictionary with the element as content.
*
* @param elements
* The initial elements that is inserted in the dictionary. Even numbered elements
* are keys, odd numbered elements are values.
*/
public PyDictionary(PyObject elements[]) {
this();
ConcurrentMap<PyObject, PyObject> map = getMap();
for (int i = 0; i < elements.length; i += 2) {
map.put(elements[i], elements[i + 1]);
}
}
@ExposedMethod(doc = BuiltinDocs.dict___init___doc)
@ExposedNew
protected final void dict___init__(PyObject[] args, String[] keywords) {
updateCommon(args, keywords, "dict");
}
public static PyObject fromkeys(PyObject keys) {
return fromkeys(keys, Py.None);
}
public static PyObject fromkeys(PyObject keys, PyObject value) {
return dict_fromkeys(TYPE, keys, value);
}
@ExposedClassMethod(defaults = "Py.None", doc = BuiltinDocs.dict_fromkeys_doc)
static PyObject dict_fromkeys(PyType type, PyObject keys, PyObject value) {
PyObject d = type.__call__();
for (PyObject o : keys.asIterable()) {
d.__setitem__(o, value);
}
return d;
}
@Override
public int __len__() {
return dict___len__();
}
@ExposedMethod(doc = BuiltinDocs.dict___len___doc)
final int dict___len__() {
return getMap().size();
}
@Override
public boolean __nonzero__() {
return getMap().size() != 0;
}
@Override
public PyObject __finditem__(int index) {
throw Py.TypeError("loop over non-sequence");
}
@Override
public PyObject __finditem__(PyObject key) {
return getMap().get(key);
}
@ExposedMethod(doc = BuiltinDocs.dict___getitem___doc)
protected final PyObject dict___getitem__(PyObject key) {
PyObject result = getMap().get(key);
if (result != null) {
return result;
}
/* Look up __missing__ method if we're a subclass. */
PyType type = getType();
if (type != TYPE) {
PyObject missing = type.lookup("__missing__");
if (missing != null) {
return missing.__get__(this, type).__call__(key);
}
}
throw Py.KeyError(key);
}
@Override
public void __setitem__(PyObject key, PyObject value) {
dict___setitem__(key, value);
}
@ExposedMethod(doc = BuiltinDocs.dict___setitem___doc)
final void dict___setitem__(PyObject key, PyObject value) {
getMap().put(key, value);
}
@Override
public void __delitem__(PyObject key) {
dict___delitem__(key);
}
@ExposedMethod(doc = BuiltinDocs.dict___delitem___doc)
final void dict___delitem__(PyObject key) {
Object ret = getMap().remove(key);
if (ret == null) {
throw Py.KeyError(key.toString());
}
}
@Override
public PyObject __iter__() {
return dict___iter__();
}
@ExposedMethod(doc = BuiltinDocs.dict___iter___doc)
final PyObject dict___iter__() {
return iterkeys();
}
@Override
public String toString() {
return dict_toString();
}
@ExposedMethod(names = {"__repr__", "__str__"}, doc = BuiltinDocs.dict___str___doc)
final String dict_toString() {
ThreadState ts = Py.getThreadState();
if (!ts.enterRepr(this)) {
return "{...}";
}
StringBuilder buf = new StringBuilder("{");
for (Entry<PyObject, PyObject> entry : getMap().entrySet()) {
buf.append((entry.getKey()).__repr__().toString());
buf.append(": ");
buf.append((entry.getValue()).__repr__().toString());
buf.append(", ");
}
if (buf.length() > 1) {
buf.delete(buf.length() - 2, buf.length());
}
buf.append("}");
ts.exitRepr(this);
return buf.toString();
}
@Override
public PyObject __eq__(PyObject otherObj) {
return dict___eq__(otherObj);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.dict___eq___doc)
final PyObject dict___eq__(PyObject otherObj) {
PyType thisType = getType();
PyType otherType = otherObj.getType();
if (otherType != thisType && !thisType.isSubType(otherType)
&& !otherType.isSubType(thisType) || otherType == PyObject.TYPE) {
return null;
}
PyDictionary other = (PyDictionary)otherObj;
int an = getMap().size();
int bn = other.getMap().size();
if (an != bn) {
return Py.False;
}
PyList akeys = keys();
for (int i = 0; i < an; i++) {
PyObject akey = akeys.pyget(i);
PyObject bvalue = other.__finditem__(akey);
if (bvalue == null) {
return Py.False;
}
PyObject avalue = __finditem__(akey);
if (!avalue._eq(bvalue).__nonzero__()) {
return Py.False;
}
}
return Py.True;
}
@Override
public PyObject __ne__(PyObject otherObj) {
return dict___ne__(otherObj);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.dict___ne___doc)
final PyObject dict___ne__(PyObject otherObj) {
PyObject eq_result = __eq__(otherObj);
if (eq_result == null) {
return null;
}
return eq_result.__not__();
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.dict___lt___doc)
final PyObject dict___lt__(PyObject otherObj) {
int result = __cmp__(otherObj);
if (result == -2) {
return null;
}
return result < 0 ? Py.True : Py.False;
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.dict___gt___doc)
final PyObject dict___gt__(PyObject otherObj) {
int result = __cmp__(otherObj);
if (result == -2) {
return null;
}
return result > 0 ? Py.True : Py.False;
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.dict___le___doc)
final PyObject dict___le__(PyObject otherObj) {
int result = __cmp__(otherObj);
if (result == -2) {
return null;
}
return result <= 0 ? Py.True : Py.False;
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.dict___ge___doc)
final PyObject dict___ge__(PyObject otherObj) {
int result = __cmp__(otherObj);
if (result == -2) {
return null;
}
return result >= 0 ? Py.True : Py.False;
}
@Override
public int __cmp__(PyObject otherObj) {
return dict___cmp__(otherObj);
}
@ExposedMethod(type = MethodType.CMP, doc = BuiltinDocs.dict___cmp___doc)
final int dict___cmp__(PyObject otherObj) {
PyType thisType = getType();
PyType otherType = otherObj.getType();
if (otherType != thisType && !thisType.isSubType(otherType)
&& !otherType.isSubType(thisType) || otherType == PyObject.TYPE) {
return -2;
}
PyDictionary other = (PyDictionary)otherObj;
int an = getMap().size();
int bn = other.getMap().size();
if (an < bn) {
return -1;
}
if (an > bn) {
return 1;
}
PyList akeys = keys();
PyList bkeys = other.keys();
akeys.sort();
bkeys.sort();
for (int i = 0; i < bn; i++) {
PyObject akey = akeys.pyget(i);
PyObject bkey = bkeys.pyget(i);
int c = akey._cmp(bkey);
if (c != 0) {
return c;
}
PyObject avalue = __finditem__(akey);
PyObject bvalue = other.__finditem__(bkey);
if (avalue == null) {
if (bvalue == null) {
continue;
}
return -3;
} else if (bvalue == null) {
return -3;
}
c = avalue._cmp(bvalue);
if (c != 0) {
return c;
}
}
return 0;
}
/**
* Return true if the key exist in the dictionary.
*/
public boolean has_key(PyObject key) {
return dict_has_key(key);
}
@ExposedMethod(doc = BuiltinDocs.dict_has_key_doc)
final boolean dict_has_key(PyObject key) {
return getMap().containsKey(key);
}
@Override
public boolean __contains__(PyObject o) {
return dict___contains__(o);
}
@ExposedMethod(doc = BuiltinDocs.dict___contains___doc)
final boolean dict___contains__(PyObject o) {
return dict_has_key(o);
}
/**
* Return this[key] if the key exists in the mapping, defaultObj is returned
* otherwise.
*
* @param key the key to lookup in the dictionary.
* @param defaultObj the value to return if the key does not exists in the mapping.
*/
public PyObject get(PyObject key, PyObject defaultObj) {
return dict_get(key, defaultObj);
}
@ExposedMethod(defaults = "Py.None", doc = BuiltinDocs.dict_get_doc)
final PyObject dict_get(PyObject key, PyObject defaultObj) {
PyObject o = getMap().get(key);
return o == null ? defaultObj : o;
}
/**
* Return this[key] if the key exists in the mapping, None
* is returned otherwise.
*
* @param key the key to lookup in the dictionary.
*/
public PyObject get(PyObject key) {
return dict_get(key, Py.None);
}
/**
* Return a shallow copy of the dictionary.
*/
public PyDictionary copy() {
return dict_copy();
}
@ExposedMethod(doc = BuiltinDocs.dict_copy_doc)
final PyDictionary dict_copy() {
return new PyDictionary(getMap()); /* no need to clone() */
}
/**
* Remove all items from the dictionary.
*/
public void clear() {
dict_clear();
}
@ExposedMethod(doc = BuiltinDocs.dict_clear_doc)
final void dict_clear() {
getMap().clear();
}
/**
* Insert all the key:value pairs from <code>d</code> into
* this dictionary.
*/
public void update(PyObject other) {
dict_update(new PyObject[] {other}, Py.NoKeywords);
}
@ExposedMethod(doc = BuiltinDocs.dict_update_doc)
final void dict_update(PyObject[] args, String[] keywords) {
updateCommon(args, keywords, "update");
}
public void updateCommon(PyObject[] args, String[] keywords, String methName) {
int nargs = args.length - keywords.length;
if (nargs > 1) {
throw PyBuiltinCallable.DefaultInfo.unexpectedCall(nargs, false, methName, 0, 1);
}
if (nargs == 1) {
PyObject arg = args[0];
Object proxy = arg.getJavaProxy();
if (proxy instanceof Map) {
merge((Map)proxy);
}
else if (arg.__findattr__("keys") != null) {
merge(arg);
} else {
mergeFromSeq(arg);
}
}
for (int i = 0; i < keywords.length; i++) {
dict___setitem__(Py.newString(keywords[i]), args[nargs + i]);
}
}
private void merge(Map<Object, Object> other) {
for (Entry<Object, Object> entry : other.entrySet()) {
dict___setitem__(Py.java2py(entry.getKey()), Py.java2py(entry.getValue()));
}
}
/**
* Merge another PyObject that supports keys() with this
* dict.
*
* @param other a PyObject with a keys() method
*/
private void merge(PyObject other) {
if (other instanceof PyDictionary) {
getMap().putAll(((PyDictionary) other).getMap());
} else if (other instanceof PyStringMap) {
mergeFromKeys(other, ((PyStringMap)other).keys());
} else {
mergeFromKeys(other, other.invoke("keys"));
}
}
/**
* Merge another PyObject that supports keys() with this
* dict.
*
* @param other a PyObject with a keys() method
* @param override if true, the value from other is used on key-collision
*/
public void merge(PyObject other, boolean override) {
synchronized(internalMap) {
if (override) {
merge(other);
} else {
if (other instanceof PyDictionary) {
Set<Map.Entry<PyObject, PyObject>> entrySet =
((PyDictionary)other).internalMap.entrySet();
for (Map.Entry<PyObject, PyObject> ent: entrySet) {
if (!internalMap.containsKey(ent.getKey())) {
internalMap.put(ent.getKey(), ent.getValue());
}
}
} else if (other instanceof PyStringMap) {
mergeFromKeys(other, ((PyStringMap)other).keys(), override);
} else {
mergeFromKeys(other, other.invoke("keys"), override);
}
}
}
}
/**
* Merge another PyObject via its keys() method
*
* @param other a PyObject with a keys() method
* @param keys the result of other's keys() method
*/
private void mergeFromKeys(PyObject other, PyObject keys) {
for (PyObject key : keys.asIterable()) {
dict___setitem__(key, other.__getitem__(key));
}
}
/**
* Merge another PyObject via its keys() method
*
* @param other a PyObject with a keys() method
* @param keys the result of other's keys() method
* @param override if true, the value from other is used on key-collision
*/
public void mergeFromKeys(PyObject other, PyObject keys, boolean override) {
synchronized(internalMap) {
if (override) {
mergeFromKeys(other, keys);
} else {
for (PyObject key : keys.asIterable()) {
if (!dict___contains__(key)) {
dict___setitem__(key, other.__getitem__(key));
}
}
}
}
}
/**
* Merge any iterable object producing iterable objects of length
* 2 into this dict.
*
* @param other another PyObject
*/
private void mergeFromSeq(PyObject other) {
PyObject pairs = other.__iter__();
PyObject pair;
for (int i = 0; (pair = pairs.__iternext__()) != null; i++) {
try {
pair = PySequence.fastSequence(pair, "");
} catch(PyException pye) {
if (pye.match(Py.TypeError)) {
throw Py.TypeError(String.format("cannot convert dictionary update sequence "
+ "element #%d to a sequence", i));
}
throw pye;
}
int n;
if ((n = pair.__len__()) != 2) {
throw Py.ValueError(String.format("dictionary update sequence element #%d "
+ "has length %d; 2 is required", i, n));
}
dict___setitem__(pair.__getitem__(0), pair.__getitem__(1));
}
}
/**
* Merge any iterable object producing iterable objects of length
* 2 into this dict.
*
* @param other another PyObject
* @param override if true, the value from other is used on key-collision
*/
public void mergeFromSeq(PyObject other, boolean override) {
synchronized(internalMap) {
if (override) {
mergeFromSeq(other);
} else {
PyObject pairs = other.__iter__();
PyObject pair;
for (int i = 0; (pair = pairs.__iternext__()) != null; i++) {
try {
pair = PySequence.fastSequence(pair, "");
} catch(PyException pye) {
if (pye.match(Py.TypeError)) {
throw Py.TypeError(String.format("cannot convert dictionary update sequence "
+ "element #%d to a sequence", i));
}
throw pye;
}
int n;
if ((n = pair.__len__()) != 2) {
throw Py.ValueError(String.format("dictionary update sequence element #%d "
+ "has length %d; 2 is required", i, n));
}
if (!dict___contains__(pair.__getitem__(0))) {
dict___setitem__(pair.__getitem__(0), pair.__getitem__(1));
}
}
}
}
}
/**
* Return this[key] if the key exist, otherwise insert key with
* a None value and return None.
*
* @param key the key to lookup in the dictionary.
*/
public PyObject setdefault(PyObject key) {
return dict_setdefault(key, Py.None);
}
/**
* Return this[key] if the key exist, otherwise insert key with
* the value of failobj and return failobj
*
* @param key the key to lookup in the dictionary.
* @param failobj the default value to insert in the dictionary
* if key does not already exist.
*/
public PyObject setdefault(PyObject key, PyObject failobj) {
return dict_setdefault(key, failobj);
}
@ExposedMethod(defaults = "Py.None", doc = BuiltinDocs.dict_setdefault_doc)
final PyObject dict_setdefault(PyObject key, PyObject failobj) {
PyObject oldValue = getMap().putIfAbsent(key, failobj);
return oldValue == null ? failobj : oldValue;
}
/* XXX: needs __doc__ but CPython does not define setifabsent */
@ExposedMethod(defaults = "Py.None")
final PyObject dict_setifabsent(PyObject key, PyObject failobj) {
PyObject oldValue = getMap().putIfAbsent(key, failobj);
return oldValue == null ? Py.None : oldValue;
}
/**
* Return a value based on key
* from the dictionary.
*/
public PyObject pop(PyObject key) {
return dict_pop(key, null);
}
/**
* Return a value based on key
* from the dictionary or default if that key is not found.
*/
public PyObject pop(PyObject key, PyObject defaultValue) {
return dict_pop(key, defaultValue);
}
@ExposedMethod(defaults = "null", doc = BuiltinDocs.dict_pop_doc)
final PyObject dict_pop(PyObject key, PyObject defaultValue) {
if (!getMap().containsKey(key)) {
if (defaultValue == null) {
throw Py.KeyError(key);
}
return defaultValue;
}
return getMap().remove(key);
}
/**
* Return a random (key, value) tuple pair and remove the pair
* from the dictionary.
*/
public PyObject popitem() {
return dict_popitem();
}
@ExposedMethod(doc = BuiltinDocs.dict_popitem_doc)
final PyObject dict_popitem() {
Iterator<Entry<PyObject, PyObject>> it = getMap().entrySet().iterator();
if (!it.hasNext()) {
throw Py.KeyError("popitem(): dictionary is empty");
}
Entry<PyObject, PyObject> entry = it.next();
PyTuple tuple = new PyTuple(entry.getKey(), entry.getValue());
it.remove();
return tuple;
}
/**
* Return a copy of the dictionary's list of (key, value) tuple
* pairs.
*/
public PyList items() {
return dict_items();
}
@ExposedMethod(doc = BuiltinDocs.dict_items_doc)
final PyList dict_items() {
List<PyObject> list = new ArrayList<PyObject>(getMap().size());
for (Entry<PyObject, PyObject> entry : getMap().entrySet()) {
list.add(new PyTuple(entry.getKey(), entry.getValue()));
}
return PyList.fromList(list);
}
/**
* Return a copy of the dictionary's list of keys.
*/
public PyList keys() {
return dict_keys();
}
@ExposedMethod(doc = BuiltinDocs.dict_keys_doc)
final PyList dict_keys() {
return PyList.fromList(new ArrayList<PyObject>(getMap().keySet()));
}
@ExposedMethod(doc = BuiltinDocs.dict_values_doc)
final PyList dict_values() {
return PyList.fromList(new ArrayList<PyObject>(getMap().values()));
}
/**
* Returns an iterator over (key, value) pairs.
*/
public PyObject iteritems() {
return dict_iteritems();
}
@ExposedMethod(doc = BuiltinDocs.dict_iteritems_doc)
final PyObject dict_iteritems() {
return new ItemsIter<PyObject>(getMap().entrySet());
}
/**
* Returns an iterator over the dictionary's keys.
*/
public PyObject iterkeys() {
return dict_iterkeys();
}
@ExposedMethod(doc = BuiltinDocs.dict_iterkeys_doc)
final PyObject dict_iterkeys() {
return new KeysIter<PyObject>(getMap().keySet());
}
/**
* Returns an iterator over the dictionary's values.
*/
public PyObject itervalues() {
return dict_itervalues();
}
@ExposedMethod(doc = BuiltinDocs.dict_itervalues_doc)
final PyObject dict_itervalues() {
return new ValuesIter(getMap().values());
}
@Override
public int hashCode() {
return dict___hash__();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof PyDictionary) {
return ((PyDictionary) obj).getMap().equals(getMap());
} else if (obj instanceof Map) {
return getMap().equals((Map) obj);
}
return false;
}
@ExposedMethod(doc = BuiltinDocs.dict___hash___doc)
final int dict___hash__() {
throw Py.TypeError(String.format("unhashable type: '%.200s'", getType().fastGetName()));
}
@Override
public boolean isMappingType() {
return true;
}
@Override
public boolean isSequenceType() {
return false;
}
/**
* Returns a dict_keys on the dictionary's keys
*/
@ExposedMethod(doc = BuiltinDocs.dict_viewkeys_doc)
public PyObject viewkeys() {
return super.viewkeys();
}
/**
* Returns a dict_items on the dictionary's items
*/
@ExposedMethod(doc = BuiltinDocs.dict_viewitems_doc)
public PyObject viewitems() {
return super.viewitems();
}
/**
* Returns a dict_values on the dictionary's values
*/
@ExposedMethod(doc = BuiltinDocs.dict_viewvalues_doc)
public PyObject viewvalues() {
return super.viewvalues();
}
public Set<PyObject> pyKeySet() {
return internalMap.keySet();
}
/*
* The following methods implement the java.util.Map interface which allows PyDictionary to be
* passed to java methods that take java.util.Map as a parameter. Basically, the Map methods are
* a wrapper around the PyDictionary's Map container stored in member variable 'table'. These
* methods convert java Object to PyObjects on insertion, and PyObject to Objects on retrieval.
*/
/** @see java.util.Map#entrySet() */
public Set entrySet() {
return new PyMapEntrySet(getMap().entrySet());
}
/** @see java.util.Map#keySet() */
public Set keySet() {
return new PyMapKeyValSet(getMap().keySet());
}
/** @see java.util.Map#values() */
public Collection values() {
return new PyMapKeyValSet(getMap().values());
}
/** @see java.util.Map#putAll(Map map) */
public void putAll(Map map) {
for (Object o : map.entrySet()) {
Entry entry = (Entry)o;
getMap().put(Py.java2py(entry.getKey()), Py.java2py(entry.getValue()));
}
}
/** @see java.util.Map#remove(Object key) */
public Object remove(Object key) {
return tojava(getMap().remove(Py.java2py(key)));
}
/** @see java.util.Map#put(Object key, Object value) */
public Object put(Object key, Object value) {
return tojava(getMap().put(Py.java2py(key), Py.java2py(value)));
}
/** @see java.util.Map#get(Object key) */
public Object get(Object key) {
return tojava(getMap().get(Py.java2py(key)));
}
/** @see java.util.Map#containsValue(Object key) */
public boolean containsValue(Object value) {
return getMap().containsValue(Py.java2py(value));
}
/** @see java.util.Map#containsValue(Object key) */
public boolean containsKey(Object key) {
return getMap().containsKey(Py.java2py(key));
}
/** @see java.util.Map#isEmpty() */
public boolean isEmpty() {
return getMap().isEmpty();
}
/** @see java.util.Map#size() */
public int size() {
return getMap().size();
}
public Object putIfAbsent(Object key, Object value) {
return tojava(getMap().putIfAbsent(Py.java2py(key), Py.java2py(value)));
}
public boolean remove(Object key, Object value) {
return getMap().remove(Py.java2py(key), Py.java2py(value));
}
public boolean replace(Object key, Object oldValue, Object newValue) {
return getMap().replace(Py.java2py(key), Py.java2py(oldValue), Py.java2py(newValue));
}
public Object replace(Object key, Object value) {
return tojava(getMap().replace(Py.java2py(key), Py.java2py(value)));
}
@ExposedType(name = "dict_values", base = PyObject.class, doc = "")
static class PyDictionaryViewValues extends BaseDictionaryView {
public final PyType TYPE = PyType.fromClass(PyDictionaryViewValues.class);
public PyDictionaryViewValues(AbstractDict dvDict) {
super(dvDict);
}
@Override
public PyObject __iter__() {
return dict_values___iter__();
}
@ExposedMethod(doc = BuiltinDocs.set___iter___doc)
final PyObject dict_values___iter__() {
return new ValuesIter(dvDict.getMap().values());
}
@ExposedMethod(doc = BuiltinDocs.set___len___doc)
final int dict_values___len__() {
return dict_view___len__();
}
@ExposedMethod(names = {"__repr__", "__str__"}, doc = BuiltinDocs.set___str___doc)
final String dict_values_toString() {
return dict_view_toString();
}
}
@ExposedType(name = "dict_keys", base = PyObject.class)
static class PyDictionaryViewKeys extends BaseDictionaryView {
public final PyType TYPE = PyType.fromClass(PyDictionaryViewKeys.class);
public PyDictionaryViewKeys(AbstractDict dvDict) {
super(dvDict);
}
@Override
public PyObject __iter__() {
return dict_keys___iter__();
}
@ExposedMethod(doc = BuiltinDocs.set___iter___doc)
final PyObject dict_keys___iter__() {
return new KeysIter<PyObject>(dvDict.pyKeySet());
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___ne___doc)
final PyObject dict_keys___ne__(PyObject otherObj) {
return dict_view___ne__(otherObj);
}