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: import org.outerx.daisy.x10.VariantKeyDocument;
019:
020: /**
021: * An immutable object identifying a specific document variant.
022: */
023: public final class VariantKey implements Comparable {
024: private final String documentId;
025: private final long branchId;
026: private final long languageId;
027:
028: public VariantKey(String documentId, long branchId, long languageId) {
029: if (documentId == null)
030: throw new IllegalArgumentException(
031: "documentId can not be null");
032: this .documentId = documentId;
033: this .branchId = branchId;
034: this .languageId = languageId;
035: }
036:
037: public String getDocumentId() {
038: return documentId;
039: }
040:
041: public long getBranchId() {
042: return branchId;
043: }
044:
045: public long getLanguageId() {
046: return languageId;
047: }
048:
049: public VariantKeyDocument.VariantKey getXml() {
050: VariantKeyDocument.VariantKey variantKeyXml = VariantKeyDocument.VariantKey.Factory
051: .newInstance();
052: variantKeyXml.setDocumentId(documentId);
053: variantKeyXml.setBranchId(branchId);
054: variantKeyXml.setLanguageId(languageId);
055: return variantKeyXml;
056: }
057:
058: public int compareTo(Object o) {
059: VariantKey otherKey = (VariantKey) o;
060: int docCompareResult = documentId
061: .compareTo(otherKey.documentId);
062: if (docCompareResult == 0) {
063: if (branchId == otherKey.branchId) {
064: if (languageId == otherKey.languageId) {
065: return 0;
066: } else if (languageId < otherKey.languageId) {
067: return -1;
068: } else {
069: return 1;
070: }
071: } else if (branchId < otherKey.branchId) {
072: return -1;
073: } else {
074: return 1;
075: }
076: } else {
077: return docCompareResult;
078: }
079: }
080:
081: public boolean equals(Object obj) {
082: if (obj == this ) {
083: return true;
084: } else if (obj instanceof VariantKey) {
085: VariantKey otherKey = (VariantKey) obj;
086: return (this .documentId.equals(otherKey.documentId)
087: && this .branchId == otherKey.branchId && this .languageId == otherKey.languageId);
088: }
089:
090: return false;
091: }
092:
093: public int hashCode() {
094: // The calculation technique for this hashcode is taken from the HashCodeBuilder
095: // of Jakarta Commons Lang, which in itself is based on techniques from the
096: // "Effective Java" book by Joshua Bloch.
097: final int iConstant = 159;
098: int iTotal = 615;
099:
100: iTotal = iTotal * iConstant + documentId.hashCode();
101: iTotal = appendHash(branchId, iTotal, iConstant);
102: iTotal = appendHash(languageId, iTotal, iConstant);
103:
104: return iTotal;
105: }
106:
107: private int appendHash(long value, int iTotal, int iConstant) {
108: return iTotal * iConstant + ((int) (value ^ (value >> 32)));
109: }
110:
111: public String toString() {
112: return " {document ID " + documentId + ", branch ID "
113: + branchId + ", language ID " + languageId + "}";
114: }
115: }
|