Mercurial > hg > blitz_condensed
comparison src/org/dancres/blitz/oid/UIDImpl.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 package org.dancres.blitz.oid; | |
2 | |
3 import java.io.Serializable; | |
4 | |
5 import org.dancres.util.BytePacker; | |
6 | |
7 /** | |
8 A unique identifier which can be generated by an Allocator. | |
9 */ | |
10 final class UIDImpl implements OID { | |
11 static final int KEY_SIZE = 12; | |
12 | |
13 private transient byte[] theKey; | |
14 | |
15 private long theId; | |
16 private int theZoneId; | |
17 | |
18 UIDImpl(byte[] aKey) { | |
19 theKey = aKey; | |
20 | |
21 BytePacker myPacker = BytePacker.getMSBPacker(theKey); | |
22 theZoneId = myPacker.getInt(0); | |
23 theId = myPacker.getLong(4); | |
24 } | |
25 | |
26 UIDImpl(int aZoneId, long anId) { | |
27 theId = anId; | |
28 theZoneId = aZoneId; | |
29 } | |
30 | |
31 public int getZoneId() { | |
32 return theZoneId; | |
33 } | |
34 | |
35 public long getId() { | |
36 return theId; | |
37 } | |
38 | |
39 synchronized byte[] getKey() { | |
40 if (theKey == null) { | |
41 theKey = keyFor(theZoneId, theId); | |
42 } | |
43 | |
44 return theKey; | |
45 } | |
46 | |
47 public String toString() { | |
48 return " " + theZoneId + "->" + theId; | |
49 } | |
50 | |
51 public int hashCode() { | |
52 /* | |
53 We include theZoneId to allow for the fact that all allocators for | |
54 a given Entry type will be allocating id's close to each other | |
55 because of the random allocator selection used when an id is | |
56 generated. Because the id's are allocated evenly, there's a high | |
57 chance of two UID's allocated close together in time colliding unless | |
58 we also factor in theZoneId. | |
59 */ | |
60 return theZoneId ^ ((int) (theId ^ (theId >>> 32))); | |
61 } | |
62 | |
63 public int compareTo(Object anObject) { | |
64 UIDImpl myOther = (UIDImpl) anObject; | |
65 | |
66 if (theZoneId < myOther.theZoneId) | |
67 return -1; | |
68 else if (theZoneId > myOther.theZoneId) | |
69 return 1; | |
70 | |
71 if (theId < myOther.theId) | |
72 return -1; | |
73 else if (theId > myOther.theId) | |
74 return 1; | |
75 else | |
76 return 0; | |
77 } | |
78 | |
79 public boolean equals(Object anObject) { | |
80 if (anObject instanceof UIDImpl) { | |
81 UIDImpl myOther = (UIDImpl) anObject; | |
82 | |
83 return ((theZoneId == myOther.theZoneId) && | |
84 (theId == myOther.theId)); | |
85 } | |
86 | |
87 return false; | |
88 } | |
89 | |
90 private byte[] keyFor(int aZoneId, long anId) { | |
91 byte[] myKey = new byte[12]; | |
92 | |
93 BytePacker myPacker = BytePacker.getMSBPacker(myKey); | |
94 myPacker.putInt(aZoneId, 0); | |
95 myPacker.putLong(anId, 4); | |
96 | |
97 return myKey; | |
98 } | |
99 } |