summaryrefslogtreecommitdiff
path: root/test/disabled/presentation/akka/src/com/eaio/uuid/UUID.java
blob: 6c49bcd1c87bb2d87904a70732e13e8a109d555b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * UUID.java
 *
 * Created 07.02.2003
 *
 * eaio: UUID - an implementation of the UUID specification
 * Copyright (c) 2003-2009 Johann Burkard (jb@eaio.com) http://eaio.com.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package com.eaio.uuid;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.omg.CORBA.portable.IDLEntity;

import com.eaio.util.lang.Hex;

/**
 * Creates UUIDs according to the DCE Universal Token Identifier specification.
 * <p>
 * All you need to know:
 * <pre>
 * UUID u = new UUID();
 * </pre>
 *
 * @see <a href="http://www.opengroup.org/onlinepubs/9629399/apdxa.htm">
 * http://www.opengroup.org/onlinepubs/9629399/apdxa.htm
 * </a>
 * @see <a href="http://www.uddi.org/pubs/draft-leach-uuids-guids-01.txt">
 * http://www.uddi.org/pubs/draft-leach-uuids-guids-01.txt
 * </a>
 * @see <a href="http://johannburkard.de/software/uuid/">UUID</a>
 * @author <a href="mailto:jb@eaio.de">Johann Burkard</a>
 * @version $Id: UUID.java 1888 2009-03-15 12:43:24Z johann $
 */
public class UUID implements Comparable<UUID>, Serializable, Cloneable,
        IDLEntity {

    /**
     * Hasn't ever changed between versions.
     */
    static final long serialVersionUID = 7435962790062944603L;

    /**
     * The time field of the UUID.
     *
     * @serial
     */
    public long time;

    /**
     * The clock sequence and node field of the UUID.
     *
     * @serial
     */
    public long clockSeqAndNode;

    /**
     * Constructor for UUID. Constructs a new, unique UUID.
     *
     * @see UUIDGen#newTime()
     * @see UUIDGen#getClockSeqAndNode()
     */
    public UUID() {
        this(UUIDGen.newTime(), UUIDGen.getClockSeqAndNode());
    }

    /**
     * Constructor for UUID. Constructs a UUID from two <code>long</code> values.
     *
     * @param time the upper 64 bits
     * @param clockSeqAndNode the lower 64 bits
     */
    public UUID(long time, long clockSeqAndNode) {
        this.time = time;
        this.clockSeqAndNode = clockSeqAndNode;
    }

    /**
     * Copy constructor for UUID. Values of the given UUID are copied.
     *
     * @param u the UUID, may not be <code>null</code>
     */
    public UUID(UUID u) {
        this(u.time, u.clockSeqAndNode);
    }

    /**
     * Parses a textual representation of a UUID.
     * <p>
     * No validation is performed. If the {@link CharSequence} is shorter than 36 characters,
     * {@link ArrayIndexOutOfBoundsException}s will be thrown.
     *
     * @param s the {@link CharSequence}, may not be <code>null</code>
     */
    public UUID(CharSequence s) {
        this(Hex.parseLong(s.subSequence(0, 18)), Hex.parseLong(s.subSequence(
                19, 36)));
    }

    /**
     * Compares this UUID to another Object. Throws a {@link ClassCastException} if
     * the other Object is not an instance of the UUID class. Returns a value
     * smaller than zero if the other UUID is "larger" than this UUID and a value
     * larger than zero if the other UUID is "smaller" than this UUID.
     *
     * @param t the other UUID, may not be <code>null</code>
     * @return a value &lt; 0, 0 or a value &gt; 0
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     * @throws ClassCastException
     */
    public int compareTo(UUID t) {
        if (this == t) {
            return 0;
        }
        if (time > t.time) {
            return 1;
        }
        if (time < t.time) {
            return -1;
        }
        if (clockSeqAndNode > t.clockSeqAndNode) {
            return 1;
        }
        if (clockSeqAndNode < t.clockSeqAndNode) {
            return -1;
        }
        return 0;
    }

    /**
     * Tweaked Serialization routine.
     *
     * @param out the ObjectOutputStream
     * @throws IOException
     */
    private void writeObject(ObjectOutputStream out) throws IOException {
        out.writeLong(time);
        out.writeLong(clockSeqAndNode);
    }

    /**
     * Tweaked Serialization routine.
     *
     * @param in the ObjectInputStream
     * @throws IOException
     */
    private void readObject(ObjectInputStream in) throws IOException {
        time = in.readLong();
        clockSeqAndNode = in.readLong();
    }

    /**
     * Returns this UUID as a String.
     *
     * @return a String, never <code>null</code>
     * @see java.lang.Object#toString()
     * @see #toAppendable(Appendable)
     */
    @Override
    public final String toString() {
        return toAppendable(null).toString();
    }

    /**
     * Appends a String representation of this to the given {@link StringBuffer} or
     * creates a new one if none is given.
     *
     * @param in the StringBuffer to append to, may be <code>null</code>
     * @return a StringBuffer, never <code>null</code>
     * @see #toAppendable(Appendable)
     */
    public StringBuffer toStringBuffer(StringBuffer in) {
        StringBuffer out = in;
        if (out == null) {
            out = new StringBuffer(36);
        }
        else {
            out.ensureCapacity(out.length() + 36);
        }
        return (StringBuffer) toAppendable(out);
    }

    /**
     * Appends a String representation of this object to the given {@link Appendable} object.
     * <p>
     * For reasons I'll probably never understand, Sun has decided to have a number of I/O classes implement
     * Appendable which forced them to destroy an otherwise nice and simple interface with {@link IOException}s.
     * <p>
     * I decided to ignore any possible IOExceptions in this method.
     *
     * @param a the Appendable object, may be <code>null</code>
     * @return an Appendable object, defaults to a {@link StringBuilder} if <code>a</code> is <code>null</code>
     */
    public Appendable toAppendable(Appendable a) {
        Appendable out = a;
        if (out == null) {
            out = new StringBuilder(36);
        }
        try {
            Hex.append(out, (int) (time >> 32)).append('-');
            Hex.append(out, (short) (time >> 16)).append('-');
            Hex.append(out, (short) time).append('-');
            Hex.append(out, (short) (clockSeqAndNode >> 48)).append('-');
            Hex.append(out, clockSeqAndNode, 12);
        }
        catch (IOException ex) {
            // What were they thinking?
        }
        return out;
    }

    /**
     * Returns a hash code of this UUID. The hash code is calculated by XOR'ing the
     * upper 32 bits of the time and clockSeqAndNode fields and the lower 32 bits of
     * the time and clockSeqAndNode fields.
     *
     * @return an <code>int</code> representing the hash code
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return (int) ((time >> 32) ^ time ^ (clockSeqAndNode >> 32) ^ clockSeqAndNode);
    }

    /**
     * Clones this UUID.
     *
     * @return a new UUID with identical values, never <code>null</code>
     */
    @Override
    public Object clone() {
        try {
            return super.clone();
        }
        catch (CloneNotSupportedException ex) {
            // One of Sun's most epic fails.
            return null;
        }
    }

    /**
     * Returns the time field of the UUID (upper 64 bits).
     *
     * @return the time field
     */
    public final long getTime() {
        return time;
    }

    /**
     * Returns the clock and node field of the UUID (lower 64 bits).
     *
     * @return the clockSeqAndNode field
     */
    public final long getClockSeqAndNode() {
        return clockSeqAndNode;
    }

    /**
     * Compares two Objects for equality.
     *
     * @see java.lang.Object#equals(Object)
     * @param obj the Object to compare this UUID with, may be <code>null</code>
     * @return <code>true</code> if the other Object is equal to this UUID,
     * <code>false</code> if not
     */
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof UUID)) {
            return false;
        }
        return compareTo((UUID) obj) == 0;
    }

    /**
     * Returns the nil UUID (a UUID whose values are both set to zero).
     * <p>
     * Starting with version 2.0, this method does return a new UUID instance every
     * time it is called. Earlier versions returned one instance. This has now been
     * changed because this UUID has public, non-final instance fields. Returning a
     * new instance is therefore more safe.
     *
     * @return a nil UUID, never <code>null</code>
     */
    public static UUID nilUUID() {
        return new UUID(0, 0);
    }

}