⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uuidgenerator.java

📁 实现了Jms的服务器源码,支持多种适配器,DB,FTP,支持多种数据库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided
 * that the following conditions are met:
 *
 * 1. Redistributions of source code must retain copyright
 *    statements and notices.  Redistributions must also contain a
 *    copy of this document.
 *
 * 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 name "Exolab" must not be used to endorse or promote
 *    products derived from this Software without prior written
 *    permission of Intalio.  For written permission,
 *    please contact info@exolab.org.
 *
 * 4. Products derived from this Software may not be called "Exolab"
 *    nor may "Exolab" appear in their names without prior written
 *    permission of Intalio. Exolab is a registered
 *    trademark of Intalio.
 *
 * 5. Due credit should be given to the Exolab Project
 *    (http://www.exolab.org/).
 *
 * THIS SOFTWARE IS PROVIDED BY INTALIO AND CONTRIBUTORS
 * ``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
 * INTALIO 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.
 *
 * Copyright 1999-2004 (C) Intalio Inc. All Rights Reserved.
 *
 * $Id: UUIDGenerator.java,v 1.4 2004/01/18 04:30:08 tanderson Exp $
 */

package org.exolab.jms.util;

import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Random;


/**
 * Universally Unique Identifier (UUID) generator.
 * <p>
 * A UUID is an identifier that is unique across both space and time,
 * with respect to the space of all UUIDs. A UUID can be used for
 * objects with an extremely short lifetime, and to reliably identifying
 * very persistent objects across a network. UUIDs are 128 bit values
 * and encoded as 36 character identifiers.
 * <p>
 * This generator produces time-based UUIDs based on the varient
 * specified in a February 4, 1998 IETF draft.
 * <p>
 * Unprefixed identifiers are generated by calling {@link #create()
 * create}. A method is also provided to create prefixed identifiers.
 * Prefixed identifiers are useful for logging and associating
 * identifiers to application objects.
 * <p>
 * To assure that identifiers can be presisted, identifiers must be
 * kept within the limit of {@link #MAXIMUM_LENGTH} characters.
 * This includes both prefixed identifiers and identifiers created
 * by the application. The {@link #trim trim} method can be used to
 * trim an identifier to the maximum allowed length. If an* identifier
 * was generated with no prefix, or with the maximum supported prefix
 * legnth, the identifier is guaranteed to be short enough and this
 * method is not required.
 * <p>
 * Convenience methods are also provided for converting an identifier
 * to and from an array of bytes. The conversion methods assume that
 * the identifier is a prefixed or unprefixed encoding of the byte
 * array as pairs of hexadecimal digits.
 * <p>
 * The UUID specification prescribes the following format for
 * representing UUIDs. Four octets encode the low field of the time
 * stamp, two octects encode the middle field of the timestamp,
 * and two octets encode the high field of the timestamp with the
 * version number. Two octets encode the clock sequence number and
 * six octets encode the unique node identifier.
 * <p>
 * The timestamp is a 60 bit value holding UTC time as a count of 100
 * nanosecond intervals since October 15, 1582. UUIDs generated in
 * this manner are guaranteed not to roll over until 3400 AD.
 * <p>
 * The clock sequence is used to help avoid duplicates that could arise
 * when the clock is set backward in time or if the node ID changes.
 * Although the system clock is guaranteed to be monotonic, the system
 * clock is not guaranteed to be monotonic across system failures.
 * The UUID cannot be sure that no UUIDs were generated with timestamps
 * larger than the current timestamp.
 * <p>
 * If the clock sequence can be determined at initialization, it is
 * incremented by one, otherwise it is set to a random number.
 * The clock sequence MUST be originally (i.e. once in the lifetime
 * of a system) initialized to a random number to minimize the
 * correlation across systems. The initial value must not be correlated
 * to the node identifier.
 * <p>
 * The node identifier must be unique for each UUID generator.
 * This is accomplished using the IEEE 802 network card address.
 * For systems with multiple IEEE 802 addresses, any available address
 * can be used. For systems with no IEEE address, a 47 bit random value
 * is used and the multicast bit is set so it will never conflict with
 * addresses obtained from network cards.
 * <p>
 * The UUID state consists of the node identifier and clock sequence.
 * The state need only be read once when the generator is initialized,
 * and the clock sequence must be incremented and stored back. If the
 * UUID state cannot be read and updated, a random number is used.
 * <p>
 * The UUID generator is thread-safe.
 * <p>
 * This class originally came from Tyrex: http://tyrex.sourceforge.net
 *
 * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
 * @version $Revision: 1.4 $ $Date: 2004/01/18 04:30:08 $
 */
public final class UUIDGenerator {

    /**
     /**
     * The identifier resolution in bytes. Identifiers are 16-byte
     * long, or 128 bits. Without a prefix, an identifier can be
     * represented as 36 hexadecimal digits and hyphens.
     * (4 hyphens are used in the UUID format)
     */
    public static final int RESOLUTION_BYTES = 16;


    /**
     * The maximum length of an identifier in textual form.
     * Prefixed identifiers and application identifiers must be
     * less or equal to the maximum length to allow persistence.
     * This maximum length is 64 characters.
     */
    public static final int MAXIMUM_LENGTH = 64;


    /**
     * The maximum length of an identifier prefix. Identifiers
     * created using {@link #create(String) create(String)} with
     * a prefix that is no longer than the maximum prefix size
     * are guaranteed to be within the maximum length allowed
     * and need not be trimmed.
     */
    public static final int MAXIMUM_PREFIX = 28;


    /**
     * The variant value determines the layout of the UUID. This
     * variant is specified in the IETF February 4, 1998 draft.
     * Used for character octets.
     */
    private static final int UUID_VARIANT_OCTET = 0x08;


    /**
     * The variant value determines the layout of the UUID. This
     * variant is specified in the IETF February 4, 1998 draft.
     * Used for byte array.
     */
    private static final int UUID_VARIANT_BYTE = 0x80;


    /**
     * The version identifier for a time-based UUID. This version
     * is specified in the IETF February 4, 1998 draft. Used for
     * character octets.
     */
    private static final int UUID_VERSION_CLOCK_OCTET = 0x01;


    /**
     * The version identifier for a time-based UUID. This version
     * is specified in the IETF February 4, 1998 draft. Used for
     * byte array.
     */
    private static final int UUID_VERSION_CLOCK_BYTE = 0x10;


    /**
     * The version identifier for a name-based UUID. This version
     * is specified in the IETF February 4, 1998 draft. Used for
     * character octets.
     */
    private static final int UUID_VERSION_NAME_OCTET = 0x03;


    /**
     * The version identifier for a name-based UUID. This version
     * is specified in the IETF February 4, 1998 draft. Used for
     * byte array.
     */
    private static final int UUID_VERSION_NAME_BYTE = 0x30;


    /**
     * The version identifier for a random-based UUID. This version
     * is specified in the IETF February 4, 1998 draft. Used for
     * character octets.
     */
    private static final int UUID_VERSION_RANDOM_CLOCK = 0x04;


    /**
     * The version identifier for a random-based UUID. This version
     * is specified in the IETF February 4, 1998 draft. Used for
     * byte array.
     */
    private static final int UUID_VERSION_RANDOM_BYTE = 0x40;


    /**
     * The difference between Java clock and UUID clock. Java clock
     * is base time is January 1, 1970. UUID clock base time is
     * October 15, 1582.
     */
    private static final long JAVA_UUID_CLOCK_DIFF = 0x0b1d069b5400L;


    /**
     * Efficient mapping from 4 bit value to lower case hexadecimal digit.
     */
    private final static char[] HEX_DIGITS = new char[]{
        '0', '1', '2', '3', '4', '5', '6', '7',
        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};


    /**
     * The number of UUIDs that can be generated per clock tick.
     * UUID assumes a clock tick every 100 nanoseconds. The actual
     * clock ticks are measured in milliseconds and based on the
     * sync-every property of the clock. The product of these two
     * values is used to set this variable.
     */
    private static int _uuidsPerTick;


    /**
     * The number of UUIDs generated in this clock tick. This counter
     * is reset each time the clock is advanced a tick. When it reaches
     * the maximum number of UUIDs allowed per tick, we block until the
     * clock advances.
     */
    private static int _uuidsThisTick;


    /**
     * The last clock. Whenever the clock changes, we record the last clock
     * to identify when we get a new clock, or when we should increments
     * the UUIDs per tick counter.
     */
    private static long _lastClock;


    /**
     * The clock sequence. This is randomly initialised, and is made of
     * four hexadecimal digits.
     */
    private static char[] _clockSeqOctet;


    /**
     * The clock sequence. The clock sequence is obtained from the UUID
     * properties and incremented by one each time we boot, or is
     * generated randomaly if missing in the UUID properties. The clock
     * sequence is made of two bytes.
     */
    private static byte[] _clockSeqByte;


    /**
     * The node identifier. The node identifier is obtained from the
     * UUID properties, or is generated if missing in the UUID properties.
     * The node identifier is made of twelve hexadecimal digits.
     */
    private static char[] _nodeIdentifierOctet;


    /**
     * The node identifier. The node identifier is obtained from the
     * UUID properties, or is generated if missing in the UUID properties.
     * The node identifier is made of six bytes.
     */
    private static byte[] _nodeIdentifierByte;


    /**
     * Creates and returns a new identifier.
     *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -