src/share/classes/java/util/UUID.java

Print this page




  73      * Explicit serialVersionUID for interoperability.
  74      */
  75     private static final long serialVersionUID = -4856846361193249489L;
  76 
  77     /*
  78      * The most significant 64 bits of this UUID.
  79      *
  80      * @serial
  81      */
  82     private final long mostSigBits;
  83 
  84     /*
  85      * The least significant 64 bits of this UUID.
  86      *
  87      * @serial
  88      */
  89     private final long leastSigBits;
  90 
  91     /*
  92      * The random number generator used by this class to create random
  93      * based UUIDs.
  94      */
  95     private static volatile SecureRandom numberGenerator = null;


  96 
  97     // Constructors and Factories
  98 
  99     /*
 100      * Private constructor which uses a byte array to construct the new UUID.
 101      */
 102     private UUID(byte[] data) {
 103         long msb = 0;
 104         long lsb = 0;
 105         assert data.length == 16 : "data must be 16 bytes in length";
 106         for (int i=0; i<8; i++)
 107             msb = (msb << 8) | (data[i] & 0xff);
 108         for (int i=8; i<16; i++)
 109             lsb = (lsb << 8) | (data[i] & 0xff);
 110         this.mostSigBits = msb;
 111         this.leastSigBits = lsb;
 112     }
 113 
 114     /**
 115      * Constructs a new {@code UUID} using the specified data.  {@code


 120      * @param  mostSigBits
 121      *         The most significant bits of the {@code UUID}
 122      *
 123      * @param  leastSigBits
 124      *         The least significant bits of the {@code UUID}
 125      */
 126     public UUID(long mostSigBits, long leastSigBits) {
 127         this.mostSigBits = mostSigBits;
 128         this.leastSigBits = leastSigBits;
 129     }
 130 
 131     /**
 132      * Static factory to retrieve a type 4 (pseudo randomly generated) UUID.
 133      *
 134      * The {@code UUID} is generated using a cryptographically strong pseudo
 135      * random number generator.
 136      *
 137      * @return  A randomly generated {@code UUID}
 138      */
 139     public static UUID randomUUID() {
 140         SecureRandom ng = numberGenerator;
 141         if (ng == null) {
 142             numberGenerator = ng = new SecureRandom();
 143         }
 144 
 145         byte[] randomBytes = new byte[16];
 146         ng.nextBytes(randomBytes);
 147         randomBytes[6]  &= 0x0f;  /* clear version        */
 148         randomBytes[6]  |= 0x40;  /* set to version 4     */
 149         randomBytes[8]  &= 0x3f;  /* clear variant        */
 150         randomBytes[8]  |= 0x80;  /* set to IETF variant  */
 151         return new UUID(randomBytes);
 152     }
 153 
 154     /**
 155      * Static factory to retrieve a type 3 (name based) {@code UUID} based on
 156      * the specified byte array.
 157      *
 158      * @param  name
 159      *         A byte array to be used to construct a {@code UUID}
 160      *
 161      * @return  A {@code UUID} generated from the specified array
 162      */
 163     public static UUID nameUUIDFromBytes(byte[] name) {


 238      * <li>1    Time-based UUID
 239      * <li>2    DCE security UUID
 240      * <li>3    Name-based UUID
 241      * <li>4    Randomly generated UUID
 242      * </ul>
 243      *
 244      * @return  The version number of this {@code UUID}
 245      */
 246     public int version() {
 247         // Version is bits masked by 0x000000000000F000 in MS long
 248         return (int)((mostSigBits >> 12) & 0x0f);
 249     }
 250 
 251     /**
 252      * The variant number associated with this {@code UUID}.  The variant
 253      * number describes the layout of the {@code UUID}.
 254      *
 255      * The variant number has the following meaning:
 256      * <p><ul>
 257      * <li>0    Reserved for NCS backward compatibility
 258      * <li>2    The Leach-Salz variant (used by this class)
 259      * <li>6    Reserved, Microsoft Corporation backward compatibility
 260      * <li>7    Reserved for future definition
 261      * </ul>
 262      *
 263      * @return  The variant number of this {@code UUID}
 264      */
 265     public int variant() {
 266         // This field is composed of a varying number of bits.
 267         // 0    -    -    Reserved for NCS backward compatibility
 268         // 1    0    -    The Leach-Salz variant (used by this class)
 269         // 1    1    0    Reserved, Microsoft backward compatibility
 270         // 1    1    1    Reserved for future definition.
 271         return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62)))
 272                       & (leastSigBits >> 63));
 273     }
 274 
 275     /**
 276      * The timestamp value associated with this UUID.
 277      *
 278      * <p> The 60 bit timestamp value is constructed from the time_low,
 279      * time_mid, and time_hi fields of this {@code UUID}.  The resulting
 280      * timestamp is measured in 100-nanosecond units since midnight,
 281      * October 15, 1582 UTC.
 282      *
 283      * <p> The timestamp value is only meaningful in a time-based UUID, which
 284      * has version type 1.  If this {@code UUID} is not a time-based UUID then
 285      * this method throws UnsupportedOperationException.
 286      *
 287      * @throws UnsupportedOperationException
 288      *         If this UUID is not a version 1 UUID




  73      * Explicit serialVersionUID for interoperability.
  74      */
  75     private static final long serialVersionUID = -4856846361193249489L;
  76 
  77     /*
  78      * The most significant 64 bits of this UUID.
  79      *
  80      * @serial
  81      */
  82     private final long mostSigBits;
  83 
  84     /*
  85      * The least significant 64 bits of this UUID.
  86      *
  87      * @serial
  88      */
  89     private final long leastSigBits;
  90 
  91     /*
  92      * The random number generator used by this class to create random
  93      * based UUIDs. In a holder class to defer initialization until needed.
  94      */
  95     private static class Holder {
  96         static final SecureRandom numberGenerator = new SecureRandom();
  97     }
  98 
  99     // Constructors and Factories
 100 
 101     /*
 102      * Private constructor which uses a byte array to construct the new UUID.
 103      */
 104     private UUID(byte[] data) {
 105         long msb = 0;
 106         long lsb = 0;
 107         assert data.length == 16 : "data must be 16 bytes in length";
 108         for (int i=0; i<8; i++)
 109             msb = (msb << 8) | (data[i] & 0xff);
 110         for (int i=8; i<16; i++)
 111             lsb = (lsb << 8) | (data[i] & 0xff);
 112         this.mostSigBits = msb;
 113         this.leastSigBits = lsb;
 114     }
 115 
 116     /**
 117      * Constructs a new {@code UUID} using the specified data.  {@code


 122      * @param  mostSigBits
 123      *         The most significant bits of the {@code UUID}
 124      *
 125      * @param  leastSigBits
 126      *         The least significant bits of the {@code UUID}
 127      */
 128     public UUID(long mostSigBits, long leastSigBits) {
 129         this.mostSigBits = mostSigBits;
 130         this.leastSigBits = leastSigBits;
 131     }
 132 
 133     /**
 134      * Static factory to retrieve a type 4 (pseudo randomly generated) UUID.
 135      *
 136      * The {@code UUID} is generated using a cryptographically strong pseudo
 137      * random number generator.
 138      *
 139      * @return  A randomly generated {@code UUID}
 140      */
 141     public static UUID randomUUID() {
 142         SecureRandom ng = Holder.numberGenerator;



 143 
 144         byte[] randomBytes = new byte[16];
 145         ng.nextBytes(randomBytes);
 146         randomBytes[6]  &= 0x0f;  /* clear version        */
 147         randomBytes[6]  |= 0x40;  /* set to version 4     */
 148         randomBytes[8]  &= 0x3f;  /* clear variant        */
 149         randomBytes[8]  |= 0x80;  /* set to IETF variant  */
 150         return new UUID(randomBytes);
 151     }
 152 
 153     /**
 154      * Static factory to retrieve a type 3 (name based) {@code UUID} based on
 155      * the specified byte array.
 156      *
 157      * @param  name
 158      *         A byte array to be used to construct a {@code UUID}
 159      *
 160      * @return  A {@code UUID} generated from the specified array
 161      */
 162     public static UUID nameUUIDFromBytes(byte[] name) {


 237      * <li>1    Time-based UUID
 238      * <li>2    DCE security UUID
 239      * <li>3    Name-based UUID
 240      * <li>4    Randomly generated UUID
 241      * </ul>
 242      *
 243      * @return  The version number of this {@code UUID}
 244      */
 245     public int version() {
 246         // Version is bits masked by 0x000000000000F000 in MS long
 247         return (int)((mostSigBits >> 12) & 0x0f);
 248     }
 249 
 250     /**
 251      * The variant number associated with this {@code UUID}.  The variant
 252      * number describes the layout of the {@code UUID}.
 253      *
 254      * The variant number has the following meaning:
 255      * <p><ul>
 256      * <li>0    Reserved for NCS backward compatibility
 257      * <li>2    IETF RFC 4122 (Leach-Salz) (used by this class)
 258      * <li>6    Reserved, Microsoft Corporation backward compatibility
 259      * <li>7    Reserved for future definition
 260      * </ul>
 261      *
 262      * @return  The variant number of this {@code UUID}
 263      */
 264     public int variant() {
 265         // This field is composed of a varying number of bits.
 266         // 0    -    -    Reserved for NCS backward compatibility
 267         // 1    0    -    The Leach-Salz aka IETF variant (used by this class)
 268         // 1    1    0    Reserved, Microsoft backward compatibility
 269         // 1    1    1    Reserved for future definition.
 270         return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62)))
 271                       & (leastSigBits >> 63));
 272     }
 273 
 274     /**
 275      * The timestamp value associated with this UUID.
 276      *
 277      * <p> The 60 bit timestamp value is constructed from the time_low,
 278      * time_mid, and time_hi fields of this {@code UUID}.  The resulting
 279      * timestamp is measured in 100-nanosecond units since midnight,
 280      * October 15, 1582 UTC.
 281      *
 282      * <p> The timestamp value is only meaningful in a time-based UUID, which
 283      * has version type 1.  If this {@code UUID} is not a time-based UUID then
 284      * this method throws UnsupportedOperationException.
 285      *
 286      * @throws UnsupportedOperationException
 287      *         If this UUID is not a version 1 UUID