Hi,
I'm making use of the Java bindings in BaseX, with some of the functions
returning List<String> and Set<String> types.
For List<String> I can adapt that to a sequence using:
declare namespace List = "java:java.util.List";
declare function util:list-to-sequence($list) {
for $n in 0 to List:size($list) - 1
return List:get($list, $n cast as xs:int)
};
however, I'm not sure how to do the equivalent for Set<String> (or more
generally, any Iterator<T>) without converting it to a list or array first,
as Set only has size() and iterator() methods. Has anyone done this before?
The best I can come up with is the following, which relies on the size of
the set and the number of next calls in the iterator to be the same (where
it should be checking hasNext):
declare namespace Set = "java:java.util.Set";
declare namespace Iterator = "java:java.util.Iterator";
declare function util:set-to-sequence($set) {
let $iterator := Set:iterator($set)
for $n in 0 to Set:size($set) - 1
return Iterator:next($iterator)
};
More generally, it would be helpful for BaseX to have adapters for Java
arrays, Lists, Sets, Maps, and Iterables/Iterators to XQuery (XDM) types
and functions to construct them in XQuery (like my util:list-to-sequence
function above).
Kind regards,
Reece