src/java.base/share/classes/java/util/UUID.java
author mchung
Tue, 06 Nov 2018 10:01:16 -0800
changeset 52427 3c6aa484536c
parent 47216 71c04702a3d5
child 57956 e0b8b019d2f5
permissions -rw-r--r--
8211122: Reduce the number of internal classes made accessible to jdk.unsupported Reviewed-by: alanb, dfuchs, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, 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
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 47216
diff changeset
    30
import jdk.internal.access.JavaLangAccess;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 47216
diff changeset
    31
import jdk.internal.access.SharedSecrets;
25668
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
    32
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A class that represents an immutable universally unique identifier (UUID).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A UUID represents a 128-bit value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p> There exist different variants of these global identifiers.  The methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * of this class are for manipulating the Leach-Salz variant, although the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * constructors allow the creation of any variant of UUID (described below).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p> The layout of a variant 2 (Leach-Salz) UUID is as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The most significant long consists of the following unsigned fields:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * 0xFFFFFFFF00000000 time_low
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * 0x00000000FFFF0000 time_mid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * 0x000000000000F000 version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * 0x0000000000000FFF time_hi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The least significant long consists of the following unsigned fields:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * 0xC000000000000000 variant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * 0x3FFF000000000000 clock_seq
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * 0x0000FFFFFFFFFFFF node
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p> The variant field contains a value which identifies the layout of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * {@code UUID}.  The bit layout described above is valid only for a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * UUID} with a variant value of 2, which indicates the Leach-Salz variant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p> The version field holds a value that describes the type of this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * UUID}.  There are four different basic types of UUIDs: time-based, DCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * security, name-based, and randomly generated UUIDs.  These types have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * version value of 1, 2, 3 and 4, respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p> For more information including algorithms used to create {@code UUID}s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * see <a href="http://www.ietf.org/rfc/rfc4122.txt"> <i>RFC&nbsp;4122: A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Universally Unique IDentifier (UUID) URN Namespace</i></a>, section 4.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * &quot;Algorithms for Creating a Time-Based UUID&quot;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public final class UUID implements java.io.Serializable, Comparable<UUID> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Explicit serialVersionUID for interoperability.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final long serialVersionUID = -4856846361193249489L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * The most significant 64 bits of this UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private final long mostSigBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * The least significant 64 bits of this UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private final long leastSigBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
25668
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
    94
    private static final JavaLangAccess jla = SharedSecrets.getJavaLangAccess();
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
    95
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The random number generator used by this class to create random
12690
a2d28a91f9b7 7071826: Avoid benign race condition in initialization of UUID
mduigou
parents: 10419
diff changeset
    98
     * based UUIDs. In a holder class to defer initialization until needed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
12690
a2d28a91f9b7 7071826: Avoid benign race condition in initialization of UUID
mduigou
parents: 10419
diff changeset
   100
    private static class Holder {
a2d28a91f9b7 7071826: Avoid benign race condition in initialization of UUID
mduigou
parents: 10419
diff changeset
   101
        static final SecureRandom numberGenerator = new SecureRandom();
a2d28a91f9b7 7071826: Avoid benign race condition in initialization of UUID
mduigou
parents: 10419
diff changeset
   102
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // Constructors and Factories
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Private constructor which uses a byte array to construct the new UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private UUID(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        long msb = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        long lsb = 0;
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   112
        assert data.length == 16 : "data must be 16 bytes in length";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        for (int i=0; i<8; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            msb = (msb << 8) | (data[i] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        for (int i=8; i<16; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            lsb = (lsb << 8) | (data[i] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this.mostSigBits = msb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this.leastSigBits = lsb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Constructs a new {@code UUID} using the specified data.  {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * mostSigBits} is used for the most significant 64 bits of the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * UUID} and {@code leastSigBits} becomes the least significant 64 bits of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * the {@code UUID}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param  mostSigBits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *         The most significant bits of the {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param  leastSigBits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *         The least significant bits of the {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public UUID(long mostSigBits, long leastSigBits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        this.mostSigBits = mostSigBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        this.leastSigBits = leastSigBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Static factory to retrieve a type 4 (pseudo randomly generated) UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * The {@code UUID} is generated using a cryptographically strong pseudo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * random number generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return  A randomly generated {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public static UUID randomUUID() {
12690
a2d28a91f9b7 7071826: Avoid benign race condition in initialization of UUID
mduigou
parents: 10419
diff changeset
   147
        SecureRandom ng = Holder.numberGenerator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        byte[] randomBytes = new byte[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        ng.nextBytes(randomBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        randomBytes[6]  &= 0x0f;  /* clear version        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        randomBytes[6]  |= 0x40;  /* set to version 4     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        randomBytes[8]  &= 0x3f;  /* clear variant        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        randomBytes[8]  |= 0x80;  /* set to IETF variant  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return new UUID(randomBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Static factory to retrieve a type 3 (name based) {@code UUID} based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * the specified byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *         A byte array to be used to construct a {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return  A {@code UUID} generated from the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public static UUID nameUUIDFromBytes(byte[] name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        MessageDigest md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            md = MessageDigest.getInstance("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } catch (NoSuchAlgorithmException nsae) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
   172
            throw new InternalError("MD5 not supported", nsae);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        byte[] md5Bytes = md.digest(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        md5Bytes[6]  &= 0x0f;  /* clear version        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        md5Bytes[6]  |= 0x30;  /* set to version 3     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        md5Bytes[8]  &= 0x3f;  /* clear variant        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        md5Bytes[8]  |= 0x80;  /* set to IETF variant  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return new UUID(md5Bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Creates a {@code UUID} from the string standard representation as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * described in the {@link #toString} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *         A string that specifies a {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return  A {@code UUID} with the specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *          If name does not conform to the string representation as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *          described in {@link #toString}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public static UUID fromString(String name) {
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   197
        int len = name.length();
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   198
        if (len > 36) {
25668
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   199
            throw new IllegalArgumentException("UUID string too large");
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   200
        }
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   201
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   202
        int dash1 = name.indexOf('-', 0);
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   203
        int dash2 = name.indexOf('-', dash1 + 1);
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   204
        int dash3 = name.indexOf('-', dash2 + 1);
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   205
        int dash4 = name.indexOf('-', dash3 + 1);
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   206
        int dash5 = name.indexOf('-', dash4 + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
25668
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   208
        // For any valid input, dash1 through dash4 will be positive and dash5
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   209
        // negative, but it's enough to check dash4 and dash5:
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   210
        // - if dash1 is -1, dash4 will be -1
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   211
        // - if dash1 is positive but dash2 is -1, dash4 will be -1
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   212
        // - if dash1 and dash2 is positive, dash3 will be -1, dash4 will be
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   213
        //   positive, but so will dash5
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   214
        if (dash4 < 0 || dash5 >= 0) {
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   215
            throw new IllegalArgumentException("Invalid UUID string: " + name);
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   216
        }
a5d9c415d1fd 8006627: UUID to/from String performance should be improved by reducing object allocations
redestad
parents: 23010
diff changeset
   217
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   218
        long mostSigBits = Long.parseLong(name, 0, dash1, 16) & 0xffffffffL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        mostSigBits <<= 16;
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   220
        mostSigBits |= Long.parseLong(name, dash1 + 1, dash2, 16) & 0xffffL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        mostSigBits <<= 16;
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   222
        mostSigBits |= Long.parseLong(name, dash2 + 1, dash3, 16) & 0xffffL;
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   223
        long leastSigBits = Long.parseLong(name, dash3 + 1, dash4, 16) & 0xffffL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        leastSigBits <<= 48;
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   225
        leastSigBits |= Long.parseLong(name, dash4 + 1, len, 16) & 0xffffffffffffL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return new UUID(mostSigBits, leastSigBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    // Field Accessor Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Returns the least significant 64 bits of this UUID's 128 bit value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return  The least significant 64 bits of this UUID's 128 bit value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public long getLeastSignificantBits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return leastSigBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Returns the most significant 64 bits of this UUID's 128 bit value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @return  The most significant 64 bits of this UUID's 128 bit value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public long getMostSignificantBits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return mostSigBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * The version number associated with this {@code UUID}.  The version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * number describes how this {@code UUID} was generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * The version number has the following meaning:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19074
diff changeset
   255
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <li>1    Time-based UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <li>2    DCE security UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <li>3    Name-based UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <li>4    Randomly generated UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @return  The version number of this {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public int version() {
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   265
        // Version is bits masked by 0x000000000000F000 in MS long
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   266
        return (int)((mostSigBits >> 12) & 0x0f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * The variant number associated with this {@code UUID}.  The variant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * number describes the layout of the {@code UUID}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * The variant number has the following meaning:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19074
diff changeset
   274
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <li>0    Reserved for NCS backward compatibility
12690
a2d28a91f9b7 7071826: Avoid benign race condition in initialization of UUID
mduigou
parents: 10419
diff changeset
   276
     * <li>2    <a href="http://www.ietf.org/rfc/rfc4122.txt">IETF&nbsp;RFC&nbsp;4122</a>
a2d28a91f9b7 7071826: Avoid benign race condition in initialization of UUID
mduigou
parents: 10419
diff changeset
   277
     * (Leach-Salz), used by this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <li>6    Reserved, Microsoft Corporation backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * <li>7    Reserved for future definition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @return  The variant number of this {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public int variant() {
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   285
        // This field is composed of a varying number of bits.
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   286
        // 0    -    -    Reserved for NCS backward compatibility
12690
a2d28a91f9b7 7071826: Avoid benign race condition in initialization of UUID
mduigou
parents: 10419
diff changeset
   287
        // 1    0    -    The IETF aka Leach-Salz variant (used by this class)
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   288
        // 1    1    0    Reserved, Microsoft backward compatibility
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   289
        // 1    1    1    Reserved for future definition.
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   290
        return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62)))
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   291
                      & (leastSigBits >> 63));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * The timestamp value associated with this UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <p> The 60 bit timestamp value is constructed from the time_low,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * time_mid, and time_hi fields of this {@code UUID}.  The resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * timestamp is measured in 100-nanosecond units since midnight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * October 15, 1582 UTC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <p> The timestamp value is only meaningful in a time-based UUID, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * has version type 1.  If this {@code UUID} is not a time-based UUID then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * this method throws UnsupportedOperationException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @throws UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *         If this UUID is not a version 1 UUID
19074
84a8d23e8f32 8020539: Clean up doclint problems in java.util package, part 2
bpb
parents: 14342
diff changeset
   308
     * @return The timestamp of this {@code UUID}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public long timestamp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (version() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            throw new UnsupportedOperationException("Not a time-based UUID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   314
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   315
        return (mostSigBits & 0x0FFFL) << 48
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   316
             | ((mostSigBits >> 16) & 0x0FFFFL) << 32
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   317
             | mostSigBits >>> 32;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * The clock sequence value associated with this UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <p> The 14 bit clock sequence value is constructed from the clock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * sequence field of this UUID.  The clock sequence field is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * guarantee temporal uniqueness in a time-based UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <p> The {@code clockSequence} value is only meaningful in a time-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * UUID, which has version type 1.  If this UUID is not a time-based UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * then this method throws UnsupportedOperationException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @return  The clock sequence of this {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @throws  UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *          If this UUID is not a version 1 UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public int clockSequence() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (version() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            throw new UnsupportedOperationException("Not a time-based UUID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   340
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   341
        return (int)((leastSigBits & 0x3FFF000000000000L) >>> 48);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * The node value associated with this UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * <p> The 48 bit node value is constructed from the node field of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * UUID.  This field is intended to hold the IEEE 802 address of the machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * that generated this UUID to guarantee spatial uniqueness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * <p> The node value is only meaningful in a time-based UUID, which has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * version type 1.  If this UUID is not a time-based UUID then this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * throws UnsupportedOperationException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @return  The node value of this {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @throws  UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *          If this UUID is not a version 1 UUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public long node() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (version() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            throw new UnsupportedOperationException("Not a time-based UUID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   364
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   365
        return leastSigBits & 0x0000FFFFFFFFFFFFL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    // Object Inherited Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Returns a {@code String} object representing this {@code UUID}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <p> The UUID string representation is as described by this BNF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * UUID                   = <time_low> "-" <time_mid> "-"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *                          <time_high_and_version> "-"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *                          <variant_and_sequence> "-"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *                          <node>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * time_low               = 4*<hexOctet>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * time_mid               = 2*<hexOctet>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * time_high_and_version  = 2*<hexOctet>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * variant_and_sequence   = 2*<hexOctet>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * node                   = 6*<hexOctet>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * hexOctet               = <hexDigit><hexDigit>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * hexDigit               =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *       "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *       | "a" | "b" | "c" | "d" | "e" | "f"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *       | "A" | "B" | "C" | "D" | "E" | "F"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * }</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @return  A string representation of this {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public String toString() {
35707
66cdb7e01a3e 8148936: Adapt UUID.toString() to Compact Strings
shade
parents: 32834
diff changeset
   395
        return jla.fastUUID(leastSigBits, mostSigBits);
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
     * Returns a hash code for this {@code UUID}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @return  A hash code value for this {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public int hashCode() {
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   404
        long hilo = mostSigBits ^ leastSigBits;
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   405
        return ((int)(hilo >> 32)) ^ (int) hilo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Compares this object to the specified object.  The result is {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * true} if and only if the argument is not {@code null}, is a {@code UUID}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * object, has the same variant, and contains the same value, bit for bit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * as this {@code UUID}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @param  obj
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *         The object to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @return  {@code true} if the objects are the same; {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *          otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public boolean equals(Object obj) {
8766
9203cd75421b 6611830: UUID thread-safety and performance improvements
mduigou
parents: 5506
diff changeset
   421
        if ((null == obj) || (obj.getClass() != UUID.class))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        UUID id = (UUID)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return (mostSigBits == id.mostSigBits &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                leastSigBits == id.leastSigBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    // Comparison Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Compares this UUID with the specified UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * <p> The first of two UUIDs is greater than the second if the most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * significant field in which the UUIDs differ is greater for the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * UUID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @param  val
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *         {@code UUID} to which this {@code UUID} is to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @return  -1, 0 or 1 as this {@code UUID} is less than, equal to, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *          greater than {@code val}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public int compareTo(UUID val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        // The ordering is intentionally set up so that the UUIDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        // can simply be numerically compared as two numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return (this.mostSigBits < val.mostSigBits ? -1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                (this.mostSigBits > val.mostSigBits ? 1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                 (this.leastSigBits < val.leastSigBits ? -1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                  (this.leastSigBits > val.leastSigBits ? 1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                   0))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
}