KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > uuid > BasicUUID


1 /*
2
3    Derby - Class org.apache.derby.impl.services.uuid.BasicUUID
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.services.uuid;
23
24 import org.apache.derby.iapi.services.io.FormatIdUtil;
25 import org.apache.derby.iapi.services.io.StoredFormatIds;
26 import org.apache.derby.iapi.services.io.Formatable;
27
28 import org.apache.derby.catalog.UUID;
29
30 import java.io.ObjectOutput JavaDoc;
31 import java.io.ObjectInput JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 import java.io.StringReader JavaDoc;
35
36
37 public class BasicUUID implements UUID, Formatable
38 {
39     /*
40     ** Fields of BasicUUID
41     */

42     
43     private long majorId; // only using 48 bits
44
private long timemillis;
45     private int sequence;
46
47     /*
48     ** Methods of BasicUUID
49     */

50
51     /**
52         Constructor only called by BasicUUIDFactory.
53     **/

54     public BasicUUID(long majorId, long timemillis, int sequence)
55     {
56         this.majorId = majorId;
57         this.timemillis = timemillis;
58         this.sequence = sequence;
59     }
60
61     /**
62         Constructor only called by BasicUUIDFactory.
63         Constructs a UUID from the string representation
64         produced by toString.
65         @see BasicUUID#toString
66     **/

67     public BasicUUID(String JavaDoc uuidstring)
68     {
69         StringReader JavaDoc sr = new StringReader JavaDoc(uuidstring);
70         sequence = (int) readMSB(sr);
71
72         long ltimemillis = readMSB(sr) << 32;
73         ltimemillis += readMSB(sr) << 16;
74         ltimemillis += readMSB(sr);
75         timemillis = ltimemillis;
76         majorId = readMSB(sr);
77     }
78
79     /**
80         Constructor only called by BasicUUIDFactory.
81         Constructs a UUID from the byte array representation
82         produced by toByteArrayio.
83         @see BasicUUID#toByteArray
84     **/

85     public BasicUUID(byte[] b)
86     {
87         int lsequence = 0;
88         for (int ix = 0; ix < 4; ix++)
89         {
90             lsequence = lsequence << 8;
91             lsequence = lsequence | (0xff & b[ix]);
92         }
93
94         long ltimemillis = 0;
95         for (int ix = 4; ix < 10; ix++)
96         {
97             ltimemillis = ltimemillis << 8;
98             ltimemillis = ltimemillis | (0xff & b[ix]);
99         }
100
101         long linetaddr = 0;
102         for (int ix = 10; ix < 16; ix++)
103         {
104             linetaddr = linetaddr << 8;
105             linetaddr = linetaddr | (0xff & b[ix]);
106         }
107
108         sequence = lsequence;
109         timemillis = ltimemillis;
110         majorId = linetaddr;
111     }
112
113     /*
114      * Formatable methods
115      */

116
117     // no-arg constructor, required by Formatable
118
public BasicUUID() { super(); }
119
120     /**
121         Write this out.
122         @exception IOException error writing to log stream
123     */

124     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
125     {
126         // RESOLVE: write out the byte array instead?
127
out.writeLong(majorId);
128         out.writeLong(timemillis);
129         out.writeInt(sequence);
130     }
131
132     /**
133         Read this in
134         @exception IOException error reading from log stream
135     */

136     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc
137     {
138         majorId = in.readLong();
139         timemillis = in.readLong();
140         sequence = in.readInt();
141     }
142
143     /**
144         Return my format identifier.
145     */

146     public int getTypeFormatId() {
147         return StoredFormatIds.BASIC_UUID;
148     }
149
150     private static void writeMSB(char[] data, int offset, long value, int nbytes)
151     {
152         for (int i = nbytes - 1; i >= 0; i--)
153         {
154            long b = (value & (255L << (8 * i))) >>> (8 * i);
155
156            int c = (int) ((b & 0xf0) >> 4);
157            data[offset++] = (char) (c < 10 ? c + '0' : (c - 10) + 'a');
158            c = (int) (b & 0x0f);
159            data[offset++] = (char) (c < 10 ? c + '0' : (c - 10) + 'a');
160         }
161     }
162
163     /**
164         Read a long value, msb first, from its character
165         representation in the string reader, using '-' or
166         end of string to delimit.
167     **/

168     private static long readMSB(StringReader JavaDoc sr)
169     {
170         long value = 0;
171
172         try
173         {
174             int c;
175             while ((c = sr.read()) != -1)
176             {
177                 if (c == '-')
178                     break;
179                 value <<= 4;
180
181                 int nibble;
182                 if (c <= '9')
183                     nibble = c - '0';
184                 else if (c <= 'F')
185                     nibble = c - 'A' + 10;
186                 else
187                     nibble = c - 'a' + 10;
188                 value += nibble;
189             }
190         }
191         catch (Exception JavaDoc e)
192         {
193         }
194
195         return value;
196     }
197
198     /*
199     ** Methods of UUID
200     */

201
202     /**
203         Implement value equality.
204
205     **/

206     public boolean equals(Object JavaDoc otherObject)
207     {
208         if (!(otherObject instanceof BasicUUID))
209             return false;
210
211         BasicUUID other = (BasicUUID) otherObject;
212
213         return (this.sequence == other.sequence)
214             && (this.timemillis == other.timemillis)
215             && (this.majorId == other.majorId);
216     }
217
218     /**
219         Provide a hashCode which is compatible with
220         the equals() method.
221     **/

222     public int hashCode()
223     {
224         long hc = majorId ^ timemillis;
225
226         return sequence ^ ((int) (hc >> 4));
227     }
228
229     /**
230         Produce a string representation of this UUID which
231         can be passed to UUIDFactory.recreateUUID later on
232         to reconstruct it. The funny representation is
233         designed to (sort of) match the format of Microsoft's
234         UUIDGEN utility.
235      */

236     public String JavaDoc toString() {return stringWorkhorse( '-' );}
237
238     /**
239         Produce a string representation of this UUID which
240         is suitable for use as a unique ANSI identifier.
241      */

242     public String JavaDoc toANSIidentifier() {return "U" + stringWorkhorse( 'X' );}
243
244     /**
245       * Private workhorse of the string making routines.
246       *
247       * @param separator Character to separate number blocks.
248       * Null means do not include a separator.
249       *
250       * @return string representation of UUID.
251       */

252     public String JavaDoc stringWorkhorse( char separator )
253     {
254         char[] data = new char[36];
255
256         writeMSB(data, 0, (long) sequence, 4);
257
258         int offset = 8;
259         if (separator != 0) data[offset++] = separator;
260
261         long ltimemillis = timemillis;
262         writeMSB(data, offset, (ltimemillis & 0x0000ffff00000000L) >>> 32, 2);
263         offset += 4;
264         if (separator != 0) data[offset++] = separator;
265         writeMSB(data, offset, (ltimemillis & 0x00000000ffff0000L) >>> 16, 2);
266         offset += 4;
267         if (separator != 0) data[offset++] = separator;
268         writeMSB(data, offset, (ltimemillis & 0x000000000000ffffL), 2);
269         offset += 4;
270         if (separator != 0) data[offset++] = separator;
271         writeMSB(data, offset, majorId, 6);
272         offset += 12;
273
274         return new String JavaDoc(data, 0, offset);
275     }
276
277     /**
278       Store this UUID in a byte array. Arrange the bytes in the UUID
279       in the same order the code which stores a UUID in a string
280       does.
281       
282       @see org.apache.derby.catalog.UUID#toByteArray
283     */

284     public byte[] toByteArray()
285     {
286         byte[] result = new byte[16];
287
288         int lsequence = sequence;
289         result[0] = (byte)(lsequence >>> 24);
290         result[1] = (byte)(lsequence >>> 16);
291         result[2] = (byte)(lsequence >>> 8);
292         result[3] = (byte)lsequence;
293
294         long ltimemillis = timemillis;
295         result[4] = (byte)(ltimemillis >>> 40);
296         result[5] = (byte)(ltimemillis >>> 32);
297         result[6] = (byte)(ltimemillis >>> 24);
298         result[7] = (byte)(ltimemillis >>> 16);
299         result[8] = (byte)(ltimemillis >>> 8);
300         result[9] = (byte)ltimemillis;
301
302         long linetaddr = majorId;
303         result[10] = (byte)(linetaddr >>> 40);
304         result[11] = (byte)(linetaddr >>> 32);
305         result[12] = (byte)(linetaddr >>> 24);
306         result[13] = (byte)(linetaddr >>> 16);
307         result[14] = (byte)(linetaddr >>> 8);
308         result[15] = (byte)linetaddr;
309
310         return result;
311     }
312
313     /**
314       Clone this UUID.
315
316       @return a copy of this UUID
317       */

318     public UUID cloneMe()
319     {
320         return new BasicUUID(majorId, timemillis, sequence);
321     }
322
323     public String JavaDoc toHexString() {return stringWorkhorse( (char) 0 );}
324 }
325
326
Popular Tags