KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cache > QueryKey


1 //$Id: QueryKey.java,v 1.4 2005/04/26 03:21:57 oneovthafew Exp $
2
package org.hibernate.cache;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Set JavaDoc;
7
8 import org.hibernate.EntityMode;
9 import org.hibernate.engine.QueryParameters;
10 import org.hibernate.engine.RowSelection;
11 import org.hibernate.type.Type;
12 import org.hibernate.util.EqualsHelper;
13
14 /**
15  * A key that identifies a particular query with bound parameter values
16  * @author Gavin King
17  */

18 public class QueryKey implements Serializable JavaDoc {
19     private final String JavaDoc sqlQueryString;
20     private final Type[] types;
21     private final Object JavaDoc[] values;
22     private final Integer JavaDoc firstRow;
23     private final Integer JavaDoc maxRows;
24     private final Map JavaDoc namedParameters;
25     private final EntityMode entityMode;
26     private final Set JavaDoc filters;
27     private final int hashCode;
28     
29     public QueryKey(String JavaDoc queryString, QueryParameters queryParameters, Set JavaDoc filters, EntityMode entityMode) {
30         this.sqlQueryString = queryString;
31         this.types = queryParameters.getPositionalParameterTypes();
32         this.values = queryParameters.getPositionalParameterValues();
33         RowSelection selection = queryParameters.getRowSelection();
34         if (selection!=null) {
35             firstRow = selection.getFirstRow();
36             maxRows = selection.getMaxRows();
37         }
38         else {
39             firstRow = null;
40             maxRows = null;
41         }
42         this.namedParameters = queryParameters.getNamedParameters();
43         this.entityMode = entityMode;
44         this.filters = filters;
45         this.hashCode = getHashCode();
46     }
47     
48     public boolean equals(Object JavaDoc other) {
49         QueryKey that = (QueryKey) other;
50         if ( !sqlQueryString.equals(that.sqlQueryString) ) return false;
51         if ( !EqualsHelper.equals(firstRow, that.firstRow) || !EqualsHelper.equals(maxRows, that.maxRows) ) return false;
52         if (types==null) {
53             if (that.types!=null) return false;
54         }
55         else {
56             if (that.types==null) return false;
57             if ( types.length!=that.types.length ) return false;
58             for ( int i=0; i<types.length; i++ ) {
59                 if ( types[i].getReturnedClass() != that.types[i].getReturnedClass() ) return false;
60                 if ( !types[i].isEqual( values[i], that.values[i], entityMode ) ) return false;
61             }
62         }
63         if ( !EqualsHelper.equals(filters, that.filters) ) return false;
64         if ( !EqualsHelper.equals(namedParameters, that.namedParameters) ) return false;
65         return true;
66     }
67     
68     public int hashCode() {
69         return hashCode;
70     }
71     
72     private int getHashCode() {
73         int result = 13;
74         result = 37 * result + ( firstRow==null ? 0 : firstRow.hashCode() );
75         result = 37 * result + ( maxRows==null ? 0 : maxRows.hashCode() );
76         for ( int i=0; i<values.length; i++ ) {
77             result = 37 * result + ( values[i]==null ? 0 : types[i].getHashCode( values[i], entityMode ) );
78         }
79         result = 37 * result + ( namedParameters==null ? 0 : namedParameters.hashCode() );
80         result = 37 * result + ( filters==null ? 0 : filters.hashCode() );
81         result = 37 * result + sqlQueryString.hashCode();
82         return result;
83     }
84
85     public String JavaDoc toString() {
86         StringBuffer JavaDoc buf = new StringBuffer JavaDoc()
87             .append("sql: ")
88             .append(sqlQueryString);
89         if (values!=null) {
90             buf.append("; parameters: ");
91             for (int i=0; i<values.length; i++) {
92                 buf.append( values[i] )
93                     .append(", ");
94             }
95         }
96         if (namedParameters!=null) {
97             buf.append("; named parameters: ")
98                 .append(namedParameters);
99         }
100         if (filters!=null) {
101             buf.append("; filters: ")
102                 .append(filters);
103         }
104         if (firstRow!=null) buf.append("; first row: ").append(firstRow);
105         if (maxRows!=null) buf.append("; max rows: ").append(maxRows);
106         return buf.toString();
107     }
108     
109 }
110
Popular Tags