-
Notifications
You must be signed in to change notification settings - Fork 174
/
_jcollection.py
277 lines (218 loc) · 7.14 KB
/
_jcollection.py
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
# *****************************************************************************
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# See NOTICE file for details.
#
# *****************************************************************************
import _jpype
from . import _jclass
from . import types as _jtypes
from . import _jcustomizer
from collections.abc import Mapping, Sequence, MutableSequence
JOverride = _jclass.JOverride
@_jcustomizer.JImplementationFor("java.lang.Iterable")
class _JIterable(object):
""" Customizer for ``java.util.Iterable``
This customizer adds the Python iterator syntax to classes that
implement Java Iterable.
"""
def __iter__(self):
return self.iterator()
@_jcustomizer.JImplementationFor("java.util.Collection")
class _JCollection(object):
""" Customizer for ``java.util.Collection``
This customizer adds the Python functions ``len()`` and ``del`` to
Java Collions to allow for Python syntax.
"""
def __len__(self):
return self.size()
def __delitem__(self, i):
raise TypeError(
"'%s' does not support item deletion, use remove() method" % type(self).__name__)
def __contains__(self, i):
try:
return self.contains(i)
except TypeError:
return False
def _sliceAdjust(slc, size):
start = slc.start
stop = slc.stop
if slc.step and (slc.step > 1 or slc.step < 0):
raise TypeError("Stride not supported")
if start is None:
start = 0
if stop is None:
stop = size
if start < 0:
start += size
if stop < 0:
stop += size
return slice(start, stop)
@_jcustomizer.JImplementationFor('java.util.List')
class _JList(object):
""" Customizer for ``java.util.List``
This customizer adds the Python list operator to function on classes
that implement the Java List interface.
"""
def __jclass_init__(self):
Sequence.register(self)
MutableSequence.register(self)
def __getitem__(self, ndx):
if isinstance(ndx, slice):
ndx = _sliceAdjust(ndx, self.size())
return self.subList(ndx.start, ndx.stop)
else:
if ndx < 0:
ndx += self.size()
return self.get(ndx)
def __setitem__(self, ndx, v):
if isinstance(ndx, slice):
ndx = _sliceAdjust(ndx, self.size())
self[ndx.start:ndx.stop].clear()
self.addAll(ndx.start, v)
else:
if ndx < 0:
ndx += self.size()
self.set(ndx, v)
def __delitem__(self, ndx):
if isinstance(ndx, slice):
ndx = _sliceAdjust(ndx, self.size())
self[ndx.start:ndx.stop].clear()
elif hasattr(ndx, '__index__'):
if ndx < 0:
ndx += self.size()
return self.remove_(_jtypes.JInt(ndx))
else:
raise TypeError("Incorrect arguments to del")
def __reversed__(self):
iterator = self.listIterator(self.size())
while iterator.hasPrevious():
yield iterator.previous()
def index(self, obj):
try:
return self.indexOf(obj)
except TypeError:
raise ValueError("%s is not in list" % repr(obj))
def count(self, obj):
try:
jo = _jpype.JObject(obj)
c = 0
for i in self:
if i.equals(jo):
c += 1
return c
except TypeError:
return 0
def insert(self, idx, obj):
if idx < 0:
idx += self.size()
return self.add(idx, obj)
def append(self, obj):
return self.add(obj)
def reverse(self):
_jpype.JClass("java.util.Collections").reverse(self)
def extend(self, lst):
self.addAll(lst)
def pop(self, idx=-1):
if idx < 0:
idx += self.size()
return self.remove_(_jtypes.JInt(idx))
def __iadd__(self, obj):
self.add(obj)
return self
def __add__(self, obj):
new = self.clone()
new.extend(obj)
return new
@JOverride(sticky=True, rename='remove_')
def remove(self, obj):
try:
rc = self.remove_(_jpype.JObject(obj, _jpype.JObject))
if rc is True:
return
except TypeError:
pass
raise ValueError("item not in list")
@_jcustomizer.JImplementationFor('java.util.Map')
class _JMap(object):
""" Customizer for ``java.util.Map``
This customizer adds the Python list and len operators to classes
that implement the Java Map interface.
"""
def __jclass_init__(self):
Mapping.register(self)
def __len__(self):
return self.size()
def __iter__(self):
return self.keySet().iterator()
def __delitem__(self, i):
return self.remove(i)
def __getitem__(self, ndx):
try:
item = self.get(ndx)
if item is not None or self.containsKey(ndx):
return item
except TypeError:
pass
raise KeyError('%s' % ndx)
def __setitem__(self, ndx, v):
self.put(ndx, v)
def items(self):
return self.entrySet()
def keys(self):
return list(self.keySet())
def __contains__(self, item):
try:
return self.containsKey(item)
except TypeError:
return False
@_jcustomizer.JImplementationFor('java.util.Set')
class _JSet(object):
def __delitem__(self, i):
return self.remove(i)
@_jcustomizer.JImplementationFor("java.util.Map.Entry")
class _JMapEntry(object):
def __len__(self):
return 2
def __getitem__(self, x):
if x == 0:
return self.getKey()
if x == 1:
return self.getValue()
raise IndexError("Pairs are always length 2")
@_jcustomizer.JImplementationFor('java.util.Iterator')
class _JIterator(object):
""" Customizer for ``java.util.Iterator``
This customizer adds the Python iterator concept to classes
that implement the Java Iterator interface.
"""
def __next__(self):
if self.hasNext():
return self.next()
raise StopIteration
def __iter__(self):
return self
@_jcustomizer.JImplementationFor('java.util.Enumeration')
class _JEnumeration(object):
""" Customizer for ``java.util.Enumerator``
This customizer adds the Python iterator concept to classes
that implement the Java Enumerator interface.
"""
def __next__(self):
if self.hasMoreElements():
return self.nextElement()
raise StopIteration
def __iter__(self):
return self
next = __next__