Move async result cursor to AsyncConnection
[yaz4j-moved-to-github.git] / src / main / java / org / yaz4j / Record.java
index e1990a7..2793ba9 100644 (file)
@@ -1,71 +1,65 @@
 package org.yaz4j;
 
-import java.io.UnsupportedEncodingException;
-
 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_record_p;
-import org.yaz4j.jni.SWIGTYPE_p_int;
 import org.yaz4j.jni.yaz4jlib;
 
-public class Record
-{
-       private SWIGTYPE_p_ZOOM_record_p record = null ;
-       private ResultSet resultSet = null ;
-       private boolean disposed = false;       
+public class Record implements Cloneable {
+
+  private SWIGTYPE_p_ZOOM_record_p record;
+  private ResultSet rset;
+  private boolean disposed = false;
+
+  Record(SWIGTYPE_p_ZOOM_record_p record, ResultSet rset) {
+    this.record = record;
+    this.rset = rset;
+  }
+
+  protected Record(SWIGTYPE_p_ZOOM_record_p record) {
+    this.record = record;
+  }
+
+  @Override
+  public void finalize() {
+    _dispose();
+  }
+
+  public byte[] get(String type) {
+    if (type == null)
+      throw new NullPointerException("type cannot be null");
+    return yaz4jlib.ZOOM_record_get_bytes(record, type);
+  }
+
+  public String render() {
+    return new String(get("render"));
+  }
+
+  public byte[] getContent() {
+    return get("raw");
+  }
 
-       Record(SWIGTYPE_p_ZOOM_record_p record, ResultSet resultSet)
-       {
-               this.resultSet = resultSet;
-               this.record = record;
-       }
+  public String getSyntax() {
+    return new String(get("syntax"));
+  }
 
-       public void finalize()
-       {
-               Dispose();
-       }
-       
-       public byte[] getContent()
-       {
-               String type = "raw";
-               SWIGTYPE_p_int length = null ;  
-               return yaz4jlib.ZOOM_record_get_bytes(record, type, length) ;
-//             String contentString = yaz4jlib.ZOOM_record_get(record, type, length) ;
-//             System.err.println("!!!!!");
-//             System.err.println(contentString);
-//             System.err.println(contentString.length());
-//             System.err.println("!!!!!");
-//             try {
-//                     byte[] bytes = contentString.getBytes("UTF8");
-//                     System.err.println(bytes.length);
-//                     return bytes ;
-//             } catch (UnsupportedEncodingException e) {
-//                     throw new RuntimeException(e);
-//             }
-       }
+  public String getDatabase() {
+    return new String(get("database"));
+  }
 
-       public String getSyntax()
-       {
-               String type = "syntax";
-               SWIGTYPE_p_int length = null ;          
-               String syntax = yaz4jlib.ZOOM_record_get(record, type, length);
-               return syntax ;
-       }
-       
-       public String getDatabase()
-       {
-               String type = "database";
-               SWIGTYPE_p_int length = null ;          
-               String database = yaz4jlib.ZOOM_record_get(record, type, length);
-       
-               return database ;
-       }
+  @Override
+  public Object clone() {
+    SWIGTYPE_p_ZOOM_record_p clone = yaz4jlib.ZOOM_record_clone(record);
+    return new Record(clone);
+  }
 
-       public void Dispose()
-       {
-               if (!disposed)
-               {
-                       resultSet = null;
-                       record = null;
-                       disposed = true;
-               }
-       }
+  void _dispose() {
+    if (!disposed) {
+      //was cloned, need to dealloc?
+      if (rset == null) {
+        yaz4jlib.ZOOM_record_destroy(record);
+      }
+      rset = null;
+      record = null;
+      disposed = true;
+    }
+  }
 }