1 16 package org.outerj.daisy.books.publisher.impl; 17 18 21 public class VersionKey implements Comparable { 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 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 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 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 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 toString() { 121 return "{document ID " + documentId + ", branch ID " + branchId + ", language ID " + languageId + ", version ID " + versionId + "}"; 122 } 123 } 124 | Popular Tags |