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

📄 ufmobileuuid.java

📁 国内很牛的软件公司花费两年半开发的用EJB3开发的代码,采用STRUTS和EJB3,目前系统进行第二版.所以拿出来共享
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * 
 */
package com.ufmobile.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * @author msf
 * @根据 j2se uuid修改
 *
 */
public final class UFMobileUUID implements java.io.Serializable, Comparable<UFMobileUUID> {

	    /**
	     * Explicit serialVersionUID for interoperability.
	     */
	     private static final long serialVersionUID = -4856846361193249489L;

	    /*
	     * The most significant 64 bits of this UUID.
	     *
	     * @serial
	     */
	    private final long mostSigBits;

	    /*
	     * The least significant 64 bits of this UUID.
	     *
	     * @serial
	     */
	    private final long leastSigBits;

	    /*
	     * The version number associated with this UUID. Computed on demand.
	     */
	    private transient int version = -1;

	    /*
	     * The variant number associated with this UUID. Computed on demand.
	     */
	    private transient int variant = -1;

	    /*
	     * The timestamp associated with this UUID. Computed on demand.
	     */
	    private transient volatile long timestamp = -1;

	    /*
	     * The clock sequence associated with this UUID. Computed on demand.
	     */
	    private transient int sequence = -1;

	    /*
	     * The node number associated with this UUID. Computed on demand.
	     */
	    private transient long node = -1;

	    /*
	     * The hashcode of this UUID. Computed on demand.
	     */
	    private transient int hashCode = -1;

	    /*
	     * The random number generator used by this class to create random
	     * based UUIDs.
	     */
	    private static volatile SecureRandom numberGenerator = null;

	    // Constructors and Factories

	    /*
	     * Private constructor which uses a byte array to construct the new UUID.
	     */
	    private UFMobileUUID(byte[] data) {
	        long msb = 0;
	        long lsb = 0;
	        assert data.length == 16;
	        for (int i=0; i<8; i++)
	            msb = (msb << 8) | (data[i] & 0xff);
	        for (int i=8; i<16; i++)
	            lsb = (lsb << 8) | (data[i] & 0xff);
	        this.mostSigBits = msb;
	        this.leastSigBits = lsb;
	    }

	    /**
	     * Constructs a new <tt>UUID</tt> using the specified data.
	     * <tt>mostSigBits</tt> is used for the most significant 64 bits
	     * of the <tt>UUID</tt> and <tt>leastSigBits</tt> becomes the
	     * least significant 64 bits of the <tt>UUID</tt>.
	     *
	     * @param  mostSigBits
	     * @param  leastSigBits
	     */
	    public UFMobileUUID(long mostSigBits, long leastSigBits) {
	        this.mostSigBits = mostSigBits;
	        this.leastSigBits = leastSigBits;
	    }

	    /**
	     * Static factory to retrieve a type 4 (pseudo randomly generated) UUID.
	     *
	     * The <code>UUID</code> is generated using a cryptographically strong
	     * pseudo random number generator.
	     *
	     * @return  a randomly generated <tt>UUID</tt>.
	     */
	    public static UFMobileUUID randomUUID() {
	        SecureRandom ng = numberGenerator;
	        if (ng == null) {
	            numberGenerator = ng = new SecureRandom();
	        }

	        byte[] randomBytes = new byte[16];
	        ng.nextBytes(randomBytes);
	        randomBytes[6]  &= 0x0f;  /* clear version        */
	        randomBytes[6]  |= 0x40;  /* set to version 4     */
	        randomBytes[8]  &= 0x3f;  /* clear variant        */
	        randomBytes[8]  |= 0x80;  /* set to IETF variant  */
	        UFMobileUUID result = new UFMobileUUID(randomBytes);
	        return new UFMobileUUID(randomBytes);
	    }

	    /**
	     * Static factory to retrieve a type 3 (name based) <tt>UUID</tt> based on
	     * the specified byte array.
	     *
	     * @param  name a byte array to be used to construct a <tt>UUID</tt>.
	     * @return  a <tt>UUID</tt> generated from the specified array.
	     */
	    public static UFMobileUUID nameUUIDFromBytes(byte[] name) {
	        MessageDigest md;
	        try {
	            md = MessageDigest.getInstance("MD5");
	        } catch (NoSuchAlgorithmException nsae) {
	            throw new InternalError("MD5 not supported");
	        }
	        byte[] md5Bytes = md.digest(name);
	        md5Bytes[6]  &= 0x0f;  /* clear version        */
	        md5Bytes[6]  |= 0x30;  /* set to version 3     */
	        md5Bytes[8]  &= 0x3f;  /* clear variant        */
	        md5Bytes[8]  |= 0x80;  /* set to IETF variant  */
	        return new UFMobileUUID(md5Bytes);
	    }

	    /**
	     * Creates a <tt>UUID</tt> from the string standard representation as
	     * described in the {@link #toString} method.
	     *
	     * @param  name a string that specifies a <tt>UUID</tt>.
	     * @return  a <tt>UUID</tt> with the specified value.
	     * @throws IllegalArgumentException if name does not conform to the
	     *         string representation as described in {@link #toString}.
	     */
	    public static UFMobileUUID fromString(String name) {
	        String[] components = name.split("-");
	        if (components.length != 5)
	            throw new IllegalArgumentException("Invalid UUID string: "+name);
	        for (int i=0; i<5; i++)
	            components[i] = "0x"+components[i];

	        long mostSigBits = Long.decode(components[0]).longValue();
	        mostSigBits <<= 16;
	        mostSigBits |= Long.decode(components[1]).longValue();
	        mostSigBits <<= 16;
	        mostSigBits |= Long.decode(components[2]).longValue();

	        long leastSigBits = Long.decode(components[3]).longValue();
	        leastSigBits <<= 48;
	        leastSigBits |= Long.decode(components[4]).longValue();

	        return new UFMobileUUID(mostSigBits, leastSigBits);
	    }

	    // Field Accessor Methods

	    /**
	     * Returns the least significant 64 bits of this UUID's 128 bit value.
	     *
	     * @return the least significant 64 bits of this UUID's 128 bit value.
	     */
	    public long getLeastSignificantBits() {
	        return leastSigBits;
	    }

	    /**
	     * Returns the most significant 64 bits of this UUID's 128 bit value.
	     *
	     * @return the most significant 64 bits of this UUID's 128 bit value.
	     */
	    public long getMostSignificantBits() {
	        return mostSigBits;
	    }

	    /**
	     * The version number associated with this <tt>UUID</tt>. The version 
	     * number describes how this <tt>UUID</tt> was generated.
	     *
	     * The version number has the following meaning:<p>
	     * <ul>
	     * <li>1    Time-based UUID
	     * <li>2    DCE security UUID
	     * <li>3    Name-based UUID
	     * <li>4    Randomly generated UUID
	     * </ul>
	     *
	     * @return  the version number of this <tt>UUID</tt>.
	     */
	    public int version() {
	        if (version < 0) {
	            // Version is bits masked by 0x000000000000F000 in MS long
	            version = (int)((mostSigBits >> 12) & 0x0f);
	        }
	        return version;
	    }

	    /**
	     * The variant number associated with this <tt>UUID</tt>. The variant 
	     * number describes the layout of the <tt>UUID</tt>.
	     *

⌨️ 快捷键说明

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