I am working in clojure with a java class that provides a search API for a binary file of a domain containing a series of records.
The java class is initialized by the file and then provides a method .querythat returns an instance of the inner class that has only one method .next, so it does not play well with the regular java API. Neither the outer nor the inner class implements any interface.
A method .querycan return null instead of an inner class. The method .nextreturns a record string or null, if no further records are found, it can immediately return zero on the first call.
How do I make this java-API work well from clojure without writing additional Java classes?
The best I could come up with was:
(defn get-records
[file query-params]
(let [tr (JavaCustomFileReader. file)]
(if-let [inner-iter (.query tr query-params)] ; .query may return null
(loop [it inner-iter
results []]
(if-let [record (.next it)]
(recur it (conj results record))
results))
[])))
This gives me a vector of results for working with clojure seq abstractions. Are there other ways to set seq from the java API, either using lazy-seq or using protocols?
source
share