📄 uuid.java
字号:
/*
* UUID.java
*
* Created 07.02.2003
*
* eaio: UUID - an implementation of the UUID specification
* Copyright (c) 2003, 2004 Johann Burkard (<mailto: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 com.eaio.uuid;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.omg.CORBA.portable.IDLEntity;
import com.eaio.util.lang.Hex;
/**
* Creates UUIDs according to the DCE Universal Token Identifier specification.
* <p>
* UUIDs of this class are suitable as primary keys for EJB Entity Beans. You
* will need to add the two following methods to your CMP Entity Beans:
* <pre>
* public abstract long getTime();
* public abstract void setTime(long time);
*
* public abstract long getClockSeqAndNode();
* public abstract void setClockSeqAndNode(long clockSeqAndNode);
* </pre>
* <p>
* Don't forget to set com.eaio.uuid.UUID as the primary key in your
* descriptor and to initialize the primary key in your bean by calling
* <pre>
* public UUID ejbCreate(...) throws CreateException {
* setTime(UUIDGen.newTime());
* setClockAndNode(UUIDGen.getClockSeqAndNode());
* return null;
* }
* </pre>
* <p>
* For usage with the Sun Application Server, you must comment out the
* <code>serialVersionUID</code> field and recompile the sources.
*
* @see <a href="http://www.opengroup.org/onlinepubs/9629399/apdxa.htm">
* http://www.opengroup.org/onlinepubs/9629399/apdxa.htm
* </a>
* @see <a href="http://www.uddi.org/pubs/draft-leach-uuids-guids-01.txt">
* http://www.uddi.org/pubs/draft-leach-uuids-guids-01.txt
* </a>
* @author <a href="mailto:jb@eaio.de">Johann Burkard</a>
* @version 2.1.1
*/
public class UUID implements Comparable, Serializable, Cloneable, IDLEntity {
/**
* If you are working with the Sun One Application Server and you are using
* this class as primary keys for EJBs, you must comment this field out.
* <p>
* This applies to version 7.
*/
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.
*
* @see UUIDGen#newTime()
* @see UUIDGen#getClockSeqAndNode()
*/
public UUID() {
this(UUIDGen.newTime(), UUIDGen.getClockSeqAndNode());
}
/**
* Constructor for UUID. Constructs a UUID from two <code>long</code> values.
*
* @param time the upper 64 bits
* @param clockSeqAndNode the lower 64 bits
*/
public UUID(long time, long clockSeqAndNode) {
this.time = time;
this.clockSeqAndNode = clockSeqAndNode;
}
/**
* Copy constructor for UUID. Values of the given UUID are copied.
*
* @param u the UUID, may not be <code>null</code>
*/
public UUID(UUID u) {
this(u.time, u.clockSeqAndNode);
}
/**
* Compares this UUID to another Object. Throws a {@link 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.
*
* @param o the other Object, may not be <code>null</code>
* @return a value < 0, 0 or a value > 0
* @see java.lang.Comparable#compareTo(java.lang.Object)
* @throws ClassCastException
*/
public int compareTo(Object o) {
if (this == o) {
return 0;
}
if (!(o instanceof UUID)) {
throw new ClassCastException(
"The argument must be of type '" + getClass().getName() + "'.");
}
UUID t = (UUID) o;
if (time > t.time) {
return 1;
}
if (time < t.time) {
return -1;
}
if (clockSeqAndNode > t.clockSeqAndNode) {
return 1;
}
if (clockSeqAndNode < t.clockSeqAndNode) {
return -1;
}
return 0;
}
/**
* Tweaked Serialization routine.
*
* @param out the ObjectOutputStream
* @throws IOException
*/
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeLong(time);
out.writeLong(clockSeqAndNode);
}
/**
* Tweaked Serialization routine.
*
* @param in the ObjectInputStream
* @throws IOException
* @throws ClassNotFoundException
*/
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
time = in.readLong();
clockSeqAndNode = in.readLong();
}
/**
* Returns this UUID as a String.
*
* @return a String, never <code>null</code>
* @see java.lang.Object#toString()
* @see #toStringBuffer(StringBuffer)
*/
public final String toString() {
return toStringBuffer(null).toString();
}
/**
* Appends a String representation of this to the given {@link StringBuffer} or
* creates a new one if none is given.
*
* @param in the StringBuffer to append to, may be <code>null</code>
* @return a StringBuffer
*/
public StringBuffer toStringBuffer(StringBuffer in) {
if (in == null) {
in = new StringBuffer(36);
}
else {
in.ensureCapacity(in.length() + 36);
}
in.append(Hex.asChars((int) (time >> 32))).append('-').append(
Hex.asChars((short) (time >> 16))).append('-').append(
Hex.asChars((short) time)).append('-').append(
Hex.asChars((short) (clockSeqAndNode >> 48))).append('-').append(
Hex.asChars(clockSeqAndNode, 12));
return in;
}
/**
* 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.
*
* @return an <code>int</code> representing the hash code
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return (int) ((time >> 32) ^ time ^ (clockSeqAndNode >> 32) ^ clockSeqAndNode);
}
/**
* Clones this UUID.
*
* @return a new UUID with identical values, never <code>null</code>
*/
public Object clone() {
return new UUID(this);
}
/**
* Returns the time field of the UUID (upper 64 bits).
*
* @return the time field
*/
public final long getTime() {
return time;
}
/**
* Returns the clock and node field of the UUID (lower 64 bits).
*
* @return the clockSeqAndNode field
*/
public final long getClockSeqAndNode() {
return clockSeqAndNode;
}
/**
* Compares two Objects for equality.
*
* @see java.lang.Object#equals(Object)
* @param obj the Object to compare this UUID with, may be <code>null</code>
* @return <code>true</code> if the other Object is equal to this UUID,
* <code>false</code> if not
*/
public boolean equals(Object obj) {
if (!(obj instanceof UUID)) {
return false;
}
return compareTo(obj) == 0;
}
/**
* Returns the nil UUID (a UUID whose values are both set to zero).
* <p>
* 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.
*
* @return a nil UUID, never <code>null</code>
*/
public static UUID nilUUID() {
return new UUID(0, 0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -