001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
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: package org.outerj.daisy.repository;
017:
018: /**
019: * An immutable object identifying a specific document variant and version.
020: */
021: public class VersionKey implements Comparable {
022: private final String documentId;
023: private final long branchId;
024: private final long languageId;
025: private final long versionId;
026:
027: public VersionKey(String documentId, long branchId,
028: long languageId, long versionId) {
029: this .documentId = documentId;
030: this .branchId = branchId;
031: this .languageId = languageId;
032: this .versionId = versionId;
033: }
034:
035: public String getDocumentId() {
036: return documentId;
037: }
038:
039: public long getBranchId() {
040: return branchId;
041: }
042:
043: public long getLanguageId() {
044: return languageId;
045: }
046:
047: public long getVersionId() {
048: return versionId;
049: }
050:
051: public String getVersion() {
052: if (versionId == -1) {
053: return "live";
054: } else if (versionId == -2) {
055: return "last";
056: } else {
057: return String.valueOf(versionId);
058: }
059: }
060:
061: public int compareTo(Object o) {
062: VersionKey otherKey = (VersionKey) o;
063: int docIdCompareResult = documentId
064: .compareTo(otherKey.documentId);
065: if (docIdCompareResult == 0) {
066: if (branchId == otherKey.branchId) {
067: if (languageId == otherKey.languageId) {
068: if (versionId == otherKey.versionId) {
069: return 0;
070: } else if (versionId < otherKey.versionId) {
071: return -1;
072: } else {
073: return 1;
074: }
075: } else if (languageId < otherKey.languageId) {
076: return -1;
077: } else {
078: return 1;
079: }
080: } else if (branchId < otherKey.branchId) {
081: return -1;
082: } else {
083: return 1;
084: }
085: } else {
086: return docIdCompareResult;
087: }
088: }
089:
090: public boolean equals(Object obj) {
091: if (obj == this ) {
092: return true;
093: } else if (obj instanceof VersionKey) {
094: VersionKey otherKey = (VersionKey) obj;
095: return (this .documentId.equals(otherKey.documentId)
096: && this .branchId == otherKey.branchId
097: && this .languageId == otherKey.languageId && this .versionId == otherKey.versionId);
098: }
099:
100: return false;
101: }
102:
103: public int hashCode() {
104: // The calculation technique for this hashcode is taken from the HashCodeBuilder
105: // of Jakarta Commons Lang, which in itself is based on techniques from the
106: // "Effective Java" book by Joshua Bloch.
107: final int iConstant = 159;
108: int iTotal = 615;
109:
110: iTotal = iTotal * iConstant + documentId.hashCode();
111: iTotal = appendHash(branchId, iTotal, iConstant);
112: iTotal = appendHash(languageId, iTotal, iConstant);
113: iTotal = appendHash(versionId, iTotal, iConstant);
114:
115: return iTotal;
116: }
117:
118: private int appendHash(long value, int iTotal, int iConstant) {
119: return iTotal * iConstant + ((int) (value ^ (value >> 32)));
120: }
121:
122: public String toString() {
123: return "{document ID " + documentId + ", branch ID " + branchId
124: + ", language ID " + languageId + ", version ID "
125: + versionId + "}";
126: }
127: }
|