X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fyaz4j%2FScanSet.java;h=0df08a592bb803302a17f36d4e5645214c0aeabd;hb=eb4350ae0b70918ff4e82456b3f37b07ead53682;hp=2140cc54f2a9c2304c40b4d68fff95327be9066c;hpb=2def7bc64faa1dfb9f3393f2c2ef0bca0c6265e1;p=yaz4j-moved-to-github.git diff --git a/src/main/java/org/yaz4j/ScanSet.java b/src/main/java/org/yaz4j/ScanSet.java index 2140cc5..0df08a5 100644 --- a/src/main/java/org/yaz4j/ScanSet.java +++ b/src/main/java/org/yaz4j/ScanSet.java @@ -1,45 +1,71 @@ 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_int; import org.yaz4j.jni.SWIGTYPE_p_size_t; import org.yaz4j.jni.yaz4jlib; -public class ScanSet { +public class ScanSet implements Iterable { - private SWIGTYPE_p_ZOOM_scanset_p scanSet = null; - private Connection connection; - private boolean disposed = false; + //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 connection) { - this.connection = connection; - this.scanSet = scanSet; - } + ScanSet(SWIGTYPE_p_ZOOM_scanset_p scanSet, Connection conn) { + this.scanSet = scanSet; + size = yaz4jlib.ZOOM_scanset_size(scanSet); + this.conn = conn; + } - public void finalize() { - dispose(); - } + public void finalize() { + _dispose(); + } - public ScanTerm get(long index) { - SWIGTYPE_p_size_t occ = yaz4jlib.new_size_tp(); - SWIGTYPE_p_size_t length = yaz4jlib.new_size_tp(); - String term = yaz4jlib.ZOOM_scanset_term(scanSet, (long) index, occ, length); - long occurences = yaz4jlib.size_tp_value(occ); - yaz4jlib.delete_size_tp(occ); - yaz4jlib.delete_size_tp(length); - return new ScanTerm(term, occurences); - } + public ScanTerm get(long index) { + SWIGTYPE_p_size_t occ = yaz4jlib.new_size_tp(); + SWIGTYPE_p_size_t length = yaz4jlib.new_size_tp(); + String term = yaz4jlib.ZOOM_scanset_term(scanSet, (long) index, occ, length); + long occurences = yaz4jlib.size_tp_value(occ); + yaz4jlib.delete_size_tp(occ); + yaz4jlib.delete_size_tp(length); + return new ScanTerm(term, occurences); + } - public long getSize() { - return yaz4jlib.ZOOM_scanset_size(scanSet); - } + public long getSize() { + return size; + } - public void dispose() { - if (!disposed) { - yaz4jlib.ZOOM_scanset_destroy(scanSet); - connection = null; - scanSet = null; - disposed = true; - } + void _dispose() { + if (!disposed) { + yaz4jlib.ZOOM_scanset_destroy(scanSet); + scanSet = null; + conn = null; + 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"); + } + }; + } }