一、前言
关于jsr082.jar源码包中javax.bluetooth.UUID定义全局序列生成器,详情参见源码说明。
二、源码说明
package javax.bluetooth;@b@@b@public class UUID@b@{@b@ long highBits;@b@ long lowBits;@b@ private static final long BASE_UUID_HIGHT = 4096L;@b@ private static final long BASE_UUID_LOW = -9223371485494954757L;@b@ private static final char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };@b@@b@ public UUID(long uuidValue)@b@ {@b@ if ((uuidValue < 0L) || (uuidValue > 4294967295L)) {@b@ throw new IllegalArgumentException("The 'uuidValue' is out of [0, 2^32 - 1] range: " + uuidValue);@b@ }@b@@b@ this.highBits = (uuidValue << 32 | 0x1000);@b@ this.lowBits = -9223371485494954757L;@b@ }@b@@b@ public UUID(String uuidValue, boolean shortUUID)@b@ {@b@ if (uuidValue == null) {@b@ throw new NullPointerException("Specified 'uuidValue' is null");@b@ }@b@@b@ if ((uuidValue.length() == 0) || ((shortUUID) && (uuidValue.length() > 8)) || (uuidValue.length() > 32))@b@ {@b@ throw new IllegalArgumentException("Invalid length of specified 'uuidValue': " + uuidValue.length());@b@ }@b@@b@ if (uuidValue.indexOf(45) != -1) {@b@ throw new NumberFormatException("The '-' character is not allowed: " + uuidValue);@b@ }@b@@b@ if (shortUUID)@b@ {@b@ long val = Long.parseLong(uuidValue, 16);@b@@b@ this.highBits = (val << 32 | 0x1000);@b@ this.lowBits = -9223371485494954757L;@b@ return;@b@ }@b@@b@ this.highBits = 0L;@b@@b@ if (uuidValue.length() < 16) {@b@ this.lowBits = Long.parseLong(uuidValue, 16);@b@ return;@b@ }@b@@b@ int l = uuidValue.length();@b@ this.lowBits = Long.parseLong(uuidValue.substring(l - 8), 16);@b@ this.lowBits |= Long.parseLong(uuidValue.substring(l - 16, l - 8), 16) << 32;@b@@b@ if (l == 16) {@b@ return;@b@ }@b@@b@ if (l <= 24) {@b@ this.highBits = Long.parseLong(uuidValue.substring(0, l - 16), 16);@b@ } else {@b@ this.highBits = Long.parseLong(uuidValue.substring(l - 24, l - 16), 16);@b@ this.highBits |= Long.parseLong(uuidValue.substring(0, l - 24), 16) << 32;@b@ }@b@ }@b@@b@ public String toString()@b@ {@b@ int[] ints = { (int)(this.lowBits & 0xFFFFFFFF), (int)(this.lowBits >>> 32 & 0xFFFFFFFF), (int)(this.highBits & 0xFFFFFFFF), (int)(this.highBits >>> 32 & 0xFFFFFFFF) };@b@@b@ int charPos = 32;@b@ char[] buf = new char[charPos];@b@ int shift = 4;@b@ int radix = 16;@b@ int mask = 15;@b@ int needZerosIndex = -1;@b@@b@ for (int i = 3; i >= 0; --i) {@b@ if (ints[i] != 0) {@b@ needZerosIndex = i - 1;@b@ break;@b@ }@b@@b@ }@b@@b@ for (i = 0; i < ints.length; ++i)@b@ {@b@ int j;@b@ if ((ints[i] == 0) && (needZerosIndex < i) && (i != 0)) {@b@ break label226:@b@ }@b@@b@ if (needZerosIndex >= i)@b@ {@b@ for (j = 0; j < 8; ++j) {@b@ buf[(--charPos)] = digits[(ints[i] & mask)];@b@ ints[i] >>>= shift;@b@ }@b@ }@b@ else@b@ do@b@ {@b@ buf[(--charPos)] = digits[(ints[i] & mask)];@b@ ints[i] >>>= shift; }@b@ while (ints[i] != 0);@b@ }@b@@b@ label226: return new String(buf, charPos, 32 - charPos);@b@ }@b@@b@ public boolean equals(Object value)@b@ {@b@ return ((value instanceof UUID) && (this.lowBits == ((UUID)value).lowBits) && (this.highBits == ((UUID)value).highBits));@b@ }@b@@b@ public int hashCode()@b@ {@b@ return (int)(this.highBits ^ this.highBits >> 32 ^ this.lowBits ^ this.lowBits >> 32);@b@ }@b@}