Skip to content

Commit 6682564

Browse files
authored
feat: Add j_collection_to_list() in jcompat.py (#6565)
Fixes #6515 This new function, unlike the existing j_list_to_list(), j_map_to_dict(), covers Set and can be used when the concrete collection type is unknown.
1 parent a8eea36 commit 6682564

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

py/server/deephaven/jcompat.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ def j_list_to_list(jlist) -> List[Any]:
100100
return [wrap_j_object(jlist.get(i)) for i in range(jlist.size())]
101101

102102

103+
def j_collection_to_list(jcollection) -> List[Any]:
104+
"""Converts a java Collection to a python list."""
105+
if not jcollection:
106+
return []
107+
108+
res = []
109+
it = jcollection.iterator()
110+
while it.hasNext():
111+
res.append(wrap_j_object(it.next()))
112+
113+
return res
114+
115+
103116
T = TypeVar("T")
104117
R = TypeVar("R")
105118

py/server/tests/test_jcompat.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
import unittest
66

77
from deephaven import dtypes
8-
from deephaven.jcompat import j_function, j_lambda, AutoCloseable
8+
from deephaven.jcompat import j_function, j_lambda, AutoCloseable, j_array_list, j_collection_to_list, j_hashset
99
from tests.testbase import BaseTestCase
1010

1111
import jpy
1212

1313
_JSharedContext = jpy.get_type("io.deephaven.engine.table.SharedContext")
1414

15+
1516
class JCompatTestCase(BaseTestCase):
1617
def test_j_function(self):
1718
def int_to_str(v: int) -> str:
@@ -25,6 +26,7 @@ def int_to_str(v: int) -> str:
2526
def test_j_lambda(self):
2627
def int_to_str(v: int) -> str:
2728
return str(v)
29+
2830
j_func = j_lambda(int_to_str, jpy.get_type('java.util.function.Function'), dtypes.string)
2931

3032
r = j_func.apply(10)
@@ -36,6 +38,15 @@ def test_auto_closeable(self):
3638
self.assertEqual(auto_closeable.closed, False)
3739
self.assertEqual(auto_closeable.closed, True)
3840

41+
def test_j_collection_to_list(self):
42+
lst = [2, 1, 3]
43+
j_list = j_array_list(lst)
44+
self.assertEqual(lst, j_collection_to_list(j_list))
45+
46+
s = set(lst)
47+
j_set = j_hashset(s)
48+
self.assertEqual(s, set(j_collection_to_list(j_set)))
49+
3950

4051
if __name__ == "__main__":
4152
unittest.main()

0 commit comments

Comments
 (0)