001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.query;
030:
031: import com.caucho.util.CharBuffer;
032:
033: /**
034: * The key for a query cache entry
035: */
036: public class QueryCacheKey {
037: private String _sql;
038: private Object[] _args;
039: private int _startRow;
040:
041: public QueryCacheKey() {
042: }
043:
044: public QueryCacheKey(String sql, Object[] args, int startRow) {
045: init(sql, args, startRow);
046: }
047:
048: /**
049: * Initialize the cache key values.
050: */
051: public void init(String sql, Object[] args, int startRow) {
052: _sql = sql;
053: _args = args;
054: _startRow = startRow;
055: }
056:
057: /**
058: * Returns the hash code.
059: */
060: public int hashCode() {
061: int hash = _startRow;
062:
063: hash = 65537 * hash + _sql.hashCode();
064:
065: Object[] args = _args;
066: for (int i = args.length - 1; i >= 0; i--) {
067: Object v = args[i];
068:
069: if (v == null)
070: hash = 65537 * hash + 17;
071: else
072: hash = 65537 * hash + 17 * v.hashCode();
073: }
074:
075: return hash;
076: }
077:
078: /**
079: * Test for equality.
080: */
081: public boolean equals(Object o) {
082: if (this == o)
083: return true;
084: else if (o == null || o.getClass() != getClass())
085: return false;
086:
087: QueryCacheKey key = (QueryCacheKey) o;
088:
089: if (!_sql.equals(key._sql)) {
090: return false;
091: }
092:
093: if (_startRow != key._startRow) {
094: return false;
095: }
096:
097: Object[] argsA = _args;
098: Object[] argsB = key._args;
099:
100: if (argsA.length != argsB.length) {
101: return false;
102: }
103:
104: for (int i = argsA.length - 1; i >= 0; i--) {
105: Object a = argsA[i];
106: Object b = argsB[i];
107:
108: if (a != b && (a == null || !a.equals(b))) {
109: return false;
110: }
111: }
112:
113: return true;
114: }
115:
116: public String toString() {
117: CharBuffer cb = new CharBuffer();
118:
119: cb.append("QueryCacheKey[");
120: cb.append(_sql);
121:
122: for (int i = 0; i < _args.length; i++) {
123: cb.append(",");
124: cb.append(_args[i]);
125: }
126:
127: cb.append("]");
128:
129: return cb.toString();
130: }
131: }
|