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