Fix JVM segv on null pointers
[yaz4j-moved-to-github.git] / src / main / java / org / yaz4j / Record.java
1 package org.yaz4j;
2
3 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_record_p;
4 import org.yaz4j.jni.yaz4jlib;
5
6 public class Record implements Cloneable {
7
8   private SWIGTYPE_p_ZOOM_record_p record;
9   private ResultSet rset;
10   private boolean disposed = false;
11
12   Record(SWIGTYPE_p_ZOOM_record_p record, ResultSet rset) {
13     this.record = record;
14     this.rset = rset;
15   }
16
17   protected Record(SWIGTYPE_p_ZOOM_record_p record) {
18     this.record = record;
19   }
20
21   @Override
22   public void finalize() {
23     _dispose();
24   }
25
26   public byte[] get(String type) {
27     if (type == null)
28       throw new NullPointerException("type cannot be null");
29     return yaz4jlib.ZOOM_record_get_bytes(record, type);
30   }
31
32   public String render() {
33     return new String(get("render"));
34   }
35
36   public byte[] getContent() {
37     return get("raw");
38   }
39
40   public String getSyntax() {
41     return new String(get("syntax"));
42   }
43
44   public String getDatabase() {
45     return new String(get("database"));
46   }
47
48   @Override
49   public Object clone() {
50     SWIGTYPE_p_ZOOM_record_p clone = yaz4jlib.ZOOM_record_clone(record);
51     return new Record(clone);
52   }
53
54   void _dispose() {
55     if (!disposed) {
56       //was cloned, need to dealloc?
57       if (rset == null) {
58         yaz4jlib.ZOOM_record_destroy(record);
59       }
60       rset = null;
61       record = null;
62       disposed = true;
63     }
64   }
65 }