KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > VariantKey


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.repository;
17
18 import org.outerx.daisy.x10.VariantKeyDocument;
19
20 /**
21  * An immutable object identifying a specific document variant.
22  */

23 public final class VariantKey implements Comparable JavaDoc {
24     private final long documentId;
25     private final long branchId;
26     private final long languageId;
27
28     public VariantKey(long documentId, long branchId, long languageId) {
29         this.documentId = documentId;
30         this.branchId = branchId;
31         this.languageId = languageId;
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 VariantKeyDocument.VariantKey getXml() {
47         VariantKeyDocument.VariantKey variantKeyXml = VariantKeyDocument.VariantKey.Factory.newInstance();
48         variantKeyXml.setDocumentId(documentId);
49         variantKeyXml.setBranchId(branchId);
50         variantKeyXml.setLanguageId(languageId);
51         return variantKeyXml;
52     }
53
54     public int compareTo(Object JavaDoc o) {
55         VariantKey otherKey = (VariantKey)o;
56         if (documentId == otherKey.documentId) {
57             if (branchId == otherKey.branchId) {
58                 if (languageId == otherKey.languageId) {
59                     return 0;
60                 } else if (languageId < otherKey.languageId) {
61                     return -1;
62                 } else {
63                     return 1;
64                 }
65             } else if (branchId < otherKey.branchId) {
66                 return -1;
67             } else {
68                 return 1;
69             }
70         } else if (documentId < otherKey.documentId) {
71             return -1;
72         } else {
73             return 1;
74         }
75     }
76
77     public boolean equals(Object JavaDoc obj) {
78         if (obj == this) {
79             return true;
80         } else if (obj instanceof VariantKey) {
81             VariantKey otherKey = (VariantKey)obj;
82             return (this.documentId == otherKey.documentId && this.branchId == otherKey.branchId
83                     && this.languageId == otherKey.languageId);
84         }
85
86         return false;
87     }
88
89     public int hashCode() {
90         // The calculation technique for this hashcode is taken from the HashCodeBuilder
91
// of Jakarta Commons Lang, which in itself is based on techniques from the
92
// "Effective Java" book by Joshua Bloch.
93
final int iConstant = 159;
94         int iTotal = 615;
95
96         iTotal = appendHash(documentId, iTotal, iConstant);
97         iTotal = appendHash(branchId, iTotal, iConstant);
98         iTotal = appendHash(languageId, iTotal, iConstant);
99
100         return iTotal;
101     }
102
103     private final int appendHash(long value, int iTotal, int iConstant) {
104         return iTotal * iConstant + ((int) (value ^ (value >> 32)));
105     }
106
107     public String JavaDoc toString() {
108         return " {document ID " + documentId + ", branch ID " + branchId + ", language ID " + languageId + "}";
109     }
110 }
111
Popular Tags