2014-09-16 10:17:13 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from jnius import autoclass, java_method, PythonJavaClass, cast
|
|
|
|
|
|
|
|
from jnius.signatures import *
|
|
|
|
|
|
|
|
JObject = autoclass('java/lang/Object')
|
|
|
|
JString = autoclass('java/lang/String')
|
|
|
|
|
|
|
|
class TestImplemIterator(PythonJavaClass):
|
|
|
|
__javainterfaces__ = [
|
|
|
|
'java/util/ListIterator', ]
|
|
|
|
|
|
|
|
def __init__(self, collection, index=0):
|
|
|
|
super(TestImplemIterator, self).__init__()
|
|
|
|
self.collection = collection
|
|
|
|
self.index = index
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(jboolean, ())
|
2014-09-16 10:17:13 +00:00
|
|
|
def hasNext(self):
|
|
|
|
return self.index < len(self.collection.data) - 1
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(JObject, ())
|
2014-09-16 10:17:13 +00:00
|
|
|
def next(self):
|
|
|
|
obj = self.collection.data[self.index]
|
|
|
|
self.index += 1
|
|
|
|
return obj
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(jboolean, ())
|
2014-09-16 10:17:13 +00:00
|
|
|
def hasPrevious(self):
|
|
|
|
return self.index >= 0
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(JObject, ())
|
2014-09-16 10:17:13 +00:00
|
|
|
def previous(self):
|
|
|
|
self.index -= 1
|
|
|
|
obj = self.collection.data[self.index]
|
|
|
|
return obj
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(jint, ())
|
2014-09-16 10:17:13 +00:00
|
|
|
def previousIndex(self):
|
|
|
|
return self.index - 1
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(JString, ())
|
2014-09-16 10:17:13 +00:00
|
|
|
def toString(self):
|
|
|
|
return repr(self)
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(JObject, (jint, ))
|
2014-09-16 10:17:13 +00:00
|
|
|
def get(self, index):
|
|
|
|
return self.collection.data[index - 1]
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(jvoid, (JObject, ))
|
2014-09-16 10:17:13 +00:00
|
|
|
def set(self, obj):
|
|
|
|
self.collection.data[self.index - 1] = obj
|
|
|
|
|
|
|
|
|
|
|
|
class TestImplem(PythonJavaClass):
|
|
|
|
__javainterfaces__ = ['java/util/List']
|
|
|
|
|
|
|
|
def __init__(self, *args):
|
|
|
|
super(TestImplem, self).__init__(*args)
|
|
|
|
self.data = list(args)
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(autoclass("java.util.Iterator"), ())
|
2014-09-16 10:17:13 +00:00
|
|
|
def iterator(self):
|
|
|
|
it = TestImplemIterator(self)
|
|
|
|
return it
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(JString, ())
|
2014-09-16 10:17:13 +00:00
|
|
|
def toString(self):
|
|
|
|
return repr(self)
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(jint, ())
|
2014-09-16 10:17:13 +00:00
|
|
|
def size(self):
|
|
|
|
return len(self.data)
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(JObject, (jint,))
|
2014-09-16 10:17:13 +00:00
|
|
|
def get(self, index):
|
|
|
|
return self.data[index]
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(JObject, (jint, JObject))
|
2014-09-16 10:17:13 +00:00
|
|
|
def set(self, index, obj):
|
|
|
|
old_object = self.data[index]
|
|
|
|
self.data[index] = obj
|
|
|
|
return old_object
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(JArray(JObject), ())
|
2014-09-16 10:17:13 +00:00
|
|
|
def toArray(self):
|
|
|
|
return self.data
|
|
|
|
|
2014-09-17 07:30:37 +00:00
|
|
|
@with_signature(autoclass("java.util.ListIterator"), ())
|
2014-09-16 10:17:13 +00:00
|
|
|
def listIterator(self):
|
|
|
|
it = TestImplemIterator(self)
|
|
|
|
return it
|
|
|
|
|
|
|
|
# TODO cover this case of listIterator.
|
|
|
|
@java_method('(I)Ljava/util/ListIterator;',
|
|
|
|
name='ListIterator')
|
|
|
|
def listIteratorI(self, index):
|
|
|
|
it = TestImplemIterator(self, index)
|
|
|
|
return it
|
|
|
|
|
|
|
|
|
|
|
|
from jnius.reflect import autoclass
|
|
|
|
|
|
|
|
class SignaturesTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_construct_stack_from_testimplem(self):
|
|
|
|
Stack = autoclass("java.util.Stack")
|
|
|
|
pyjlist = TestImplem(1, 2, 3, 4, 5, 6, 7)
|
|
|
|
stack = Stack()
|
|
|
|
stack.addAll(pyjlist)
|
|
|
|
self.assertEquals(7, pyjlist.size())
|
|
|
|
self.assertEquals(stack.size(), pyjlist.size())
|
|
|
|
array = pyjlist.toArray
|
|
|
|
|
|
|
|
|
|
|
|
|