KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > VersionKey


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.books.publisher.impl;
17
18 /**
19  * An immutable object identifying a specific document variant and version.
20  */

21 public class VersionKey implements Comparable JavaDoc {
22     private final long documentId;
23     private final long branchId;
24     private final long languageId;
25     private final long versionId;
26
27     public VersionKey(long documentId, long branchId, long languageId, long versionId) {
28         this.documentId = documentId;
29         this.branchId = branchId;
30         this.languageId = languageId;
31         this.versionId = versionId;
32     }
33
34     public long getDocumentId() {
35         return documentId;
36     }
37
38     public long getBranchId() {
39         return branchId;
40     }
41
42     public long getLanguageId() {
43         return languageId;
44     }
45
46     public long getVersionId() {
47         return versionId;
48     }
49
50     public String JavaDoc getVersion() {
51         if (versionId == -1) {
52             return "live";
53         } else if (versionId == -2) {
54             return "last";
55         } else {
56             return String.valueOf(versionId);
57         }
58     }
59
60     public int compareTo(Object JavaDoc o) {
61         VersionKey otherKey = (VersionKey)o;
62         if (documentId == otherKey.documentId) {
63             if (branchId == otherKey.branchId) {
64                 if (languageId == otherKey.languageId) {
65                     if (versionId == otherKey.versionId) {
66                         return 0;
67                     } else if (versionId < otherKey.versionId) {
68                         return -1;
69                     } else {
70                         return 1;
71                     }
72                 } else if (languageId < otherKey.languageId) {
73                     return -1;
74                 } else {
75                     return 1;
76                 }
77             } else if (branchId < otherKey.branchId) {
78                 return -1;
79             } else {
80                 return 1;
81             }
82         } else if (documentId < otherKey.documentId) {
83             return -1;
84         } else {
85             return 1;
86         }
87     }
88
89     public boolean equals(Object JavaDoc obj) {
90         if (obj == this) {
91             return true;
92         } else if (obj instanceof VersionKey) {
93             VersionKey otherKey = (VersionKey)obj;
94             return (this.documentId == otherKey.documentId && this.branchId == otherKey.branchId
95                     && this.languageId == otherKey.languageId && this.versionId == otherKey.versionId);
96         }
97
98         return false;
99     }
100
101     public int hashCode() {
102         // The calculation technique for this hashcode is taken from the HashCodeBuilder
103
// of Jakarta Commons Lang, which in itself is based on techniques from the
104
// "Effective Java" book by Joshua Bloch.
105
final int iConstant = 159;
106         int iTotal = 615;
107
108         iTotal = appendHash(documentId, iTotal, iConstant);
109         iTotal = appendHash(branchId, iTotal, iConstant);
110         iTotal = appendHash(languageId, iTotal, iConstant);
111         iTotal = appendHash(versionId, iTotal, iConstant);
112
113         return iTotal;
114     }
115
116     private int appendHash(long value, int iTotal, int iConstant) {
117         return iTotal * iConstant + ((int) (value ^ (value >> 32)));
118     }
119
120     public String JavaDoc toString() {
121         return "{document ID " + documentId + ", branch ID " + branchId + ", language ID " + languageId + ", version ID " + versionId + "}";
122     }
123 }
124
Popular Tags