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: /**
032: * A key for cached query results.
033: */
034: public class CachedQueryKey {
035: private String _sql;
036: private Object[] _parameters;
037: private int _parameterCount;
038:
039: public CachedQueryKey() {
040: }
041:
042: public CachedQueryKey(String sql, Object[] parameters, int count) {
043: _sql = sql;
044: _parameterCount = count;
045:
046: if (count > 0) {
047: _parameters = new Object[count];
048:
049: for (int i = 0; i < count; i++) {
050: _parameters[i] = parameters[i];
051: }
052: }
053: }
054:
055: void init(String sql, Object[] parameters, int count) {
056: _sql = sql;
057: _parameters = parameters;
058: _parameterCount = count;
059: }
060:
061: /**
062: * Returns the SQL
063: */
064: public String getSQL() {
065: return _sql;
066: }
067:
068: /**
069: * Returns the hash-code for the key.
070: */
071: public int hashCode() {
072: int hash = _sql.hashCode();
073:
074: for (int i = _parameterCount - 1; i >= 0; i--) {
075: Object o = _parameters[i];
076:
077: if (o != null)
078: hash = 65521 * hash + o.hashCode();
079: else
080: hash = 65521 * hash;
081: }
082:
083: return hash;
084: }
085:
086: /**
087: * Returns true if the key matches.
088: */
089: public boolean equals(Object o) {
090: if (!(o instanceof CachedQueryKey))
091: return false;
092:
093: CachedQueryKey key = (CachedQueryKey) o;
094:
095: if (!_sql.equals(key._sql))
096: return false;
097: if (_parameterCount != key._parameterCount)
098: return false;
099:
100: for (int i = _parameterCount - 1; i >= 0; i--) {
101: Object paramA = _parameters[i];
102: Object paramB = key._parameters[i];
103:
104: if (paramA != paramB
105: && (paramA == null || !paramA.equals(paramB)))
106: return false;
107: }
108:
109: return true;
110: }
111: }
|