jdk/src/share/classes/java/util/UUID.java
author yhuang
Wed, 14 Mar 2012 23:07:21 -0700
changeset 12135 078775fa4b30
parent 10419 12c063b39232
child 12690 a2d28a91f9b7
permissions -rw-r--r--
7085757: Currency Data: ISO 4217 Amendment 152 Reviewed-by: naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8766
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A class that represents an immutable universally unique identifier (UUID).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A UUID represents a 128-bit value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p> There exist different variants of these global identifiers.  The methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * of this class are for manipulating the Leach-Salz variant, although the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * constructors allow the creation of any variant of UUID (described below).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p> The layout of a variant 2 (Leach-Salz) UUID is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The most significant long consists of the following unsigned fields:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * 0xFFFFFFFF00000000 time_low
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * 0x00000000FFFF0000 time_mid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * 0x000000000000F000 version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * 0x0000000000000FFF time_hi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The least significant long consists of the following unsigned fields:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * 0xC000000000000000 variant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * 0x3FFF000000000000 clock_seq
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * 0x0000FFFFFFFFFFFF node
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p> The variant field contains a value which identifies the layout of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * {@code UUID}.  The bit layout described above is valid only for a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * UUID} with a variant value of 2, which indicates the Leach-Salz variant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p> The version field holds a value that describes the type of this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * UUID}.  There are four different basic types of UUIDs: time-based, DCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * security, name-based, and randomly generated UUIDs.  These types have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * version value of 1, 2, 3 and 4, respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p> For more information including algorithms used to create {@code UUID}s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * see <a href="http://www.ietf.org/rfc/rfc4122.txt"> <i>RFC&nbsp;4122: A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Universally Unique IDentifier (UUID) URN Namespace</i></a>, section 4.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * &quot;Algorithms for Creating a Time-Based UUID&quot;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public final class UUID implements java.io.Serializable, Comparable<UUID> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Explicit serialVersionUID for interoperability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final long serialVersionUID = -4856846361193249489L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * The most significant 64 bits of this UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private final long mostSigBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * The least significant 64 bits of this UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private final long leastSigBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * The random number generator used by this class to create random
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * based UUIDs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static volatile SecureRandom numberGenerator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // Constructors and Factories
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Private constructor which uses a byte array to construct the new UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private UUID(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        long msb = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        long lsb = 0;
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   105
        assert data.length == 16 : "data must be 16 bytes in length";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        for (int i=0; i<8; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            msb = (msb << 8) | (data[i] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        for (int i=8; i<16; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            lsb = (lsb << 8) | (data[i] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.mostSigBits = msb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.leastSigBits = lsb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Constructs a new {@code UUID} using the specified data.  {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * mostSigBits} is used for the most significant 64 bits of the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * UUID} and {@code leastSigBits} becomes the least significant 64 bits of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * the {@code UUID}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param  mostSigBits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *         The most significant bits of the {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param  leastSigBits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *         The least significant bits of the {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public UUID(long mostSigBits, long leastSigBits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        this.mostSigBits = mostSigBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        this.leastSigBits = leastSigBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Static factory to retrieve a type 4 (pseudo randomly generated) UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * The {@code UUID} is generated using a cryptographically strong pseudo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * random number generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return  A randomly generated {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static UUID randomUUID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        SecureRandom ng = numberGenerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (ng == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            numberGenerator = ng = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        byte[] randomBytes = new byte[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        ng.nextBytes(randomBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        randomBytes[6]  &= 0x0f;  /* clear version        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        randomBytes[6]  |= 0x40;  /* set to version 4     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        randomBytes[8]  &= 0x3f;  /* clear variant        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        randomBytes[8]  |= 0x80;  /* set to IETF variant  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return new UUID(randomBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Static factory to retrieve a type 3 (name based) {@code UUID} based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * the specified byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *         A byte array to be used to construct a {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return  A {@code UUID} generated from the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public static UUID nameUUIDFromBytes(byte[] name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        MessageDigest md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            md = MessageDigest.getInstance("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } catch (NoSuchAlgorithmException nsae) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
   168
            throw new InternalError("MD5 not supported", nsae);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        byte[] md5Bytes = md.digest(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        md5Bytes[6]  &= 0x0f;  /* clear version        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        md5Bytes[6]  |= 0x30;  /* set to version 3     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        md5Bytes[8]  &= 0x3f;  /* clear variant        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        md5Bytes[8]  |= 0x80;  /* set to IETF variant  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return new UUID(md5Bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Creates a {@code UUID} from the string standard representation as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * described in the {@link #toString} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *         A string that specifies a {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return  A {@code UUID} with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *          If name does not conform to the string representation as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *          described in {@link #toString}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public static UUID fromString(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        String[] components = name.split("-");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (components.length != 5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            throw new IllegalArgumentException("Invalid UUID string: "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        for (int i=0; i<5; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            components[i] = "0x"+components[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        long mostSigBits = Long.decode(components[0]).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        mostSigBits <<= 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        mostSigBits |= Long.decode(components[1]).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        mostSigBits <<= 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        mostSigBits |= Long.decode(components[2]).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        long leastSigBits = Long.decode(components[3]).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        leastSigBits <<= 48;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        leastSigBits |= Long.decode(components[4]).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return new UUID(mostSigBits, leastSigBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    // Field Accessor Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Returns the least significant 64 bits of this UUID's 128 bit value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @return  The least significant 64 bits of this UUID's 128 bit value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public long getLeastSignificantBits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return leastSigBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Returns the most significant 64 bits of this UUID's 128 bit value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @return  The most significant 64 bits of this UUID's 128 bit value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public long getMostSignificantBits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return mostSigBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * The version number associated with this {@code UUID}.  The version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * number describes how this {@code UUID} was generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * The version number has the following meaning:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <p><ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <li>1    Time-based UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <li>2    DCE security UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * <li>3    Name-based UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <li>4    Randomly generated UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @return  The version number of this {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public int version() {
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   247
        // Version is bits masked by 0x000000000000F000 in MS long
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   248
        return (int)((mostSigBits >> 12) & 0x0f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * The variant number associated with this {@code UUID}.  The variant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * number describes the layout of the {@code UUID}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * The variant number has the following meaning:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <p><ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <li>0    Reserved for NCS backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <li>2    The Leach-Salz variant (used by this class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <li>6    Reserved, Microsoft Corporation backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <li>7    Reserved for future definition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @return  The variant number of this {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public int variant() {
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   266
        // This field is composed of a varying number of bits.
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   267
        // 0    -    -    Reserved for NCS backward compatibility
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   268
        // 1    0    -    The Leach-Salz variant (used by this class)
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   269
        // 1    1    0    Reserved, Microsoft backward compatibility
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   270
        // 1    1    1    Reserved for future definition.
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   271
        return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62)))
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   272
                      & (leastSigBits >> 63));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * The timestamp value associated with this UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <p> The 60 bit timestamp value is constructed from the time_low,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * time_mid, and time_hi fields of this {@code UUID}.  The resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * timestamp is measured in 100-nanosecond units since midnight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * October 15, 1582 UTC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * <p> The timestamp value is only meaningful in a time-based UUID, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * has version type 1.  If this {@code UUID} is not a time-based UUID then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * this method throws UnsupportedOperationException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @throws UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *         If this UUID is not a version 1 UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public long timestamp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (version() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            throw new UnsupportedOperationException("Not a time-based UUID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   294
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   295
        return (mostSigBits & 0x0FFFL) << 48
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   296
             | ((mostSigBits >> 16) & 0x0FFFFL) << 32
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   297
             | mostSigBits >>> 32;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * The clock sequence value associated with this UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <p> The 14 bit clock sequence value is constructed from the clock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * sequence field of this UUID.  The clock sequence field is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * guarantee temporal uniqueness in a time-based UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <p> The {@code clockSequence} value is only meaningful in a time-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * UUID, which has version type 1.  If this UUID is not a time-based UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * then this method throws UnsupportedOperationException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @return  The clock sequence of this {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @throws  UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *          If this UUID is not a version 1 UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public int clockSequence() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (version() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            throw new UnsupportedOperationException("Not a time-based UUID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   320
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   321
        return (int)((leastSigBits & 0x3FFF000000000000L) >>> 48);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * The node value associated with this UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <p> The 48 bit node value is constructed from the node field of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * UUID.  This field is intended to hold the IEEE 802 address of the machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * that generated this UUID to guarantee spatial uniqueness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <p> The node value is only meaningful in a time-based UUID, which has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * version type 1.  If this UUID is not a time-based UUID then this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * throws UnsupportedOperationException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return  The node value of this {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @throws  UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *          If this UUID is not a version 1 UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public long node() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (version() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            throw new UnsupportedOperationException("Not a time-based UUID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   344
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   345
        return leastSigBits & 0x0000FFFFFFFFFFFFL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    // Object Inherited Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Returns a {@code String} object representing this {@code UUID}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <p> The UUID string representation is as described by this BNF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * UUID                   = <time_low> "-" <time_mid> "-"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *                          <time_high_and_version> "-"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *                          <variant_and_sequence> "-"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *                          <node>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * time_low               = 4*<hexOctet>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * time_mid               = 2*<hexOctet>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * time_high_and_version  = 2*<hexOctet>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * variant_and_sequence   = 2*<hexOctet>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * node                   = 6*<hexOctet>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * hexOctet               = <hexDigit><hexDigit>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * hexDigit               =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *       "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *       | "a" | "b" | "c" | "d" | "e" | "f"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *       | "A" | "B" | "C" | "D" | "E" | "F"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * }</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @return  A string representation of this {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return (digits(mostSigBits >> 32, 8) + "-" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                digits(mostSigBits >> 16, 4) + "-" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                digits(mostSigBits, 4) + "-" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                digits(leastSigBits >> 48, 4) + "-" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                digits(leastSigBits, 12));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /** Returns val represented by the specified number of hex digits. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    private static String digits(long val, int digits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        long hi = 1L << (digits * 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return Long.toHexString(hi | (val & (hi - 1))).substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Returns a hash code for this {@code UUID}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @return  A hash code value for this {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public int hashCode() {
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   394
        long hilo = mostSigBits ^ leastSigBits;
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   395
        return ((int)(hilo >> 32)) ^ (int) hilo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Compares this object to the specified object.  The result is {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * true} if and only if the argument is not {@code null}, is a {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * object, has the same variant, and contains the same value, bit for bit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * as this {@code UUID}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @param  obj
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *         The object to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @return  {@code true} if the objects are the same; {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *          otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public boolean equals(Object obj) {
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   411
        if ((null == obj) || (obj.getClass() != UUID.class))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        UUID id = (UUID)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return (mostSigBits == id.mostSigBits &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                leastSigBits == id.leastSigBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    // Comparison Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Compares this UUID with the specified UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * <p> The first of two UUIDs is greater than the second if the most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * significant field in which the UUIDs differ is greater for the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param  val
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *         {@code UUID} to which this {@code UUID} is to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @return  -1, 0 or 1 as this {@code UUID} is less than, equal to, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *          greater than {@code val}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public int compareTo(UUID val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        // The ordering is intentionally set up so that the UUIDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        // can simply be numerically compared as two numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return (this.mostSigBits < val.mostSigBits ? -1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                (this.mostSigBits > val.mostSigBits ? 1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                 (this.leastSigBits < val.leastSigBits ? -1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                  (this.leastSigBits > val.leastSigBits ? 1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                   0))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
}