Skip to content

Commit e261832

Browse files
authored
Merge pull request #55 from tanmaykm/tan/misc
implement `convert` for `ArrayList`
2 parents 443dcc8 + 2af8a21 commit e261832

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/convert.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ end
162162

163163
convert{X,Y}(::Type{@jimport(java.util.Map)}, K::Type{JavaObject{X}}, V::Type{JavaObject{Y}}, x::Dict) = convert(@jimport(java.util.Map), convert(@jimport(java.util.HashMap), K, V, x))
164164

165+
function convert{X}(::Type{@jimport(java.util.ArrayList)}, x::Vector, V::Type{JavaObject{X}}=JObject)
166+
ArrayList = @jimport(java.util.ArrayList)
167+
a = ArrayList(())
168+
for v in x
169+
jcall(a, "add", jboolean, (JObject,), convert(V, v))
170+
end
171+
return a
172+
end
173+
174+
convert{X}(::Type{@jimport(java.util.List)}, x::Vector, V::Type{JavaObject{X}}=JObject) = convert(@jimport(java.util.ArrayList), x, V)
165175

166176
# Convert a reference to a java.lang.String into a Julia string. Copies the underlying byte buffer
167177
function unsafe_string(jstr::JString) #jstr must be a jstring obtained via a JNI call

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ a=@compat Dict("a"=>"A", "b"=>"B")
9595
b=convert(@jimport(java.util.Map), JString, JString, a)
9696
@assert jcall(b, "size", jint, ()) == 2
9797

98+
# test for ArrayList conversion
99+
JArrayList = @jimport(java.util.ArrayList)
100+
p = JArrayList(())
101+
a = ["hello", " ", "world"]
102+
b = convert(@jimport(java.util.ArrayList), a, JString)
103+
@assert jcall(b, "size", jint, ()) == 3
104+
98105
#Inner Classes
99106
TestInner = @jimport(Test$TestInner)
100107
Test = @jimport(Test)

0 commit comments

Comments
 (0)