package org.yaz4j;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import org.yaz4j.exception.ZoomException;
import org.yaz4j.jni.SWIGTYPE_p_ZOOM_scanset_p;
import org.yaz4j.jni.SWIGTYPE_p_size_t;
import org.yaz4j.jni.yaz4jlib;
-public class ScanSet {
+public class ScanSet implements Iterable<ScanTerm> {
//for GC ref-count
private Connection conn;
private SWIGTYPE_p_ZOOM_scanset_p scanSet;
private boolean disposed = false;
+ private long size = 0;
ScanSet(SWIGTYPE_p_ZOOM_scanset_p scanSet, Connection conn) {
this.scanSet = scanSet;
+ size = yaz4jlib.ZOOM_scanset_size(scanSet);
this.conn = conn;
}
}
public long getSize() {
- return yaz4jlib.ZOOM_scanset_size(scanSet);
+ return size;
}
void _dispose() {
disposed = true;
}
}
+
+ @Override
+ public Iterator<ScanTerm> iterator() {
+ return new Iterator<ScanTerm>() {
+ private long cur;
+ @Override
+ public boolean hasNext() {
+ return cur < size;
+ }
+
+ @Override
+ public ScanTerm next() {
+ return get(cur++);
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException("remove operation not supported");
+ }
+ };
+ }
}
con.close();
}
}
+
+ @Test
+ public void testConnectionScan() {
+ Connection con = new Connection("z3950.indexdata.dk:210/gils", 0);
+ assertNotNull(con);
+ try {
+ con.setSyntax("sutrs");
+ System.out.println("Open connection to z3950.indexdata.dk:210/gils...");
+ con.connect();
+ ScanSet s = con.scan(new PrefixQuery("@attr 1=4 utah"));
+ System.out.println("Scan for 'utah'...");
+ assertNotNull(s);
+ assertEquals(s.getSize(), 9);
+ ScanTerm rec = s.get(0);
+ assertNotNull(rec);
+ String term = rec.getTerm();
+ long occur = rec.getOccurences();
+ assertEquals("utah", term);
+ assertEquals(9, occur);
+ System.out.println("Read all scan terms..");
+ // read all records
+ int i = 0;
+ for (ScanTerm r : s) {
+ assertNotNull(r);
+ System.out.println("Got term "+r.getTerm());
+ i++;
+ }
+ } catch (ZoomException ze) {
+ fail(ze.getMessage());
+ } finally {
+ con.close();
+ }
+ }
@Test
public void testConnectionDeprecated() {