Fix JVM segv on null pointers
[yaz4j-moved-to-github.git] / src / main / java / org / yaz4j / Query.java
1 /*
2  * Copyright (c) 1995-2013, Index Datassss
3  * All rights reserved.
4  * See the file LICENSE for details.
5  */
6 package org.yaz4j;
7
8 import org.yaz4j.exception.ZoomException;
9 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_query_p;
10 import org.yaz4j.jni.yaz4jlib;
11
12 /**
13  * @see <a href="http://www.indexdata.com/yaz/doc/zoom.query.html">YAZ ZOOM Query</a>
14  * @author jakub
15  */
16 public abstract class Query {
17   
18   SWIGTYPE_p_ZOOM_query_p query;
19   private boolean disposed = false;
20   
21   protected Query(String queryString) {
22     if (queryString == null)
23       throw new NullPointerException("query string cannot be null");
24     query = yaz4jlib.ZOOM_query_create();
25   }
26   
27   public void sortBy(String strategy, String criteria) throws ZoomException {
28     int ret = yaz4jlib.ZOOM_query_sortby2(query, strategy, criteria);
29     if (ret != 0) {
30       throw new ZoomException("query sortBy failed");
31     }
32   }
33   
34   protected void finalize() {
35     _dispose();
36   }
37
38   void _dispose() {
39     if (!disposed) {
40       yaz4jlib.ZOOM_query_destroy(query);
41       disposed = true;
42     }
43   }
44   
45 }