jdk/src/share/classes/java/util/UUID.java
changeset 12690 a2d28a91f9b7
parent 10419 12c063b39232
child 14342 8435a30053c1
equal deleted inserted replaced
12689:35cbede4f958 12690:a2d28a91f9b7
    88      */
    88      */
    89     private final long leastSigBits;
    89     private final long leastSigBits;
    90 
    90 
    91     /*
    91     /*
    92      * The random number generator used by this class to create random
    92      * The random number generator used by this class to create random
    93      * based UUIDs.
    93      * based UUIDs. In a holder class to defer initialization until needed.
    94      */
    94      */
    95     private static volatile SecureRandom numberGenerator = null;
    95     private static class Holder {
       
    96         static final SecureRandom numberGenerator = new SecureRandom();
       
    97     }
    96 
    98 
    97     // Constructors and Factories
    99     // Constructors and Factories
    98 
   100 
    99     /*
   101     /*
   100      * Private constructor which uses a byte array to construct the new UUID.
   102      * Private constructor which uses a byte array to construct the new UUID.
   135      * random number generator.
   137      * random number generator.
   136      *
   138      *
   137      * @return  A randomly generated {@code UUID}
   139      * @return  A randomly generated {@code UUID}
   138      */
   140      */
   139     public static UUID randomUUID() {
   141     public static UUID randomUUID() {
   140         SecureRandom ng = numberGenerator;
   142         SecureRandom ng = Holder.numberGenerator;
   141         if (ng == null) {
       
   142             numberGenerator = ng = new SecureRandom();
       
   143         }
       
   144 
   143 
   145         byte[] randomBytes = new byte[16];
   144         byte[] randomBytes = new byte[16];
   146         ng.nextBytes(randomBytes);
   145         ng.nextBytes(randomBytes);
   147         randomBytes[6]  &= 0x0f;  /* clear version        */
   146         randomBytes[6]  &= 0x0f;  /* clear version        */
   148         randomBytes[6]  |= 0x40;  /* set to version 4     */
   147         randomBytes[6]  |= 0x40;  /* set to version 4     */
   253      * number describes the layout of the {@code UUID}.
   252      * number describes the layout of the {@code UUID}.
   254      *
   253      *
   255      * The variant number has the following meaning:
   254      * The variant number has the following meaning:
   256      * <p><ul>
   255      * <p><ul>
   257      * <li>0    Reserved for NCS backward compatibility
   256      * <li>0    Reserved for NCS backward compatibility
   258      * <li>2    The Leach-Salz variant (used by this class)
   257      * <li>2    <a href="http://www.ietf.org/rfc/rfc4122.txt">IETF&nbsp;RFC&nbsp;4122</a>
       
   258      * (Leach-Salz), used by this class
   259      * <li>6    Reserved, Microsoft Corporation backward compatibility
   259      * <li>6    Reserved, Microsoft Corporation backward compatibility
   260      * <li>7    Reserved for future definition
   260      * <li>7    Reserved for future definition
   261      * </ul>
   261      * </ul>
   262      *
   262      *
   263      * @return  The variant number of this {@code UUID}
   263      * @return  The variant number of this {@code UUID}
   264      */
   264      */
   265     public int variant() {
   265     public int variant() {
   266         // This field is composed of a varying number of bits.
   266         // This field is composed of a varying number of bits.
   267         // 0    -    -    Reserved for NCS backward compatibility
   267         // 0    -    -    Reserved for NCS backward compatibility
   268         // 1    0    -    The Leach-Salz variant (used by this class)
   268         // 1    0    -    The IETF aka Leach-Salz variant (used by this class)
   269         // 1    1    0    Reserved, Microsoft backward compatibility
   269         // 1    1    0    Reserved, Microsoft backward compatibility
   270         // 1    1    1    Reserved for future definition.
   270         // 1    1    1    Reserved for future definition.
   271         return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62)))
   271         return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62)))
   272                       & (leastSigBits >> 63));
   272                       & (leastSigBits >> 63));
   273     }
   273     }