KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > didl > UUID


1 /*
2  * Copyright (c) 2004-2005, Hewlett-Packard Company and Massachusetts
3  * Institute of Technology. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the Hewlett-Packard Company nor the name of the
17  * Massachusetts Institute of Technology nor the names of their
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32  * DAMAGE.
33  */

34 package org.dspace.app.didl;
35
36 import java.io.Serializable JavaDoc;
37 /**
38  * This class implements UUID version 4. The values for the various fields are
39  * crypto random values set by the factory class UUIDFactory
40  *
41  * Development of this code was part of the aDORe repository project by the
42  * Research Library of the Los Alamos National Laboratory.
43  *
44  * This code is based on the implementation of UUID version 4 (the one that
45  * uses random/pseudo-random numbers by Ashraf Amrou of the Old Dominion University
46  * (Aug 14, 2003)
47  **/

48 public final class UUID implements Serializable JavaDoc
49 {
50     private long hi;
51     private long lo;
52
53     /**
54      * Construct a Version 4 UUID object from another UUID object
55      *
56      * @param uuid
57      * the UUID to use as a base for the new UUID
58      **/

59     public UUID(UUID uuid)
60     {
61         this.hi = uuid.hi;
62         this.lo = uuid.lo;
63     }
64     
65     /**
66      * Construct a Version 4 UUID object form the two given long values.
67      * These values are (pseudo)random numbers (best if crypto quality)
68      *
69      * @param _hi
70      * first long value
71      *
72      * @param _lo
73      * second long value
74      *
75      **/

76     public UUID(long _hi, long _lo)
77     {
78         this.hi = _hi;
79         this.lo = _lo;
80         // IETF variant (10)b
81
lo &= 0x3FFFFFFFFFFFFFFFL; lo |= 0x8000000000000000L;
82         // set multicast bit (so that it there is no chance it will clash
83
// with other UUIDs generated based on real IEEE 802 addresses)
84
lo |= 0x0000800000000000L;
85         // version 4 (100)b: the one based on random/pseudo-random numbers
86
hi &= 0xFFFFFFFFFFFF0FFFL; hi |= 0x0000000000004000L;
87     }
88
89     /**
90      * Compare UUID objects
91      *
92      * @param obj
93      * the object to compare this UUID against
94      *
95      * @return true or false
96      **/

97     public boolean equals(Object JavaDoc obj)
98     {
99         if(this == obj) // comparing to myself
100
return true;
101         if(obj instanceof UUID)
102             return equals((UUID)obj);
103         return false;
104     }
105
106     /**
107      * Compare UUIDs
108      *
109      * @param uuid
110      * the UUID to compare this UUID against
111      *
112      * @return true or false
113      **/

114     public boolean equals(UUID uuid)
115     {
116         return (hi == uuid.hi && lo == uuid.lo);
117     }
118
119     
120     /**
121      * Generate a hash for the UUID
122      *
123      * @return hash code for the UUID
124      *
125      **/

126     public int hashCode()
127     {
128         return new Long JavaDoc(hi ^ lo).hashCode();
129     }
130  
131     
132     /**
133      * Obtain a string representation of the UUID object
134      *
135      * @return the string representation of this UUID
136      *
137      **/

138     public String JavaDoc toString()
139     {
140         return (/**"urn:uuid:" + **/
141                 hexDigits(hi >> 32, 4) // time_low: 4 hexOctet (8 hex digits)
142
+ "-" +
143                 hexDigits(hi >> 16, 2) // time_mid: 2 hexOctet (4 hex digits)
144
+ "-" +
145                 hexDigits(hi, 2) // time_high_and_version: 2 hexOctet (4 hex digits)
146
+ "-" +
147                 hexDigits(lo >> 48, 2) // clock_seq_and_reserved: 1 hexOctet (2 hex digits) & clock_seq_low: 1 hexOctet (2 hex digits)
148
+ "-" +
149                 hexDigits(lo, 6)); // node: 6 hexOctet (12 hex digits)
150
}
151
152     /**
153      * Obtain the Hex value of a given number of least significant octets
154      * from a long value as a String
155      *
156      * @param lVal
157      * the long value to retrieve octets from
158      *
159      * @param nHexOctets
160      * number of hex octets to return
161      *
162      * @return hex value of least significant octets as a string
163      *
164      **/

165     private static String JavaDoc hexDigits(long lVal, int nHexOctets) {
166         long tmp = 1L << (nHexOctets * 2 * 4); // e.g., if nHexOctets is 2, tmp = (1 0000 0000 0000 0000)b & tmp - 1 = (1111 1111 1111 1111)b
167
long result = lVal & (tmp - 1); // get ride of the uneeded most significant bits
168
result = tmp | result; // make sure the digit at position (nDigits + 1) equals 1 (to preserve leading zeroes)
169
return Long.toHexString(result).substring(1); // getride ot the digit at position nDigits + 1
170
}
171 }
Popular Tags