View Javadoc
1 /* 2 * 3 * Copyright 2001 Sun Microsystems(TM), Inc. All Rights Reserved. 4 * 5 * The contents of this file are made available under and subject to the 6 * Research Use Rights of the Sun(TM) Community Source License v 3.0 (the 7 * "License"). Software distributed under the License is distributed on an "AS 8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the 9 * License for the specific language governing rights and limitations under 10 * the License. 11 * 12 * Contributor(s): Sun Microsystems, Inc. 13 * 14 * $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/util/UUID.java,v 1.2 2002/11/09 04:59:23 jasilva Exp $ 15 * 16 */ 17 18 package com.sun.ebxml.registry.util; 19 20 import java.io.Serializable; 21 import java.io.DataInput; 22 import java.io.DataOutput; 23 import java.io.IOException; 24 25 /*** 26 * A universally unique identifier (UUID). 27 * A UUID is a 128-bit value. <p> 28 * 29 * The most significant long can be decomposed into the following 30 * unsigned fields: 31 * <pre> 32 * 0xFFFFFFFF00000000 time_low 33 * 0x00000000FFFF0000 time_mid 34 * 0x000000000000F000 version 35 * 0x0000000000000FFF time_hi 36 * </pre> 37 * The least significant long can be decomposed into the following 38 * unsigned fields: 39 * <pre> 40 * 0xC000000000000000 variant 41 * 0x3FFF000000000000 clock_seq 42 * 0x0000FFFFFFFFFFFF node 43 * </pre> 44 * The variant field must be 0x2. The version field must be either 0x1 or 0x4. 45 * If the version field is 0x4, then the most significant bit of the node 46 * field must be set to 1, and the remaining fields are set to values 47 * produced by a cryptographically strong pseudo-random number generator. 48 * If the version field is 0x1, then the node field is set to an IEEE 802 49 * address, the clock_seq field is set to a 14-bit random number, and the 50 * time_low, time_mid, and time_hi fields are set to the least, middle and 51 * most significant bits (respectively) of a 60-bit timestamp measured in 52 * 100-nanosecond units since midnight, October 15, 1582 UTC. 53 */ 54 public final class UUID implements Serializable { 55 56 /* 57 private static final long serialVersionUID = -7803375959559762239L; 58 */ 59 60 /*** 61 * The most significant 64 bits. 62 * 63 * @serial 64 */ 65 private long mostSig; 66 /*** 67 * The least significant 64 bits. 68 * 69 * @serial 70 */ 71 private long leastSig; 72 73 /*** 74 * Simple constructor. 75 * 76 * @param mostSig the most significant 64 bits 77 * @param leastSig the lease significant 64 bits 78 */ 79 public UUID(long mostSig, long leastSig) { 80 this.mostSig = mostSig; 81 this.leastSig = leastSig; 82 } 83 84 /*** 85 * Reads in 16 bytes in standard network byte order. 86 * 87 * @param in the input stream to read 16 bytes from 88 */ 89 public UUID(DataInput in) throws IOException { 90 this.mostSig = in.readLong(); 91 this.leastSig = in.readLong(); 92 } 93 94 /*** Returns the most significant 64 bits of the service ID. */ 95 public long getMostSignificantBits() { 96 return mostSig; 97 } 98 99 /*** Returns the least significant 64 bits of the service ID. */ 100 public long getLeastSignificantBits() { 101 return leastSig; 102 } 103 104 /*** 105 * Writes out 16 bytes in standard network byte order. 106 * 107 * @param out the output stream to write 16 bytes to 108 */ 109 public void writeBytes(DataOutput out) throws IOException { 110 out.writeLong(mostSig); 111 out.writeLong(leastSig); 112 } 113 114 public int hashCode() { 115 return (int)((mostSig >> 32) ^ mostSig ^ (leastSig >> 32) ^ leastSig); 116 } 117 118 /*** 119 * UU IDs are equal if they represent the same 128-bit value. 120 */ 121 public boolean equals(Object obj) { 122 if (!(obj instanceof UUID)) 123 return false; 124 UUID sid = (UUID)obj; 125 return (mostSig == sid.mostSig && leastSig == sid.leastSig); 126 } 127 128 /*** 129 * Returns a 36-character string of six fields separated by hyphens, 130 * with each field represented in lowercase hexadecimal with the same 131 * number of digits as in the field. The order of fields is: time_low, 132 * time_mid, version and time_hi treated as a single field, variant and 133 * clock_seq treated as a single field, and node. 134 */ 135 public String toString() { 136 return (digits(mostSig >> 32, 8) + "-" + 137 digits(mostSig >> 16, 4) + "-" + 138 digits(mostSig, 4) + "-" + 139 digits(leastSig >> 48, 4) + "-" + 140 digits(leastSig, 12)); 141 } 142 143 /*** Returns val represented by the specified number of hex digits. */ 144 private static String digits(long val, int digits) { 145 long hi = 1L << (digits * 4); 146 return Long.toHexString(hi | (val & (hi - 1))).substring(1); 147 } 148 }

This page was automatically generated by Maven