From 1d166b5f383474099fe09d4b7e0fff229b6e50e8 Mon Sep 17 00:00:00 2001 From: Jakub Skoczen Date: Wed, 2 Apr 2014 01:06:29 +0200 Subject: [PATCH] ScanSet is iterable --- src/main/java/org/yaz4j/ScanSet.java | 30 +++++++++++++++++++++++++++-- src/test/org/yaz4j/ConnectionTest.java | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/yaz4j/ScanSet.java b/src/main/java/org/yaz4j/ScanSet.java index 2ba8dd7..0df08a5 100644 --- a/src/main/java/org/yaz4j/ScanSet.java +++ b/src/main/java/org/yaz4j/ScanSet.java @@ -1,18 +1,23 @@ 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 { //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; } @@ -31,7 +36,7 @@ public class ScanSet { } public long getSize() { - return yaz4jlib.ZOOM_scanset_size(scanSet); + return size; } void _dispose() { @@ -42,4 +47,25 @@ public class ScanSet { disposed = true; } } + + @Override + public Iterator iterator() { + return new Iterator() { + 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"); + } + }; + } } diff --git a/src/test/org/yaz4j/ConnectionTest.java b/src/test/org/yaz4j/ConnectionTest.java index 56198f0..4459b30 100644 --- a/src/test/org/yaz4j/ConnectionTest.java +++ b/src/test/org/yaz4j/ConnectionTest.java @@ -51,6 +51,39 @@ public class ConnectionTest { 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() { -- 1.7.10.4