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

📄 uuid.java

📁 简单的web应用,实现基本的购物车功能,有良好的扩展性
💻 JAVA
字号:
package util.uid;

import java.util.Random;

public class UUID extends UIDFactory
{

    protected UUID(long highTag, long loTag)
    {
        m_uuid = null;
        m_hiTag = highTag;
        m_loTag = loTag;
        m_uuid = toString(toByteArray());
    }

    protected UUID()
    {
        m_uuid = null;
        next();
        m_uuid = toString(toByteArray());
    }

    private static final byte hiNibble(byte b)
    {
        return (byte)(b >> 4 & 0xf);
    }

    private static final byte loNibble(byte b)
    {
        return (byte)(b & 0xf);
    }

    private static final byte setHiNibble(byte dest, int b)
    {
        dest &= 0xf;
        dest |= (byte)b << 4;
        return dest;
    }

    private static final byte setLoNibble(byte dest, int b)
    {
        dest &= 0xf0;
        dest |= (byte)b & 0xf;
        return dest;
    }

    public boolean equals(Object obj)
    {

//        boolean flag;
//        UUID uuid = (UUID)obj;
//        flag = uuid.m_hiTag == m_hiTag && uuid.m_loTag == m_loTag;
//        return flag;
//        ClassCastException cce;
//        cce;
//          return ()
            if(!(obj instanceof UUID))
                return false;
        return false;
    }

    public String getNextUID()
    {
        next();
        return m_uuid;
    }

    public String getUID()
    {
        return m_uuid;
    }

    public void setUID(String uidStr)
        throws Exception
    {
        long loTag = 0L;
        long hiTag = 0L;
        int len = uidStr.length();
        if(32 != len)
            throw new Exception("bad string format");
        int i = 0;
        int idx = 0;
        for(; i < 2; i++)
        {
            loTag = 0L;
            for(int j = 0; j < len / 2; j++)
            {
                String s = uidStr.substring(idx++, idx);
                int val = Integer.parseInt(s, 16);
                loTag <<= 4;
                loTag |= val;
            }

            if(i == 0)
                hiTag = loTag;
        }

        m_hiTag = hiTag;
        m_loTag = loTag;
        m_uuid = toString(toByteArray());
    }

    public String toPrintableString()
    {
        byte bytes[] = toByteArray();
        if(16 != bytes.length)
            return "** Bad UUID Format/Value **";
        StringBuffer buf = new StringBuffer();
        int i;
        for(i = 0; i < 4; i++)
        {
            buf.append(Integer.toHexString(hiNibble(bytes[i])));
            buf.append(Integer.toHexString(loNibble(bytes[i])));
        }

        while(i < 10)
        {
            buf.append('-');
            for(int j = 0; j < 2; j++)
            {
                buf.append(Integer.toHexString(hiNibble(bytes[i])));
                buf.append(Integer.toHexString(loNibble(bytes[i++])));
            }

        }
        buf.append('-');
        for(; i < 16; i++)
        {
            buf.append(Integer.toHexString(hiNibble(bytes[i])));
            buf.append(Integer.toHexString(loNibble(bytes[i])));
        }

        return buf.toString();
    }

    public String toString()
    {
        return m_uuid;
    }

    protected static UIDFactory getInstance()
    {
        return new UUID();
    }

    protected static String toString(byte bytes[])
    {
        if(16 != bytes.length)
            return "** Bad UUID Format/Value **";
        StringBuffer buf = new StringBuffer();
        for(int i = 0; i < 16; i++)
        {
            buf.append(Integer.toHexString(hiNibble(bytes[i])));
            buf.append(Integer.toHexString(loNibble(bytes[i])));
        }

        return buf.toString();
    }

    protected void next()
    {
        m_hiTag = System.currentTimeMillis() + UIDFactory.JVMHASH * 0x100000000L ^ UIDFactory.MACHINEID;
        m_loTag = UIDFactory.EPOCH + Math.abs(UIDFactory.m_random.nextLong());
        m_uuid = toString(toByteArray());
    }

    protected byte[] toByteArray()
    {
        byte bytes[] = new byte[16];
        int idx = 15;
        long val = m_loTag;
        for(int i = 0; i < 8; i++)
        {
            bytes[idx--] = (byte)(int)(val & 255L);
            val >>= 8;
        }

        val = m_hiTag;
        for(int i = 0; i < 8; i++)
        {
            bytes[idx--] = (byte)(int)(val & 255L);
            val >>= 8;
        }

        if(!isMD5())
            return bytes;
        else
            return UIDFactory.toMD5(bytes);
    }

    protected static final int BITS8 = 8;
    protected static final int BYTELEN = 16;
    protected static final int HIMASK = 240;
    protected static final int LO8BITMASK = 255;
    protected static final int LOMASK = 15;
    protected static final long MAX_INT = 32767L;
    protected static final long MAX_LONG = 0x7fffffffL;
    protected long m_hiTag;
    protected long m_loTag;
    protected String m_uuid;
}

⌨️ 快捷键说明

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