Recommended way to get an empty XdmSequenceIterator?

Chapman Flack <chap-JULfBfA8satPFr4CO1/[email protected]> Fri, 16 Oct 2020 19:46:27 -0400
Newsgroups gmane.text.xml.saxon.help
Message-ID <[email protected]>
Hi,

I have a simple case where ordinarily I will get an
XdmSequenceIterator<XdmItem> as the result of XQueryEvaluator.iterator(),
and pass that to a consumer method that does expect an
XdmSequenceIterator<XdmItem> (so as to be able to close it, if
the full result isn't needed).

But in another case that's detectable, I should skip the evaluator and
just pass the consumer method an empty XdmSequenceIterator<XdmItem> instead.

My first instinct was to use XdmEmptySequence.getInstance().iterator(),
but that returns a java.util.Iterator, not an XdmSequenceIterator.

I seem to have been getting away with casting that java.util.Iterator
to an XdmSequenceIterator<XdmItem>, but it seems a bit smelly to assume
that is what will be returned, and cast to an assumed generic type.

I could import EmptyIterator from net.sf.saxon.tree.iter and construct
an XdmSequenceIterator from an instance of that. But then I end up
suppressing both an unchecked-cast and a raw-type warning, because the
XdmSequenceIterator constructor returns a raw type.

So I think for now I will stick with the assumption and cast.

Maybe XdmSequenceIterator should just have an emptyIterator() method of
its own?

Or perhaps a generic

static <T extends XdmItem>
  XdmSequenceIterator<T> of(java.util.Iterator<T> iter)

that just returns an appropriately-typed XdmSequenceIterator whose close()
does nothing, from an arbitrary java.util.Iterator?  It could internally
just be a cast, if the argument is already an XdmSequenceIterator.

Regards,
-Chap