Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * UUID.java
   *
   * Created 07.02.2003
   *
   * eaio: UUID - an implementation of the UUID specification
   * Copyright (c) 2003-2009 Johann Burkard (jb@eaio.com) http://eaio.com.
   *
   * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included
  * in all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  * USE OR OTHER DEALINGS IN THE SOFTWARE.
  *
  */
 package base.uuid;
 
 
Creates UUIDs according to the DCE Universal Token Identifier specification.

All you need to know:

 UUID u = new UUID();
 

Author(s):
Johann Burkard
Version:
$Id: UUID.java 4688 2012-03-09 14:49:49Z johann $
See also:
http://www.opengroup.org/onlinepubs/9629399/apdxa.htm
http://www.uddi.org/pubs/draft-leach-uuids-guids-01.txt
UUID
 
 public class UUID implements Comparable<UUID>, ExternalizableCloneable,
Hasn't ever changed between versions.
 
 	static final long serialVersionUID = 7435962790062944603L;

The time field of the UUID.

Serial:
 
 	public long time;

The clock sequence and node field of the UUID.

Serial:
 
 	public long clockSeqAndNode;

Constructor for UUID. Constructs a new, unique UUID.

 
 	public UUID() {
 		this(UUIDGen.newTime(), UUIDGen.getClockSeqAndNode());
 	}

Constructor for UUID. Constructs a UUID from two long values.

Parameters:
time the upper 64 bits
clockSeqAndNode the lower 64 bits
 
 	public UUID(long timelong clockSeqAndNode) {
 		this. = time;
 		this. = clockSeqAndNode;
 	}

Copy constructor for UUID. Values of the given UUID are copied.

Parameters:
u the UUID, may not be null
	public UUID(UUID u) {
		this(u.timeu.clockSeqAndNode);
	}

Parses a textual representation of a UUID.

No validation is performed. If the java.lang.CharSequence is shorter than 36 characters, java.lang.ArrayIndexOutOfBoundsExceptions will be thrown.

Parameters:
s the java.lang.CharSequence, may not be null
	public UUID(CharSequence s) {
		this(Hex.parseLong(s.subSequence(0, 18)), Hex.parseLong(s.subSequence(19, 36)));
	}
	public java.util.UUID toJavaUUID() {
		return java.util.UUID.fromString(toString());
	}

Compares this UUID to another Object. Throws a java.lang.ClassCastException if the other Object is not an instance of the UUID class. Returns a value smaller than zero if the other UUID is "larger" than this UUID and a value larger than zero if the other UUID is "smaller" than this UUID.

Parameters:
t the other UUID, may not be null
Returns:
a value < 0, 0 or a value > 0
Throws:
java.lang.ClassCastException
See also:
java.lang.Comparable.compareTo(java.lang.Object)
	public int compareTo(UUID t) {
		if (this == t) {
			return 0;
		}
		if ( > t.time) {
			return 1;
		}
		if ( < t.time) {
			return -1;
		}
		if ( > t.clockSeqAndNode) {
			return 1;
		}
		if ( < t.clockSeqAndNode) {
			return -1;
		}
		return 0;
	}

Tweaked Serialization routine.
	public void writeExternal(ObjectOutput outthrows IOException {
	}

Tweaked Serialization routine.
	public void readExternal(ObjectInput inthrows IOException {
		 = in.readLong();
	}

Returns this UUID as a String.

	public final String toString() {
		return toAppendable(null).toString();
	}

Appends a String representation of this to the given java.lang.StringBuffer or creates a new one if none is given.

Parameters:
in the StringBuffer to append to, may be null
Returns:
a StringBuffer, never null
See also:
toAppendable(java.lang.Appendable)
		StringBuffer out = in;
		if (out == null) {
			out = new StringBuffer(36);
		}
		else {
			out.ensureCapacity(out.length() + 36);
		}
		return (StringBuffertoAppendable(out);
	}

Appends a String representation of this object to the given java.lang.Appendable object.

For reasons I'll probably never understand, Sun has decided to have a number of I/O classes implement Appendable which forced them to destroy an otherwise nice and simple interface with java.io.IOExceptions.

I decided to ignore any possible IOExceptions in this method.

Parameters:
a the Appendable object, may be null
Returns:
an Appendable object, defaults to a java.lang.StringBuilder if a is null
		Appendable out = a;
		if (out == null) {
			out = new StringBuilder(36);
		}
		try {
			Hex.append(out, (int) ( >> 32)).append('-');
			Hex.append(out, (short) ( >> 16)).append('-');
			Hex.append(out, (short).append('-');
			Hex.append(out, (short) ( >> 48)).append('-');
			Hex.append(out, 12);
		}
		catch (IOException ex) {
			// What were they thinking?
		}
		return out;
	}

Returns a hash code of this UUID. The hash code is calculated by XOR'ing the upper 32 bits of the time and clockSeqAndNode fields and the lower 32 bits of the time and clockSeqAndNode fields.

Returns:
an int representing the hash code
See also:
java.lang.Object.hashCode()
	public int hashCode() {
		return (int) (( >> 32) ^  ^ ( >> 32) ^ );
	}

Clones this UUID.

Returns:
a new UUID with identical values, never null
	public Object clone() {
		try {
			return super.clone();
		}
			// One of Sun's most epic fails.
			return null;
		}
	}

Returns the time field of the UUID (upper 64 bits).

Returns:
the time field
	public final long getTime() {
		return ;
	}

Returns the clock and node field of the UUID (lower 64 bits).

Returns:
the clockSeqAndNode field
	public final long getClockSeqAndNode() {
	}

Compares two Objects for equality.

Parameters:
obj the Object to compare this UUID with, may be null
Returns:
true if the other Object is equal to this UUID, false if not
See also:
java.lang.Object.equals(java.lang.Object)
	public boolean equals(Object obj) {
		if (!(obj instanceof UUID)) {
			return false;
		}
		return compareTo((UUIDobj) == 0;
	}

Returns the nil UUID (a UUID whose values are both set to zero).

Starting with version 2.0, this method does return a new UUID instance every time it is called. Earlier versions returned one instance. This has now been changed because this UUID has public, non-final instance fields. Returning a new instance is therefore more safe.

Returns:
a nil UUID, never null
	public static UUID nilUUID() {
		return new UUID(0, 0);
	}
New to GrepCode? Check out our FAQ X