3d89bd7495b3b750aed389efa24b72b98887c94d
[yaz4j-moved-to-github.git] / src / main / java / org / yaz4j / ResultSet.java
1 package org.yaz4j;
2
3 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_record_p;
4 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_resultset_p;
5 import org.yaz4j.jni.yaz4jlib;
6
7 public class ResultSet {
8     //for GC refcount
9     private Connection conn;
10     private SWIGTYPE_p_ZOOM_resultset_p resultSet;
11     private long size = 0;
12     private boolean disposed = false;
13
14     ResultSet(SWIGTYPE_p_ZOOM_resultset_p resultSet, Connection conn) {
15         this.resultSet = resultSet;
16         size = yaz4jlib.ZOOM_resultset_size(this.resultSet);
17         this.conn = conn;
18     }
19
20     @Override
21     public void finalize() {
22         this._dispose();
23     }
24
25     /**
26      * Read option by name.
27      * @param name option name
28      * @return option value
29      */
30     public String option(String name) {
31       return yaz4jlib.ZOOM_resultset_option_get(resultSet, name);
32     }
33
34     /**
35      * Write option with a given name.
36      * @param name option name
37      * @param value option value
38      * @return result set (self) for chainability
39      */
40     public ResultSet option(String name, String value) {
41       yaz4jlib.ZOOM_resultset_option_set(resultSet, name, value);
42       return this;
43     }
44
45     public Record getRecord(int index) {
46       SWIGTYPE_p_ZOOM_record_p record = 
47         yaz4jlib.ZOOM_resultset_record(resultSet, index);
48       return new Record(record, this);
49     }
50
51     public long getSize() {
52         return size;
53     }
54
55     void _dispose() {
56         if (!disposed) {
57             yaz4jlib.ZOOM_resultset_destroy(resultSet);
58             resultSet = null;
59             conn = null;
60             disposed = true;
61         }
62     }
63 }