KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > util > UUID_


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

32 package net.sf.retrotranslator.runtime.java.util;
33
34 import java.io.Serializable JavaDoc;
35 import java.security.*;
36
37 /**
38  * @author Taras Puchko
39  */

40 public class UUID_ implements Serializable JavaDoc, Comparable JavaDoc<UUID_> {
41
42     private static final long serialVersionUID = 4759128571957838954L;
43     private static final SecureRandom randomGenerator = new SecureRandom();
44
45     private long mostSignificantBits;
46     private long leastSignificantBits;
47
48     public UUID_(long mostSigBits, long leastSigBits) {
49         mostSignificantBits = mostSigBits;
50         leastSignificantBits = leastSigBits;
51     }
52
53     public static UUID_ randomUUID() {
54         byte[] bytes = new byte[16];
55         randomGenerator.nextBytes(bytes);
56         return newInstance(bytes, 4);
57     }
58
59     public static UUID_ nameUUIDFromBytes(byte[] name) {
60         try {
61             return newInstance(MessageDigest.getInstance("MD5").digest(name), 3);
62         } catch (NoSuchAlgorithmException e) {
63             throw new InternalError JavaDoc(e.getMessage());
64         }
65     }
66
67     public static UUID_ fromString(String JavaDoc name) {
68         String JavaDoc[] values = name.split("-");
69         if (values.length != 5) throw new IllegalArgumentException JavaDoc(name);
70         try {
71             return new UUID_(Long.parseLong(values[0], 16) << 32 | Long.parseLong(values[1], 16) << 16 |
72                     Long.parseLong(values[2], 16), Long.parseLong(values[3], 16) << 48 | Long.parseLong(values[4], 16));
73         } catch (NumberFormatException JavaDoc e) {
74             throw new IllegalStateException JavaDoc(name);
75         }
76     }
77
78     public long getMostSignificantBits() {
79         return mostSignificantBits;
80     }
81
82     public long getLeastSignificantBits() {
83         return leastSignificantBits;
84     }
85
86     public int version() {
87         return (int) (mostSignificantBits >>> 12) & 0x0F;
88     }
89
90     public int variant() {
91         if (leastSignificantBits >>> 63 == 0) return 0;
92         if (leastSignificantBits >>> 62 == 2) return 2;
93         return (int) (leastSignificantBits >>> 61);
94     }
95
96     public long timestamp() {
97         assertVersion1();
98         return (mostSignificantBits & 0x00000FFFL) << 48 |
99                 (mostSignificantBits & 0xFFFF0000L) << 16 |
100                 mostSignificantBits >>> 32;
101     }
102
103     public int clockSequence() {
104         assertVersion1();
105         return (int) (leastSignificantBits >>> 48) & 0x3FFF;
106     }
107
108     public long node() {
109         assertVersion1();
110         return leastSignificantBits & 0x0000FFFFFFFFFFFFL;
111     }
112
113     public String JavaDoc toString() {
114         return hex(mostSignificantBits >> 32, 8) +
115                 '-' + hex(mostSignificantBits >> 16, 4) +
116                 '-' + hex(mostSignificantBits, 4) +
117                 '-' + hex(leastSignificantBits >> 48, 4) +
118                 '-' + hex(leastSignificantBits, 12);
119     }
120
121     public int hashCode() {
122         return (int) (mostSignificantBits >> 32 ^ mostSignificantBits
123                 ^ leastSignificantBits >> 32 ^ leastSignificantBits);
124     }
125
126     public boolean equals(Object JavaDoc obj) {
127         if (obj instanceof UUID_) {
128             UUID_ val = (UUID_) obj;
129             return mostSignificantBits == val.mostSignificantBits &&
130                     leastSignificantBits == val.leastSignificantBits;
131         }
132         return false;
133     }
134
135     public int compareTo(UUID_ val) {
136         if (mostSignificantBits > val.mostSignificantBits) return 1;
137         if (mostSignificantBits < val.mostSignificantBits) return -1;
138         if (leastSignificantBits > val.leastSignificantBits) return 1;
139         if (leastSignificantBits < val.leastSignificantBits) return -1;
140         return 0;
141     }
142
143     private static UUID_ newInstance(byte[] bytes, int version) {
144         return new UUID_(getLong(bytes, 0) & ~0xF000L | version << 12,
145                 getLong(bytes, 8) & ~0xC000000000000000L | 0x8000000000000000L);
146     }
147
148     private static long getLong(byte[] bytes, int offset) {
149         long result = 0;
150         for (int i = 0; i < 8; i++) {
151             result = result << 8 | bytes[i + offset] & 0xFF;
152         }
153         return result;
154     }
155
156     private static String JavaDoc hex(long value, int length) {
157         String JavaDoc s = Long.toHexString(1L << (length << 2) | value);
158         return s.substring(s.length() - length);
159     }
160
161     private void assertVersion1() {
162         if (version() != 1) throw new UnsupportedOperationException JavaDoc("Not a version 1 UUID");
163     }
164
165 }
166
Popular Tags