001: /*
002: * $Id: BaseRow.java,v 1.3 2005/12/22 09:02:30 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine.rows;
042:
043: import org.axiondb.Row;
044:
045: /**
046: * An abstract base implementation of {@link Row}, providing {@link #equals equals},
047: * {@link #hashCode hashCode} and {@link #toString toString} implementations.
048: *
049: * @version $Revision: 1.3 $ $Date: 2005/12/22 09:02:30 $
050: * @author Rodney Waldhoff
051: * @author Ahimanikya Satapathy
052: */
053: public abstract class BaseRow implements Row {
054: /**
055: * Return a hash code for me, in keeping with the generic {@link Object#hashCode}
056: * contract.
057: */
058: public int hashCode() {
059: int hash = _hash;
060: if (hash == 0) {
061: hash = size();
062: for (int i = 0, I = size(); i < I; i++) {
063: if (null != get(i)) {
064: hash ^= get(i).hashCode();
065: } else {
066: hash ^= i;
067: }
068: }
069: _hash = hash;
070: }
071: return _hash;
072: }
073:
074: /**
075: * Returns <code>true</code> iff <i>that</i> is a {@link Row} with the same number
076: * of fields and each is equal to the corresponding field in me.
077: * <p>
078: * Adheres to the generic {@link Object#equals} contract.
079: */
080: public boolean equals(Object that) {
081: if (this == that) {
082: return true;
083: }
084:
085: if (that instanceof Row) {
086: Row thatrow = (Row) that;
087: if (size() == thatrow.size()) {
088: for (int i = 0, m = size(); i < m; i++) {
089: Object a = get(i);
090: Object b = thatrow.get(i);
091: if (null == a) {
092: if (null != b) {
093: return false;
094: }
095: // they're both null, so keep going
096: } else if (!a.equals(b)) {
097: return false;
098: }
099: }
100: return true;
101: }
102: }
103: return false;
104: }
105:
106: /**
107: * Returns a simple {@link String} representation of me, perhaps suitable for
108: * debugging purposes.
109: */
110: public String toString() {
111: StringBuffer buf = new StringBuffer();
112: buf.append("{");
113: for (int i = 0, m = size(); i < m; i++) {
114: buf.append(get(i));
115: if (i != size() - 1) {
116: buf.append(",");
117: }
118: }
119: buf.append("}");
120: return buf.toString();
121: }
122:
123: public int getIdentifier() {
124: return _id;
125: }
126:
127: public void setIdentifier(int id) {
128: _id = id;
129: }
130:
131: private int _id = -1;
132:
133: /** Cache the hash code for the string */
134: protected int _hash; // Default to 0
135:
136: }
|