📄 uuid.java
字号:
/************************************************************************
*
* $Id: UUID.java,v 1.2 2002/03/04 21:42:58 echtcherbina Exp $
*
* Copyright (c) 2001 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Sun Microsystems, Inc. for Project JXTA."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
* must not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact Project JXTA at http://www.jxta.org.
*
* 5. Products derived from this software may not be called "JXTA",
* nor may "JXTA" appear in their name, without prior written
* permission of Sun.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Project JXTA. For more
* information on Project JXTA, please see
* <http://www.jxta.org/>.
*
* This license is based on the BSD license adopted by the Apache Foundation.
*********************************************************************************/
package net.jxta.impl.id.UUID;
import java.io.Serializable;
/**
* A <code>UUID</code> is a 128-bit universally unique identifier.
* The most significant long can be decomposed into the following
* unsigned fields:
* <pre>
* 0xFFFFFFFF00000000 time_low
* 0x00000000FFFF0000 time_mid
* 0x000000000000F000 version
* 0x0000000000000FFF time_hi
* </pre>
* The least significant long can be decomposed into the following
* unsigned fields:
* <pre>
* 0xC000000000000000 variant
* 0x3FFF000000000000 clock_seq
* 0x0000FFFFFFFFFFFF node
* </pre>
*
* The variant field must be 0x2. The version field must be either 0x1 or 0x4.
* If the version field is 0x4, then the most significant bit of the node
* field must be set to 1, and the remaining fields are set to values
* produced by a cryptographically strong pseudo-random number generator.
* If the version field is 0x1, then the node field is set to an IEEE 802
* address, the clock_seq field is set to a 14-bit random number, and the
* time_low, time_mid, and time_hi fields are set to the least, middle and
* most significant bits (respectively) of a 60-bit timestamp measured in
* 100-nanosecond units since midnight, October 15, 1582 UTC.
*
* @see net.jxta.id.ID
* @see net.jxta.id.IDFactory
* @see net.jxta.impl.id.UUID.UUIDFactory
*
* @version $Revision: 1.2 $
* @since JXTA 1.0
*
*/
public final class UUID implements Serializable {
/**
* The most significant 64 bits.
*
*/
private long mostSig;
/**
* The least significant 64 bits.
*
*/
private long leastSig;
/**
* Simple constructor.
*
* @param mostSig the most significant 64 bits
* @param leastSig the lease significant 64 bits
*
* @since JXTA 1.0
*/
public UUID(long mostSig, long leastSig) {
this.mostSig = mostSig;
this.leastSig = leastSig;
}
/**
* Returns the most significant 64 bits of the UUID.
*
* @return long return most significant bits
*
* @since JXTA 1.0
*/
public long getMostSignificantBits() {
return mostSig;
}
/**
* Returns the least significant 64 bits of the UUID.
*
* @return long lesat significant bits
*
* @since JXTA 1.0
*/
public long getLeastSignificantBits() {
return leastSig;
}
/**
* Returns the hash code of the UUID
*
* @return int hashcode
*
* @since JXTA 1.0
*/
public int hashCode() {
return (int)((mostSig >> 32) ^ mostSig ^ (leastSig >> 32) ^ leastSig);
}
/**
* Compares two UUIDs for equality.
*
* @param target the CodatID to be compared against.
* @return boolean true if CodatIds are equal, false otherwise.
*
* @since JXTA 1.0
*/
public boolean equals( Object target ) {
if (this == target)
return true;
if (target instanceof UUID) {
return equals( (UUID) target );
}
else
return false;
}
/**
* UUIDs are equal if they represent the same 128-bit value.
*
* @param sid UUID seed
* @return boolean true if equals
*
* @since JXTA 1.0
*/
public boolean equals(UUID sid) {
return (mostSig == sid.mostSig && leastSig == sid.leastSig);
}
/**
* Returns a 36-character string of six fields separated by hyphens,
* with each field represented in lowercase hexadecimal with the same
* number of digits as in the field. The order of fields is: time_low,
* time_mid, version and time_hi treated as a single field, variant and
* clock_seq treated as a single field, and node.
*
* @return String return value
*
* @since JXTA 1.0
*/
public String toString() {
return (digits(mostSig >> 32, 8) + "-" +
digits(mostSig >> 16, 4) + "-" +
digits(mostSig, 4) + "-" +
digits(leastSig >> 48, 4) + "-" +
digits(leastSig, 12));
}
/**
* Returns val represented by the specified number of hex digits
*
* @param val value
* @param digits
* @return String return value
*
* @since JXTA 1.0
*/
private static String digits(long val, int digits) {
long hi = 1L << (digits * 4);
return Long.toHexString(hi | (val & (hi - 1))).substring(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -