KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > query > CachedQueryKey


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.query;
30
31 /**
32  * A key for cached query results.
33  */

34 public class CachedQueryKey {
35   private String JavaDoc _sql;
36   private Object JavaDoc []_parameters;
37   private int _parameterCount;
38
39   public CachedQueryKey()
40   {
41   }
42
43   public CachedQueryKey(String JavaDoc sql, Object JavaDoc []parameters, int count)
44   {
45     _sql = sql;
46     _parameterCount = count;
47
48     if (count > 0) {
49       _parameters = new Object JavaDoc[count];
50       
51       for (int i = 0; i < count; i++) {
52     _parameters[i] = parameters[i];
53       }
54     }
55   }
56
57   void init(String JavaDoc sql, Object JavaDoc []parameters, int count)
58   {
59     _sql = sql;
60     _parameters = parameters;
61     _parameterCount = count;
62   }
63
64   /**
65    * Returns the SQL
66    */

67   public String JavaDoc getSQL()
68   {
69     return _sql;
70   }
71
72   /**
73    * Returns the hash-code for the key.
74    */

75   public int hashCode()
76   {
77     int hash = _sql.hashCode();
78
79     for (int i = _parameterCount - 1; i >= 0; i--) {
80       Object JavaDoc o = _parameters[i];
81
82       if (o != null)
83     hash = 65521 * hash + o.hashCode();
84       else
85     hash = 65521 * hash;
86     }
87
88     return hash;
89   }
90
91   /**
92    * Returns true if the key matches.
93    */

94   public boolean equals(Object JavaDoc o)
95   {
96     if (! (o instanceof CachedQueryKey))
97       return false;
98
99     CachedQueryKey key = (CachedQueryKey) o;
100
101     if (! _sql.equals(key._sql))
102       return false;
103     if (_parameterCount != key._parameterCount)
104       return false;
105
106     for (int i = _parameterCount - 1; i >= 0; i--) {
107       Object JavaDoc paramA = _parameters[i];
108       Object JavaDoc paramB = key._parameters[i];
109       
110       if (paramA != paramB && (paramA == null || ! paramA.equals(paramB)))
111     return false;
112     }
113
114     return true;
115   }
116 }
117
Popular Tags