KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.caucho.util.CharBuffer;
32
33 /**
34  * The key for a query cache entry
35  */

36 public class QueryCacheKey {
37   private String JavaDoc _sql;
38   private Object JavaDoc []_args;
39   private int _startRow;
40
41   public QueryCacheKey()
42   {
43   }
44
45   public QueryCacheKey(String JavaDoc sql, Object JavaDoc []args, int startRow)
46   {
47     init(sql, args, startRow);
48   }
49
50   /**
51    * Initialize the cache key values.
52    */

53   public void init(String JavaDoc sql, Object JavaDoc []args, int startRow)
54   {
55     _sql = sql;
56     _args = args;
57     _startRow = startRow;
58   }
59
60   /**
61    * Returns the hash code.
62    */

63   public int hashCode()
64   {
65     int hash = _startRow;
66
67     hash = 65537 * hash + _sql.hashCode();
68
69     Object JavaDoc []args = _args;
70     for (int i = args.length - 1; i >= 0; i--) {
71       Object JavaDoc v = args[i];
72
73       if (v == null)
74     hash = 65537 * hash + 17;
75       else
76     hash = 65537 * hash + 17 * v.hashCode();
77     }
78
79     return hash;
80   }
81
82   /**
83    * Test for equality.
84    */

85   public boolean equals(Object JavaDoc o)
86   {
87     if (this == o)
88       return true;
89     else if (o == null || o.getClass() != getClass())
90       return false;
91
92     QueryCacheKey key = (QueryCacheKey) o;
93
94     if (! _sql.equals(key._sql)) {
95       return false;
96     }
97   
98     if (_startRow != key._startRow) {
99       return false;
100     }
101
102     Object JavaDoc []argsA = _args;
103     Object JavaDoc []argsB = key._args;
104
105     if (argsA.length != argsB.length) {
106       return false;
107     }
108
109     for (int i = argsA.length - 1; i >= 0; i--) {
110       Object JavaDoc a = argsA[i];
111       Object JavaDoc b = argsB[i];
112
113       if (a != b && (a == null || ! a.equals(b))) {
114     return false;
115       }
116     }
117
118     return true;
119   }
120
121   public String JavaDoc toString()
122   {
123     CharBuffer cb = new CharBuffer();
124
125     cb.append("QueryCacheKey[");
126     cb.append(_sql);
127
128     for (int i = 0; i < _args.length; i++) {
129       cb.append(",");
130       cb.append(_args[i]);
131     }
132
133     cb.append("]");
134
135     return cb.toString();
136   }
137 }
138
Popular Tags