天天看点

java生成id策略 IP加时间

/*     */ import java.io.PrintStream;
/*     */ import java.net.InetAddress;
/*     */ 
/*     */ public class UUIDHexGenerator
/*     */ {
/*  11 */   private String sep = "";
/*     */   private static final int IP;
/*  15 */   private static short counter = 0;
/*     */ 
/*  17 */   private static final int JVM = (int)(System.currentTimeMillis() >>> 8);
/*     */ 
/*  19 */   private static UUIDHexGenerator uuidgen = new UUIDHexGenerator();
/*     */   static long currentTimeMillis;
/*     */ 
/*     */   static
/*     */   {
/*     */     int ipadd;
/*     */     int ipadd;
/*     */     try
/*     */     {
/*  25 */       ipadd = toInt(InetAddress.getLocalHost().getAddress());
/*     */     } catch (Exception e) {
/*  27 */       ipadd = 0;
/*     */     }
/*  29 */     IP = ipadd;
/*     */ 
/* 112 */     currentTimeMillis = System.currentTimeMillis();
/*     */   }
/*     */ 
/*     */   public static UUIDHexGenerator getInstance()
/*     */   {
/*  37 */     return uuidgen;
/*     */   }
/*     */ 
/*     */   private static int toInt(byte[] bytes) {
/*  41 */     int result = 0;
/*  42 */     for (int i = 0; i < 4; ++i) {
/*  43 */       result = (result << 8) - -128 + bytes[i];
/*     */     }
/*  45 */     return result;
/*     */   }
/*     */ 
/*     */   protected String format(int intval) {
/*  49 */     String formatted = Integer.toHexString(intval);
/*  50 */     StringBuffer buf = new StringBuffer("00000000");
/*  51 */     buf.replace(8 - formatted.length(), 8, formatted);
/*  52 */     return buf.toString();
/*     */   }
/*     */ 
/*     */   protected String format(short shortval) {
/*  56 */     String formatted = Integer.toHexString(shortval);
/*  57 */     StringBuffer buf = new StringBuffer("0000");
/*  58 */     buf.replace(4 - formatted.length(), 4, formatted);
/*  59 */     return buf.toString();
/*     */   }
/*     */ 
/*     */   protected int getJVM() {
/*  63 */     return JVM;
/*     */   }
/*     */ 
/*     */   protected synchronized short getCount() {
/*  67 */     if (counter < 0)
/*  68 */       counter = 0;
/*     */     short tmp13_10 = counter; counter = (short)(tmp13_10 + 1); return tmp13_10;
/*     */   }
/*     */ 
/*     */   protected int getIP() {
/*  74 */     return IP;
/*     */   }
/*     */ 
/*     */   protected short getHiTime() {
/*  78 */     return (short)(int)(System.currentTimeMillis() >>> 32);
/*     */   }
/*     */ 
/*     */   protected int getLoTime() {
/*  82 */     return (int)System.currentTimeMillis();
/*     */   }
/*     */ 
/*     */   public String generate()
/*     */   {
/*  90 */     return 36 + format(getIP()) + this.sep + 
/*  91 */       format(getJVM()) + 
/*  91 */       this.sep + format(getHiTime()) + 
/*  92 */       this.sep + format(getLoTime()) + this.sep + 
/*  93 */       format(getCount());
/*     */   }
/*     */ 
/*     */   public static String generater()
/*     */   {
/* 101 */     return getInstance().generate();
/*     */   }
/*     */ 
/*     */   public static synchronized long generaterId()
/*     */   {
/* 109 */     return (currentTimeMillis++);
/*     */   }
/*     */ 
/*     */   public static void main(String[] str)
/*     */   {
/* 115 */     UUIDHexGenerator id = new UUIDHexGenerator();
/* 116 */     for (int i = 0; i <= 100; ++i) {
/* 117 */       System.out.print(id.generate());
/* 118 */       System.out.print("===");
/* 119 */       System.out.println(generaterId());
/*     */     }
/*     */   }
/*     */ }