uuidgenerator.java
来自「OpenJMS是一个开源的Java Message Service API 1.」· Java 代码 · 共 899 行 · 第 1/3 页
JAVA
899 行
* @return An identifier */ public static String create() { return String.valueOf(createTimeUUIDChars()); } /** * Creates and returns a new prefixed identifier. * <p> * This method is equivalent to <code>prefix + create()</code>. * * @param prefix The prefix to use * @return A prefixed identifier */ public static String create(String prefix) { StringBuffer buffer; if (prefix == null) { throw new IllegalArgumentException("Argument 'prefix' is null"); } buffer = new StringBuffer(MAXIMUM_LENGTH - MAXIMUM_PREFIX + prefix.length()); buffer.append(prefix); buffer.append(createTimeUUIDChars()); return buffer.toString(); } /** * Creates and returns a new identifier. * * @return An identifier */ public static byte[] createBinary() { return createTimeUUIDBytes(); } /** * Converts a prefixed identifier into a byte array. An exception * is thrown if the identifier does not match the excepted textual * encoding. * <p> * The format for the identifier is <code>prefix{nn|-}*</code>: * a prefix followed by a sequence of bytes, optionally separated * by hyphens. Each byte is encoded as a pair of hexadecimal digits. * * @param prefix The identifier prefix * @param identifier The prefixed identifier * @return The identifier as an array of bytes * @throws InvalidIDException The identifier does not begin with * the prefix, or does not consist of a sequence of hexadecimal * digit pairs, optionally separated by hyphens */ public static byte[] toBytes(String prefix, String identifier) throws InvalidIDException { int index; char digit; byte nibble; byte[] bytes; byte[] newBytes; if (identifier == null) { throw new IllegalArgumentException("Argument identifier is null"); } if (prefix == null) { throw new IllegalArgumentException("Argument prefix is null"); } if (!identifier.startsWith(prefix)) { throw new InvalidIDException( "Invalid identifier: expected prefix " + prefix + "in identifier " + identifier); } index = 0; bytes = new byte[(identifier.length() - prefix.length()) / 2]; for (int i = prefix.length(); i < identifier.length(); ++i) { digit = identifier.charAt(i); if (digit == '-') { continue; } if (digit >= '0' && digit <= '9') { nibble = (byte) ((digit - '0') << 4); } else if (digit >= 'A' && digit <= 'F') { nibble = (byte) ((digit - ('A' - 0x0A)) << 4); } else if (digit >= 'a' && digit <= 'f') { nibble = (byte) ((digit - ('a' - 0x0A)) << 4); } else { throw new InvalidIDException( "character " + String.valueOf(digit) + " encountered, expected hexadecimal digit in identifier " + identifier); } ++i; if (i == identifier.length()) { throw new InvalidIDException( "Invalid identifier: odd number of hexadecimal digits in " + "identifier " + identifier); } digit = identifier.charAt(i); if (digit >= '0' && digit <= '9') { nibble = (byte) (nibble | (digit - '0')); } else if (digit >= 'A' && digit <= 'F') { nibble = (byte) (nibble | (digit - ('A' - 0x0A))); } else if (digit >= 'a' && digit <= 'f') { nibble = (byte) (nibble | (digit - ('a' - 0x0A))); } else { throw new InvalidIDException( "character " + String.valueOf(digit) + " encountered, expected hexadecimal digit in identifier " + identifier); } bytes[index] = nibble; ++index; } if (index == bytes.length) { return bytes; } newBytes = new byte[index]; while (index-- > 0) { newBytes[index] = bytes[index]; } return newBytes; } /** * Converts an identifier into a byte array. An exception is * thrown if the identifier does not match the excepted textual * encoding. * <p> * The format for the identifier is <code>{nn|-}*</code>: * a sequence of bytes, optionally separated by hyphens. * Each byte is encoded as a pair of hexadecimal digits. * * @param identifier The identifier * @return The identifier as an array of bytes * @throws InvalidIDException The identifier does not consist * of a sequence of hexadecimal digit pairs, optionally separated * by hyphens */ public static byte[] toBytes(String identifier) throws InvalidIDException { int index; char digit; byte nibble; byte[] bytes; byte[] newBytes; if (identifier == null) { throw new IllegalArgumentException("Argument identifier is null"); } index = 0; bytes = new byte[identifier.length() / 2]; for (int i = 0; i < identifier.length(); ++i) { digit = identifier.charAt(i); if (digit == '-') continue; if (digit >= '0' && digit <= '9') nibble = (byte) ((digit - '0') << 4); else if (digit >= 'A' && digit <= 'F') nibble = (byte) ((digit - ('A' - 0x0A)) << 4); else if (digit >= 'a' && digit <= 'f') nibble = (byte) ((digit - ('a' - 0x0A)) << 4); else { throw new InvalidIDException( "character " + String.valueOf(digit) + " encountered, expected hexadecimal digit in identifier " + identifier); } ++i; if (i == identifier.length()) { throw new InvalidIDException( "Invalid identifier: odd number of hexadecimal digits in " + "identifier " + identifier); } digit = identifier.charAt(i); if (digit >= '0' && digit <= '9') nibble = (byte) (nibble | (digit - '0')); else if (digit >= 'A' && digit <= 'F') nibble = (byte) (nibble | (digit - ('A' - 0x0A))); else if (digit >= 'a' && digit <= 'f') nibble = (byte) (nibble | (digit - ('a' - 0x0A))); else { throw new InvalidIDException( "character " + String.valueOf(digit) + " encountered, expected hexadecimal digit in identifier " + identifier); } bytes[index] = nibble; ++index; } if (index == bytes.length) return bytes; newBytes = new byte[index]; while (index-- > 0) newBytes[index] = bytes[index]; return newBytes; } /** * Converts a byte array into a prefixed identifier. * <p> * The format for the identifier is <code>prefix{nn|-}*</code>: * a prefix followed by a sequence of bytes, optionally separated * by hyphens. Each byte is encoded as a pair of hexadecimal digits. * * @param prefix The identifier prefix * @param byte An array of bytes * @return A string representation of the identifier */ public static String fromBytes(String prefix, byte[] bytes) { StringBuffer buffer; if (prefix == null) throw new IllegalArgumentException("Argument prefix is null"); if (bytes == null || bytes.length == 0) throw new IllegalArgumentException("Argument bytes is null or an empty array"); buffer = new StringBuffer(prefix); for (int i = 0; i < bytes.length; ++i) { buffer.append(HEX_DIGITS[(bytes[i] & 0xF0) >> 4]); buffer.append(HEX_DIGITS[(bytes[i] & 0x0F)]); } return buffer.toString(); } /** * Converts a byte array into an identifier. * <p> * The format for the identifier is <code>{nn|-}*</code>: a sequence * of bytes, optionally separated by hyphens. Each byte is encoded as * a pair of hexadecimal digits. * * @param bytes An array of bytes * @return A string representation of the identifier */ public static String fromBytes(byte[] bytes) { StringBuffer buffer; if (bytes == null || bytes.length == 0) throw new IllegalArgumentException("Argument bytes is null or an empty array"); buffer = new StringBuffer(); for (int i = 0; i < bytes.length; ++i) { buffer.append(HEX_DIGITS[(bytes[i] & 0xF0) >> 4]); buffer.append(HEX_DIGITS[(bytes[i] & 0x0F)]); } return buffer.toString(); } /** * Truncates an identifier so that it does not extend beyond * {@link #MAXIMUM_LENGTH} characters in length. * * @param identifier An identifier * @return An identifier trimmed to {@link #MAXIMUM_LENGTH} characters */ public static String trim(String identifier) { if (identifier == null) throw new IllegalArgumentException("Argument identifier is null"); if (identifier.length() > MAXIMUM_LENGTH) return identifier.substring(0, MAXIMUM_LENGTH); return identifier; } /** * Returns a time-based UUID as a character array. The UUID * identifier is always 36 characters long. * * @return A time-based UUID */ public static char[] createTimeUUIDChars() { long clock; char[] chars; long nextClock; // Acquire lock to assure synchornized generation synchronized (UUID.class) { clock = Clock.clock(); while (true) { if (clock > _lastClock) { // Since we are using the clock interval for the UUID space, // we must make sure the next clock provides sufficient // room so UUIDs do not roll over. nextClock = _lastClock + (_uuidsThisTick / 100); if (clock <= nextClock) clock = Clock.synchronize(); if (clock > nextClock) { // Clock reading changed since last UUID generated, // reset count of UUIDs generated with this clock. _uuidsThisTick = 0; _lastClock = clock; // Adjust UUIDs per tick in case the clock sleep ticks // have changed. _uuidsPerTick = Clock.getUnsynchTicks() * 100; break; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?