首页 > 代码库 > 生成主键的工具类

生成主键的工具类

  1 public class UNIDGenerate {
  2 
  3     private static final int IP;
  4     
  5     static {
  6         int ipadd;
  7         try {
  8             ipadd = toInt(InetAddress.getLocalHost().getAddress());
  9         } catch (Exception e) {
 10             ipadd = 0;
 11         }
 12         IP = ipadd;
 13     }
 14 
 15     private static short counter = (short) 0;
 16 
 17     private static final int JVM = (int) (System.currentTimeMillis() >>> 8);
 18 
 19     private String sep = "";
 20 
 21     /**
 22      * 从java 的虚拟机计算出一个值 Unique across JVMs on this machine (unless they load
 23      * this class in the same quater second - very unlikely)
 24      */
 25     protected int getJVM() {
 26         return JVM;
 27     }
 28 
 29     /**
 30      * 在一个毫秒单位里的唯一整数值 Unique in a millisecond for this JVM instance (unless
 31      * there are > Short.MAX_VALUE instances created in a millisecond)
 32      */
 33     protected short getCount() {
 34         synchronized (UNIDGenerate.class) {
 35             if (counter < 0)
 36                 counter = 0;
 37             return counter++;
 38         }
 39     }
 40 
 41     /**
 42      * 局域网唯一IP地址 Unique in a local network
 43      */
 44     protected int getIP() {
 45         return IP;
 46     }
 47 
 48     /**
 49      * Unique down to millisecond
 50      */
 51     protected short getHiTime() {
 52         return (short) (System.currentTimeMillis() >>> 32);
 53     }
 54 
 55     protected int getLoTime() {
 56         return (int) System.currentTimeMillis();
 57     }
 58 
 59     protected String format(int intval) {
 60         String formatted = Integer.toHexString(intval);
 61         StringBuffer buf = new StringBuffer("00000000");
 62         buf.replace(8 - formatted.length(), 8, formatted);
 63         return buf.toString();
 64     }
 65 
 66     protected String format(short shortval) {
 67         String formatted = Integer.toHexString(shortval);
 68         StringBuffer buf = new StringBuffer("0000");
 69         buf.replace(4 - formatted.length(), 4, formatted);
 70         return buf.toString();
 71     }
 72 
 73     /**
 74      * 返回生成的unid
 75      *
 76      * @return String-unid值
 77      */
 78     public String toString() {
 79         StringBuffer sb = new StringBuffer(36).append(format(getIP())).append(
 80                 sep).append(format(getJVM())).append(sep).append(
 81                 format(getHiTime())).append(sep).append(format(getLoTime()))
 82                 .append(sep).append(format(getCount()));
 83         MessageDigest md5 = null;
 84         try {
 85             md5 = MessageDigest.getInstance("MD5");
 86         } catch (NoSuchAlgorithmException e) {
 87             return sb.toString().toUpperCase();
 88         }
 89         md5.update(sb.toString().getBytes());
 90         byte[] array = md5.digest();
 91         StringBuffer ret = new StringBuffer();
 92         for (int j = 0; j < array.length; ++j) {
 93             int b = array[j] & 0xFF;
 94             if (b < 0x10)
 95                 ret.append(‘0‘);
 96             ret.append(Integer.toHexString(b));
 97         }
 98         return ret.toString().toUpperCase();
 99     }
100 
101     /**
102      * 返回生成的unid
103      *
104      * @return String-unid值
105      */
106     public String getUnid() {
107         return toString();
108     }
109 
110     /**
111      * 判断输入的字符串是否为32位Unid
112      *
113      * @param str
114      *            输入字符串
115      * @return 若是32位Unid,则返回True,否则返回False
116      */
117     public static boolean isUnid(String str) {
118         if (str.length() == 32 && str.matches("[A-Z0-9]{32}")) {
119             return true;
120         }
121         return false;
122     }
123 
124     public static int toInt(byte[] bytes) {
125         int result = 0;
126         for (int i = 0; i < 4; i++) {
127             result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
128         }
129         return result;
130     }
131 }

生成主键的工具类