java.util

Class UUID

Implemented Interfaces:
Comparable<T>, Serializable

public final class UUID
extends Object
implements Serializable, Comparable<T>

A class that represents an immutable universally unique identifier (UUID). A UUID represents a 128-bit value.

There exist different variants of these global identifiers. The methods of this class are for manipulating the Leach-Salz variant, although the constructors allow the creation of any variant of UUID (described below).

The layout of a variant 2 (Leach-Salz) UUID is as follows: The most significant long consists of the following unsigned fields:

 0xFFFFFFFF00000000 time_low
 0x00000000FFFF0000 time_mid
 0x000000000000F000 version
 0x0000000000000FFF time_hi
 
The least significant long consists of the following unsigned fields:
 0xC000000000000000 variant
 0x3FFF000000000000 clock_seq
 0x0000FFFFFFFFFFFF node
 

The variant field contains a value which identifies the layout of the UUID. The bit layout described above is valid only for a UUID with a variant value of 2, which indicates the Leach-Salz variant.

The version field holds a value that describes the type of this UUID. There are four different basic types of UUIDs: time-based, DCE security, name-based, and randomly generated UUIDs. These types have a version value of 1, 2, 3 and 4, respectively.

For more information including algorithms used to create UUIDs, see the Internet-Draft UUIDs and GUIDs or the standards body definition at ISO/IEC 11578:1996.

Since:
1.5
See Also:
Serialized Form

Constructor Summary

UUID(long mostSigBits, long leastSigBits)
Constructs a new UUID using the specified data.

Method Summary

int
clockSequence()
The clock sequence value associated with this UUID.
int
compareTo(UUID val)
Compares this UUID with the specified UUID.
boolean
equals(Object obj)
Compares this object to the specified object.
static UUID
fromString(String name)
Creates a UUID from the string standard representation as described in the toString() method.
long
getLeastSignificantBits()
Returns the least significant 64 bits of this UUID's 128 bit value.
long
getMostSignificantBits()
Returns the most significant 64 bits of this UUID's 128 bit value.
int
hashCode()
Returns a hash code for this UUID.
static UUID
nameUUIDFromBytes(byte[] name)
Static factory to retrieve a type 3 (name based) UUID based on the specified byte array.
long
node()
The node value associated with this UUID.
static UUID
randomUUID()
Static factory to retrieve a type 4 (pseudo randomly generated) UUID.
long
timestamp()
The timestamp value associated with this UUID.
String
toString()
Returns a String object representing this UUID.
int
variant()
The variant number associated with this UUID.
int
version()
The version number associated with this UUID.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

UUID

public UUID(long mostSigBits,
            long leastSigBits)
Constructs a new UUID using the specified data. mostSigBits is used for the most significant 64 bits of the UUID and leastSigBits becomes the least significant 64 bits of the UUID.
Parameters:
mostSigBits -
leastSigBits -
Usages and Demos :

View More Examples of UUID(long mostSigBits,long leastSigBits)
   1:     private byte[] d4;
   2:     public static final UUID nil = new UUID(0, (short) 0, (short) 0, new byte[] {  0, 0, 0, 0, 0, 0, 0, 0 } );
   3:     static Random random = new Random();
   4:         ...
   5:         }
   6:         return new UUID(d1, (short) (d2 % 65536), (short) (d3 % 65536), d4);
   7:     }
   8:     public UUID() {
   9:         d1 = 0;

View Full Code Here
   1:            UUID[] uuids = new UUID[1];
   2:         uuids[0] = new UUID("1106",true);
   3:         try{
   4:            if(btDev == null){
   5:            System.out.println("No device has been discovered, " +

View Full Code Here
   1:          UUID[] uuids = new UUID[1];
   2:         uuids[0] = new UUID("6666", true);
   3:         try{
   4:             if(btDev == null){
   5:                  System.out.println("No device has been discovered, hence not worth proceeding exiting .... ");

View Full Code Here
   1:     public void testGetMostSignificantBits() throws Exception {
   2:         assertEquals(123, new UUID(123, 456).getMostSignificantBits());
   3:     }
   4:         ...
   5:     public void testGetLeastSignificantBits() throws Exception {
   6:         assertEquals(456, new UUID(123, 456).getLeastSignificantBits());
   7:     }
   8:         ...
   9:         assertEquals("112210f4-7de9-8115-e53d-0978c0cc8c54",
  10:                 new UUID(1234567890123456789L, -1928374651209348012L).toString());
  11:     }

View Full Code Here
   1: 
   2:         searchList[1] = new UUID( "00112233445566778899AABBCCDDEEFF", false );
   3: 
   4:         for( int i=0; i<devList.length; i++ ) 
   5:         {

View Full Code Here

Method Details

clockSequence

public int clockSequence()
The clock sequence value associated with this UUID.

The 14 bit clock sequence value is constructed from the clock sequence field of this UUID. The clock sequence field is used to guarantee temporal uniqueness in a time-based UUID.

The clockSequence value is only meaningful in a time-based UUID, which has version type 1. If this UUID is not a time-based UUID then this method throws UnsupportedOperationException.

Returns:
the clock sequence of this UUID.
Throws:
UnsupportedOperationException - if this UUID is not a version 1 UUID.

compareTo

public int compareTo(UUID val)
Compares this UUID with the specified UUID.

The first of two UUIDs follows the second if the most significant field in which the UUIDs differ is greater for the first UUID.

Parameters:
val - UUID to which this UUID is to be compared.
Returns:
-1, 0 or 1 as this UUID is less than, equal to, or greater than val.

equals

public boolean equals(Object obj)
Compares this object to the specified object. The result is true if and only if the argument is not null, is a UUID object, has the same variant, and contains the same value, bit for bit, as this UUID.
Overrides:
equals in interface Object
Parameters:
obj - the object to compare with.
Returns:
true if the objects are the same; false otherwise.
Usages and Demos :

View More Examples of equals(Object obj)
   1: 
   2: import java.util.UUID;
   3: 
   4:         ...
   5: 
   6:   protected final UUID handle;
   7: 
   8:         ...
   9:   {
  10:     this(UUID.randomUUID());
  11:   }
  12:         ...
  13:     return rhs instanceof UUIDHandle &&
  14:            handle.equals(((UUIDHandle) rhs).handle);

View Full Code Here
   1: import java.util.Arrays;
   2: import java.util.UUID;
   3: 
   4:         ...
   5:     
   6:     private UUID    m_uuid;
   7:     
   8:         ...
   9:     private KongaGuid() {
  10:         m_uuid = UUID.randomUUID();
  11:     }
  12:         ...
  13:         return (obj instanceof KongaGuid) ? 
  14:                         m_uuid.equals(((KongaGuid) obj).m_uuid) : false;

View Full Code Here
   1: import java.util.Date;
   2: import java.util.UUID;
   3: 
   4:         ...
   5: 
   6:     private UUID id;
   7:     private Date date;
   8:         ...
   9: 
  10:     public Event(UUID id, Date date, String name, String text) {
  11:         this.id = id;
  12:         ...
  13:         if(id!=null) {
  14:             return id.equals(event.id);

View Full Code Here
   1: import java.util.Date;
   2: import java.util.UUID;
   3: import javax.persistence.*;
   4:         ...
   5: 
   6:     private UUID id;
   7: 
   8:         ...
   9: 
  10:     public Event(UUID id, Date date, String name, String text) {
  11:         this.id = id;
  12:         ...
  13:         }
  14:         return id.equals(event.getId());

View Full Code Here

fromString

public static UUID fromString(String name)
Creates a UUID from the string standard representation as described in the toString() method.
Parameters:
name - a string that specifies a UUID.
Returns:
a UUID with the specified value.
Throws:
IllegalArgumentException - if name does not conform to the string representation as described in toString().
Usages and Demos :

View More Examples of fromString(String name)
   1: 
   2: import java.util.UUID;
   3: 
   4:         ...
   5:     public Object convert(final Class clazz, final Object value) {
   6:         return UUID.fromString( value.toString() );
   7:     }
   8: }

View Full Code Here
   1: 
   2: import java.util.UUID;
   3: import org.embl.ebi.escience.baclava.LSIDProvider.NamespaceEnumeration;
   4:         ...
   5:         assertTrue(id.startsWith(prefix));
   6:         UUID uuid = UUID.fromString(id.split(":")[4]);
   7:         ...
   8:         assertEquals(2, uuid.variant());
   9:         assertEquals(4, uuid.version());

View Full Code Here
   1: 
   2: import java.util.UUID;
   3: 
   4:         ...
   5:   {
   6:     return ((UUID) in).toString();
   7:   }
   8:         ...
   9:     try {
  10:       return UUID.fromString(in);
  11:     }

View Full Code Here
   1: import java.util.Arrays;
   2: import java.util.UUID;
   3: 
   4:         ...
   5:     
   6:     private UUID    m_uuid;
   7:     
   8:         ...
   9:     private KongaGuid() {
  10:         m_uuid = UUID.randomUUID();
  11:     }
  12:         ...
  13:     private KongaGuid(String stringRep) {
  14:         m_uuid = UUID.fromString(stringRep);

View Full Code Here
   1: 
   2: import java.util.UUID;
   3: import javax.swing.Icon;
   4:         ...
   5: 
   6:     public static final class ID extends Property<UUID> {
   7: 
   8:         ...
   9:         public ID(final String id) throws IllegalArgumentException {
  10:             super(UUID.fromString(id));
  11:         }

View Full Code Here

getLeastSignificantBits

public long getLeastSignificantBits()
Returns the least significant 64 bits of this UUID's 128 bit value.
Returns:
the least significant 64 bits of this UUID's 128 bit value.
Usages and Demos :

View More Examples of getLeastSignificantBits()
   1: import java.util.Arrays;
   2: import java.util.UUID;
   3: 
   4:         ...
   5:     
   6:     private UUID    m_uuid;
   7:     
   8:         ...
   9:     private KongaGuid() {
  10:         m_uuid = UUID.randomUUID();
  11:     }
  12:         ...
  13:         byte[] least = new BigInteger(String.valueOf(
  14:                         m_uuid.getLeastSignificantBits())).toByteArray();

View Full Code Here
   1:         stringOut = new StringOutputStream
   2:     public String toBase64Name( UUID uuid )
   3:     {
   4:         ...
   5:         {
   6:             writeBase64( uuid.getMostSignificantBits() );
   7:         ...
   8:             writeBase64( uuid.getLeastSignificantBits() );
   9:             base64Out.flushBase64();

View Full Code Here

getMostSignificantBits

public long getMostSignificantBits()
Returns the most significant 64 bits of this UUID's 128 bit value.
Returns:
the most significant 64 bits of this UUID's 128 bit value.
Usages and Demos :

View More Examples of getMostSignificantBits()
   1: import java.util.Arrays;
   2: import java.util.UUID;
   3: 
   4:         ...
   5:     
   6:     private UUID    m_uuid;
   7:     
   8:         ...
   9:     private KongaGuid() {
  10:         m_uuid = UUID.randomUUID();
  11:     }
  12:         ...
  13:         byte[] most = new BigInteger(String.valueOf(
  14:                         m_uuid.getMostSignificantBits())).toByteArray();

View Full Code Here
   1:         stringOut = new StringOutputStream
   2:     public String toBase64Name( UUID uuid )
   3:     {
   4:         ...
   5:         {
   6:             writeBase64( uuid.getMostSignificantBits() );
   7:         ...
   8:             writeBase64( uuid.getLeastSignificantBits() );
   9:             base64Out.flushBase64();

View Full Code Here

hashCode

public int hashCode()
Returns a hash code for this UUID.
Overrides:
hashCode in interface Object
Returns:
a hash code value for this UUID.
Usages and Demos :

View More Examples of hashCode()
   1: 
   2: import java.util.UUID;
   3: 
   4:         ...
   5: 
   6:     private UUID    m_uuid;
   7:     
   8:         ...
   9:     private UiJobID() {
  10:         m_uuid = UUID.randomUUID();
  11:     }
  12:         ...
  13:     public int hashCode() {
  14:         return m_uuid.hashCode();

View Full Code Here
   1: 
   2: import java.util.UUID;
   3: 
   4:         ...
   5: 
   6:   protected final UUID handle;
   7: 
   8:         ...
   9:   {
  10:     this(UUID.randomUUID());
  11:   }
  12:         ...
  13:   {
  14:     return handle.hashCode();

View Full Code Here
   1: import java.util.Arrays;
   2: import java.util.UUID;
   3: 
   4:         ...
   5:     
   6:     private UUID    m_uuid;
   7:     
   8:         ...
   9:     private KongaGuid() {
  10:         m_uuid = UUID.randomUUID();
  11:     }
  12:         ...
  13:     public int hashCode() {
  14:         return m_uuid.hashCode();

View Full Code Here
   1: import java.util.Date;
   2: import java.util.UUID;
   3: 
   4:         ...
   5: 
   6:     private UUID id;
   7:     private Date date;
   8:         ...
   9: 
  10:     public Event(UUID id, Date date, String name, String text) {
  11:         this.id = id;
  12:         ...
  13:     public int hashCode() {
  14:         return id.hashCode();

View Full Code Here
   1: import java.util.Date;
   2: import java.util.UUID;
   3: import javax.persistence.*;
   4:         ...
   5: 
   6:     private UUID id;
   7: 
   8:         ...
   9: 
  10:     public Event(UUID id, Date date, String name, String text) {
  11:         this.id = id;
  12:         ...
  13:     public int hashCode() {
  14:         return id.hashCode();

View Full Code Here

nameUUIDFromBytes

public static UUID nameUUIDFromBytes(byte[] name)
Static factory to retrieve a type 3 (name based) UUID based on the specified byte array.
Parameters:
name - a byte array to be used to construct a UUID.
Returns:
a UUID generated from the specified array.
Usages and Demos :

View More Examples of nameUUIDFromBytes(byte[] name)
   1: 
   2: import java.util.UUID;
   3: 
   4:         ...
   5:     public void testRandomUUID() throws Exception {
   6:         UUID uuid = UUID.randomUUID();
   7:         assertEquals(2, uuid.variant());
   8:         ...
   9:     public void testNameUUIDFromBytes() throws Exception {
  10:         UUID uuid = UUID.nameUUIDFromBytes("abc".getBytes());
  11:         assertEquals(2, uuid.variant());

View Full Code Here

node

public long node()
The node value associated with this UUID.

The 48 bit node value is constructed from the node field of this UUID. This field is intended to hold the IEEE 802 address of the machine that generated this UUID to guarantee spatial uniqueness.

The node value is only meaningful in a time-based UUID, which has version type 1. If this UUID is not a time-based UUID then this method throws UnsupportedOperationException.

Returns:
the node value of this UUID.
Throws:
UnsupportedOperationException - if this UUID is not a version 1 UUID.

randomUUID

public static UUID randomUUID()
Static factory to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator.
Returns:
a randomly generated UUID.
Usages and Demos :

View More Examples of randomUUID()
   1: 
   2: import java.util.UUID;
   3: 
   4:         ...
   5:     public String getID(NamespaceEnumeration namespace) {
   6:         UUID uuid = UUID.randomUUID();
   7:         ...
   8:         return "urn:lsid:net.sf.taverna:" + namespace + ":" + uuid;                
   9:     }    

View Full Code Here
   1: 
   2: import java.util.UUID;
   3: 
   4:         ...
   5: 
   6:     private UUID    m_uuid;
   7:     
   8:         ...
   9:     private UiJobID() {
  10:         m_uuid = UUID.randomUUID();
  11:     }

View Full Code Here
   1: import java.rmi.server.ExportException;
   2: import java.util.UUID;
   3: import java.util.WeakHashMap;
   4:         ...
   5:     
   6:     UUID id = UUID.randomUUID();
   7:     
   8:     Handler(Object impl) {
   9:         this.impl = impl;

View Full Code Here
   1: import java.util.Properties;
   2: import java.util.UUID;
   3: 
   4:         ...
   5:         Image i = new Image();
   6:         i.setName(UUID.randomUUID().toString());
   7:         Image test = factory.getUpdateService().saveAndReturnObject(i);
   8:         assertFalse(test.getDetails().getPermissions().isGranted(Role.USER, Right.WRITE));
   9:     }

View Full Code Here
   1: import java.util.Map;
   2: import java.util.UUID;
   3: 
   4:         ...
   5: class UUIDGenerator implements Generator {
   6:     private static final String SCHEME = "urn:uuid";
   7:     private static final String SCHEME_COLON = SCHEME + ":";
   8:         ...
   9:     private String getUUID() {
  10:         return UUID.randomUUID().toString();
  11:     }

View Full Code Here

timestamp

public long timestamp()
The timestamp value associated with this UUID.

The 60 bit timestamp value is constructed from the time_low, time_mid, and time_hi fields of this UUID. The resulting timestamp is measured in 100-nanosecond units since midnight, October 15, 1582 UTC.

The timestamp value is only meaningful in a time-based UUID, which has version type 1. If this UUID is not a time-based UUID then this method throws UnsupportedOperationException.

Throws:
UnsupportedOperationException - if this UUID is not a version 1 UUID.

toString

public String toString()
Returns a String object representing this UUID.

The UUID string representation is as described by this BNF :

  UUID                   =  "-"  "-"
                            "-"
                            "-"
                           
  time_low               = 4*
  time_mid               = 2*
  time_high_and_version  = 2*
  variant_and_sequence   = 2*
  node                   = 6*
  hexOctet               = 
  hexDigit               =
        "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
        | "a" | "b" | "c" | "d" | "e" | "f"
        | "A" | "B" | "C" | "D" | "E" | "F"
 
Overrides:
toString in interface Object
Returns:
a string representation of this UUID.
Usages and Demos :

View More Examples of toString()
   1: 
   2: import java.util.UUID;
   3: 
   4:         ...
   5: 
   6:     private UUID    m_uuid;
   7:     
   8:         ...
   9:     private UiJobID() {
  10:         m_uuid = UUID.randomUUID();
  11:     }
  12:         ...
  13:     public String toString() {
  14:         return m_uuid.toString();

View Full Code Here
   1: 
   2: import java.util.UUID;
   3: 
   4:         ...
   5: 
   6:   protected final UUID handle;
   7: 
   8:         ...
   9:   {
  10:     this(UUID.randomUUID());
  11:   }
  12:         ...
  13:   {
  14:     return handle.toString();

View Full Code Here
   1: import java.util.Arrays;
   2: import java.util.UUID;
   3: 
   4:         ...
   5:     
   6:     private UUID    m_uuid;
   7:     
   8:         ...
   9:     private KongaGuid() {
  10:         m_uuid = UUID.randomUUID();
  11:     }
  12:         ...
  13:     public String toString() {
  14:         return m_uuid.toString();

View Full Code Here
   1: import java.util.Date;
   2: import java.util.UUID;
   3: import javax.persistence.*;
   4:         ...
   5: 
   6:     private UUID id;
   7: 
   8:         ...
   9: 
  10:     public Event(UUID id, Date date, String name, String text) {
  11:         this.id = id;
  12:         ...
  13:     public String getKey() {
  14:         return id.toString();

View Full Code Here

variant

public int variant()
The variant number associated with this UUID. The variant number describes the layout of the UUID. The variant number has the following meaning:

  • 0 Reserved for NCS backward compatibility
  • 2 The Leach-Salz variant (used by this class)
  • 6 Reserved, Microsoft Corporation backward compatibility
  • 7 Reserved for future definition
Returns:
the variant number of this UUID.

version

public int version()
The version number associated with this UUID. The version number describes how this UUID was generated. The version number has the following meaning:

  • 1 Time-based UUID
  • 2 DCE security UUID
  • 3 Name-based UUID
  • 4 Randomly generated UUID
Returns:
the version number of this UUID.