01: package org.hibernate.engine.query.sql;
02:
03: import org.hibernate.util.ArrayHelper;
04:
05: import java.util.Set;
06: import java.util.Collection;
07: import java.util.HashSet;
08: import java.util.Arrays;
09: import java.util.Collections;
10:
11: /**
12: * Defines the specification or blue-print for a native-sql query.
13: * Essentially a simple struct containing the information needed to "translate"
14: * a native-sql query and cache that translated representation. Also used as
15: * the key by which the native-sql query plans are cached.
16: *
17: * @author Steve Ebersole
18: */
19: public class NativeSQLQuerySpecification {
20: private final String queryString;
21: private final NativeSQLQueryReturn[] queryReturns;
22: private final Set querySpaces;
23: private final int hashCode;
24:
25: public NativeSQLQuerySpecification(String queryString,
26: NativeSQLQueryReturn[] queryReturns, Collection querySpaces) {
27: this .queryString = queryString;
28: this .queryReturns = queryReturns;
29: if (querySpaces == null) {
30: this .querySpaces = Collections.EMPTY_SET;
31: } else {
32: Set tmp = new HashSet();
33: tmp.addAll(querySpaces);
34: this .querySpaces = Collections.unmodifiableSet(tmp);
35: }
36:
37: // pre-determine and cache the hashcode
38: int hashCode = queryString.hashCode();
39: hashCode = 29 * hashCode + this .querySpaces.hashCode();
40: if (this .queryReturns != null) {
41: hashCode = 29 * hashCode
42: + ArrayHelper.toList(this .queryReturns).hashCode();
43: }
44: this .hashCode = hashCode;
45: }
46:
47: public String getQueryString() {
48: return queryString;
49: }
50:
51: public NativeSQLQueryReturn[] getQueryReturns() {
52: return queryReturns;
53: }
54:
55: public Set getQuerySpaces() {
56: return querySpaces;
57: }
58:
59: public boolean equals(Object o) {
60: if (this == o) {
61: return true;
62: }
63: if (o == null || getClass() != o.getClass()) {
64: return false;
65: }
66:
67: final NativeSQLQuerySpecification that = (NativeSQLQuerySpecification) o;
68:
69: return querySpaces.equals(that.querySpaces)
70: && queryString.equals(that.queryString)
71: && Arrays.equals(queryReturns, that.queryReturns);
72: }
73:
74: public int hashCode() {
75: return hashCode;
76: }
77: }
|