UUID.java 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/* UUID.java -- Class that represents a UUID object.
   Copyright (C) 2006  Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package java.util;

import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * This class represents a 128-bit UUID value.
47
 *
48
 * There are several types of UUID, and while this class can be used to store
49
 * them, only the Leach-Salz (variant 2) UUID specified in RFC-4122 will
50 51 52
 * give meaningful results from the method calls.
 * See: http://tools.ietf.org/html/4122 for the details
 *
53
 * The format of a Leach-Salz (variant 2) time-based (version 1) UUID
54 55 56 57 58
 * is as follows:
 * time_low - upper 32 bits of the most significant 64 bits,
 *            this is the least-significant part of the timestamp.
 *
 * time_mid - bits 16-31 of the most significant 64 bits,
59
 *            this is the middle portion of the timestamp.
60
 *
61
 * version  - bits 8-15 of the most significant 64 bits.
62 63 64 65 66
 *
 * time_hi  - bits 0-7 of the most significant 64 bits,
 *            the most significant portion of the timestamp.
 *
 * clock_and_reserved  - bits 48-63 of the least significant 64 bits.
67
 *                       a variable number of bits hold the variant
68
 *                       (see the spec)
69
 *
70 71 72 73 74 75 76 77
 * node identifier     - bits 0-47 of the least signficant 64 bits.
 *
 * These fields are valid only for version 1, in the remaining versions,
 * only the version and variant fields are set, all others are used for data.
 *
 * @since 1.5
 * @author Sven de Marothy
 */
78 79
public final class UUID
  extends Object
80
  implements Serializable, Comparable<UUID>
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
{
  private static final long serialVersionUID = -4856846361193249489L;

  /**
   * Serialized field - most significant 64 bits.
   */
  private long mostSigBits;

  /**
   * Serialized field - least significant 64 bits.
   */
  private long leastSigBits;

  /**
   * Random-number generator.
   */
  private static transient Random r = new Random();

  /**
   * Constructs a new UUID.
   *
   * @since 1.5
   */
  public UUID(long mostSigBits, long leastSigBits)
  {
    this.mostSigBits = mostSigBits;
    this.leastSigBits = leastSigBits;
  }
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  /**
   * Returns the clock-sequence value of this UUID.
   * This field only exists in a time-based (version 1) UUID.
   *
   * @throws UnsupportedOperationException if the UUID type is not 1.
   * @returns an int containing the clock-sequence value.
   */
  public int clockSequence()
  {
    if( version() != 1 )
      throw new UnsupportedOperationException("Not a type 1 UUID");
    return (int)((leastSigBits & 0x3FFF000000000000L) >> 48);
  }

  /**
   * Compare this UUID to another.
   * The comparison is performed as between two 128-bit integers.
   *
   * @return -1 if this < val, 0 if they are equal, 1 if this > val.
   */
  public int compareTo(UUID o)
  {
    if( mostSigBits < o.mostSigBits )
      return -1;
    if( mostSigBits > o.mostSigBits )
      return 1;
    if( leastSigBits < o.leastSigBits )
      return -1;
    if( leastSigBits > o.mostSigBits )
      return 1;
    return 0;
  }

  /**
   * Compare a (UUID) object to this one
   */
  public boolean equals(Object obj)
  {
    if( !(obj instanceof UUID ) )
      return false;
150 151
    return ( ((UUID)obj).mostSigBits == mostSigBits &&
             ((UUID)obj).leastSigBits == leastSigBits );
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  }

  /**
   * Creates a UUID object from a Sting representation.
   *
   * For the format of the string,
   * @see #toString()
   *
   * @return a new UUID object.
   */
  public static UUID fromString(String name)
  {
    StringTokenizer st = new StringTokenizer( name.trim(), "-" );
    if( st.countTokens() < 5 )
      throw new IllegalArgumentException( "Incorrect UUID string"+
167
                                          " representation:"+name );
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

    long msb = (Long.parseLong(st.nextToken(), 16) << 32); // time low
    msb |= (Long.parseLong(st.nextToken(), 16) << 16); // time mid
    msb |= Long.parseLong(st.nextToken(), 16); // time high

    long lsb = (Long.parseLong(st.nextToken(), 16) << 48); // clock
    lsb |= Long.parseLong(st.nextToken(), 16); // node

    return new UUID(msb, lsb);
  }

  /**
   * Returns a String representation of the UUID.
   *
   * The format of the standard string representation (given in RFC4122) is:
   *
   * time-low "-" time-mid "-"
   * time-high-and-version "-"
   * clock-seq-and-reserved
   * clock-seq-low "-" node
   *
   * Where each field is represented as a hex string.
   *
   * @return the String representation.
   */
  public String toString()
  {
    return // time-low first
      padHex( (( mostSigBits & 0xFFFFFFFF00000000L) >> 32) & 0xFFFFFFFFL, 8)
      + "-" + // then time-mid
198
      padHex( (( mostSigBits & 0xFFFF0000L ) >> 16), 4 )
199
      + "-" + // time-high
200
      padHex( ( mostSigBits & 0x0000000000000000FFFFL ), 4 )
201
      + "-" + // clock (note - no reason to separate high and low here)
202
      padHex( (((leastSigBits & 0xFFFF000000000000L) >> 48) & 0xFFFF), 4 )
203
      + "-" + // finally the node value.
204
      padHex(leastSigBits & 0xFFFFFFFFFFFFL, 12);
205 206 207 208
  }

  /**
   * Returns the least significant 64 bits of the UUID as a <code>long</code>.
209
   */
210 211 212 213 214 215 216
  public long getLeastSignificantBits()
  {
    return leastSigBits;
  }

  /**
   * Returns the most significant 64 bits of the UUID as a <code>long</code>.
217
   */
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  public long getMostSignificantBits()
  {
    return mostSigBits;
  }

  /**
   * Returns a hash of this UUID.
   */
  public int hashCode()
  {
    int l1 = (int)(leastSigBits & 0xFFFFFFFFL);
    int l2 = (int)((leastSigBits & 0xFFFFFFFF00000000L) >> 32);
    int m1 = (int)(mostSigBits & 0xFFFFFFFFL);
    int m2 = (int)((mostSigBits & 0xFFFFFFFF00000000L) >> 32);

    return (l1 ^ l2) ^ (m1 ^ m2);
  }

  /**
   * Creates a UUID version 3 object (name based with MD5 hashing)
   * from a series of bytes representing a name.
   */
  public static UUID nameUUIDFromBytes(byte[] name)
241
  {
242 243 244 245 246
    long msb, lsb;
    byte[] hash;

    try
      {
247 248 249 250
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        hash = md5.digest( name );
      }
    catch (NoSuchAlgorithmException e)
251
      {
252
        throw new UnsupportedOperationException("No MD5 algorithm available.");
253
      }
254

255 256 257 258 259 260 261 262 263 264
    msb = ((hash[0] & 0xFFL) << 56) | ((hash[1] & 0xFFL) << 48) |
      ((hash[2] & 0xFFL) << 40) | ((hash[3] & 0xFFL) << 32) |
      ((hash[4] & 0xFFL) << 24) | ((hash[5] & 0xFFL) << 16) |
      ((hash[6] & 0xFFL) << 8) | (hash[7] & 0xFFL);

    lsb = ((hash[8] & 0xFFL) << 56) | ((hash[9] & 0xFFL) << 48) |
      ((hash[10] & 0xFFL) << 40) | ((hash[11] & 0xFFL) << 32) |
      ((hash[12] & 0xFFL) << 24) | ((hash[13] & 0xFFL) << 16) |
      ((hash[14] & 0xFFL) << 8) | (hash[15] & 0xFFL);

265
    lsb &= 0x3FFFFFFFFFFFFFFFL;
266 267
    lsb |= 0x8000000000000000L; // set top two bits to variant 2

268 269
    msb &= 0xFFFFFFFFFFFF0FFFL;
    msb |= 0x3000; // Version 3;
270 271 272 273 274

    return new UUID(msb, lsb);
  }

  /**
275
   * Returns the 48-bit node value in a long.
276 277 278 279 280
   * This field only exists in a time-based (version 1) UUID.
   *
   * @throws UnsupportedOperationException if the UUID type is not 1.
   * @returns a long with the node value in the lower 48 bits.
   */
281
  public long node()
282 283 284 285 286 287 288
  {
    if( version() != 1 )
      throw new UnsupportedOperationException("Not a type 1 UUID");
    return (leastSigBits & 0xFFFFFFFFFFFFL);
  }

  /**
289
   * Returns the 60-bit timestamp value of the UUID in a long.
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
   * This field only exists in a time-based (version 1) UUID.
   *
   * @throws UnsupportedOperationException if the UUID type is not 1.
   * @returns a long with the timestamp value.
   */
  public long timestamp()
  {
    if( version() != 1 )
      throw new UnsupportedOperationException("Not a type 1 UUID");
    long time = (( mostSigBits & 0xFFFFFFFF00000000L) >> 32);
    time |= (( mostSigBits & 0xFFFF0000L ) << 16);
    long time_hi = ( mostSigBits & 0xFFFL );
    time |= (time_hi << 48);
    return time;
  }

  /**
   * Generate a Leach-Salz (Variant 2) randomly generated (version 4)
   * UUID.
   *
   */
  public static UUID randomUUID()
312 313
  {
    long lsb = r.nextLong();
314 315
    long msb = r.nextLong();

316
    lsb &= 0x3FFFFFFFFFFFFFFFL;
317 318
    lsb |= 0x8000000000000000L; // set top two bits to variant 2

319 320
    msb &= 0xFFFFFFFFFFFF0FFFL;
    msb |= 0x4000; // Version 4;
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

    return new UUID( msb, lsb );
  }

  /**
   * Returns a hex String from l, padded to n spaces.
   */
  private String padHex( long l, int n )
  {
    String s = Long.toHexString( l );
    while( s.length() < n )
      s = "0" + s;
    return s;
  }

  /**
   * Returns the variant of the UUID
   *
   * This may be:
   * 0 = Reserved for NCS backwards-compatibility
   * 2 = Leach-Salz (supports the other methods in this class)
   * 6 = Reserved for Microsoft backwards-compatibility
   * 7 = (reserved for future use)
   */
  public int variant()
  {
    // Get the top 3 bits (not all may be part of the variant)
    int v = (int)((leastSigBits & 0xE000000000000000L) >> 61);
    if( (v & 0x04) == 0 ) // msb of the variant is 0
      return 0;
    if( (v & 0x02) == 0 ) // variant is 0 1 (Leach-Salz)
      return 2;
353
    return v; // 6 or 7
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
  }

  /**
   * Returns the version # of the UUID.
   *
   * Valid version numbers for a variant 2 UUID are:
   * 1 = Time based UUID
   * 2 = DCE security UUID
   * 3 = Name-based UUID using MD5 hashing
   * 4 = Randomly generated UUID
   * 5 = Name-based UUID using SHA-1 hashing
   *
   * @return the version number
   */
  public int version()
  {
    return (int)((mostSigBits & 0xF000L) >> 12);
  }
}