• UUID生成工具


    public class UUIDUtils {
    private static SecureRandom SEEDER_STATIC = null;
    private static byte[] ADDRESS = null;
    private static String MID_VALUE_STATIC = null;
    private String midValue = null;
    private SecureRandom seeder = null;

    static {
    try {
    ADDRESS = InetAddress.getLocalHost().getAddress();
    StringBuffer buffer = new StringBuffer(8);
    buffer.append(toHex(toInt(ADDRESS), 8));
    MID_VALUE_STATIC = buffer.toString();
    SEEDER_STATIC = new SecureRandom();
    SEEDER_STATIC.nextInt();
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }

    public UUIDUtils() {
    StringBuffer buffer = new StringBuffer(16);
    buffer.append(MID_VALUE_STATIC);
    buffer.append(toHex(System.identityHashCode(this), 8));
    midValue = buffer.toString();
    seeder = new SecureRandom();
    seeder.nextInt();
    }

    /**
    * 该方法用来产生一个32位的唯一的标记String
    *
    * @param obj
    * 传入一个参考的对象
    * @return
    */
    public static String generate(Object obj) {
    StringBuffer uid = new StringBuffer(32);

    // get the system time
    long currentTimeMillis = System.currentTimeMillis();
    uid.append(toHex((int) (currentTimeMillis & -1L), 8));

    // get the internet address
    uid.append(MID_VALUE_STATIC);

    // get the object hash value
    uid.append(toHex(System.identityHashCode(obj), 8));

    // get the random number
    uid.append(toHex(getRandom(), 8));

    return uid.toString();
    }

    /**
    * 该方法用来产生一个32位的String唯一标记
    */
    public String generate() {
    StringBuffer uid = new StringBuffer(32);

    // get the system time
    long currentTimeMillis = System.currentTimeMillis();
    uid.append(toHex((int) (currentTimeMillis & -1L), 8));

    // get the internet address
    uid.append(midValue);

    // get the random number
    uid.append(toHex(seeder.nextInt(), 8));

    return uid.toString();
    }

    private static String toHex(int value, int length) {
    char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    StringBuffer buffer = new StringBuffer(length);
    int shift = length - 1 << 2;
    for (int i = -1; ++i < length;) {
    buffer.append(hexDigits[value >> shift & 0xf]);
    value <<= 4;
    }

    return buffer.toString();
    }

    private static int toInt(byte[] bytes) {
    int value = 0;
    for (int i = -1; ++i < bytes.length;) {
    value <<= 8;
    value |= bytes[i];
    }

    return value;
    }

    private static synchronized int getRandom() {
    return SEEDER_STATIC.nextInt();
    }

  • 相关阅读:
    請問各位大大,我要將listview顯示的縮圖加入到listview2,請問該如何做呢
    一个可设置窗口透明属性的控件,可让窗口透明、半透明
    laravel he stream or file "..laravel-2019-02-14.log" could not be opened: failed to open stream: Permission denied
    每日学习-20190721
    linux centos无法删除网站根目录下的.user.ini解决办法
    laravel在使用Composer安装插件时要求输入授权用户名密码解决办法
    Centos7 日志查看工具
    Centos7 Putty SSH密钥登录
    阿里云Centos7用putty ssh链接掉线
    阿里云 centos 无法执行moodle cron
  • 原文地址:https://www.cnblogs.com/guangxiang/p/10621509.html
Copyright © 2020-2023  润新知