001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.jdbc.snapshot;
018:
019: import java.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: /**
025: * A row level snapshot holder. Holds a list of id values (as Strings) and a
026: * list of version values (as Long).
027: * <p>
028: * <code>hashCode</code> and <code>equals</code> uses the ids alone to
029: * perform the operations.
030: * <p>
031: * A utility method {@link #isOlderThan(JdbcAliasRowSnapshot)} is provides to
032: * compare it with other row snapshots.
033: *
034: * @author kimchy
035: */
036: public class JdbcAliasRowSnapshot implements Serializable {
037:
038: private static final long serialVersionUID = 4300559727598252558L;
039:
040: private ArrayList ids = new ArrayList();
041:
042: private ArrayList versions = new ArrayList();
043:
044: public void addIdValue(String idValue) {
045: ids.add(idValue);
046: }
047:
048: public List getIds() {
049: return ids;
050: }
051:
052: public void addVersionValue(Long versionValue) {
053: versions.add(versionValue);
054: }
055:
056: public boolean isOlderThan(JdbcAliasRowSnapshot rowSnapshot) {
057: for (int i = 0; i < versions.size(); i++) {
058: if (((Long) versions.get(i))
059: .compareTo((Long) rowSnapshot.versions.get(i)) < 0) {
060: return true;
061: }
062: }
063: return false;
064: }
065:
066: public boolean equals(Object other) {
067: JdbcAliasRowSnapshot otherRow = (JdbcAliasRowSnapshot) other;
068: if (otherRow.ids.size() == 1) {
069: return ids.get(0).equals(otherRow.ids.get(0));
070: } else {
071: if (ids.size() != otherRow.ids.size()) {
072: return false;
073: }
074: for (int i = 0; i < ids.size(); i++) {
075: if (!ids.get(i).equals(otherRow.ids.get(i))) {
076: return false;
077: }
078: }
079: }
080: return true;
081: }
082:
083: public int hashCode() {
084: int result;
085: result = ids.get(0).hashCode();
086: if (ids.size() > 1) {
087: for (int i = 1; i < ids.size(); i++) {
088: result = 7 * result + ids.get(i).hashCode();
089: }
090: }
091: return result;
092: }
093:
094: public String toString() {
095: StringBuffer sb = new StringBuffer();
096: sb.append("Ids [");
097: for (Iterator it = ids.iterator(); it.hasNext();) {
098: sb.append(it.next());
099: sb.append(",");
100: }
101: sb.append("] Versions [");
102: for (Iterator it = versions.iterator(); it.hasNext();) {
103: sb.append(it.next());
104: sb.append(",");
105: }
106: sb.append("]");
107: return sb.toString();
108: }
109: }
|