随机ID号码

--生成随机ID号
 1 package com.g_j_x.myjava;
 2 
 3 /**
 4  * 单例模式, 格式化调用
 5  * @author g_j_x
 6  * @date 2017-4-27
 7  */
 8 public class Global
 9 {
10     private static Global instance;
11     private Global(){}
12     public static Global getInstance()
13     {
14         if (instance == null) {
15             instance = new Global();
16         }
17         return instance;
18     }
19     
20     public String UUID()
21     {
22         return UUID.randomUUID().toString().replace("-","");
23     }
24 }
点击+展开
  1 package com.g_j_x.myjava;
  2 
  3 import java.io.IOException;
  4 import java.io.ObjectInputStream;
  5 import java.io.Serializable;
  6 import java.security.MessageDigest;
  7 import java.security.NoSuchAlgorithmException;
  8 import java.security.SecureRandom;
  9 
 10 /**
 11  * 生成随机ID
 12  * @author g_j_x
 13  * @date 2017-4-27
 14  */
 15 public final class UUID
 16   implements Serializable, Comparable<UUID>
 17 {
 18   private static final long serialVersionUID = -4856846361193249489L;
 19   private final long mostSigBits;
 20   private final long leastSigBits;
 21   private transient int version = -1;
 22   private transient int variant = -1;
 23   private volatile transient long timestamp = -1L;
 24   private transient int sequence = -1;
 25   private transient long node = -1L;
 26   private transient int hashCode = -1;
 27   
 28   private UUID(byte[] paramArrayOfByte)
 29   {
 30     long l1 = 0L;
 31     long l2 = 0L;
 32     /**
 33      * 在实现中,assertion就是在程序中的一条语句,它对一个boolean表达式进行检查,一个正确程序必须保证这个boolean表达式的值为true;
 34      * 如果该值为false,说明程序已经处于不正确的状态下,系统将给出警告并且退出。一般来说,assertion用于保证程序最基本、关键的正确性。
 35      * assertion检查通常在开发和测试时开启。为了提高性能,在软件发布后,assertion检查通常是关闭的。
 36      * 在语法上,为了支持assertion,Java增加了一个关键字assert。它包括两种表达式,分别如下:
 37      * 1. assert expression1;
 38      * 2. assert expression1: expression2;
 39      * expression1表示一个boolean表达式,expression2表示一个基本类型、表达式或者是一个Object,用于在失败时输出错误信息。
 40      * 1.      参数 -esa 和 -dsa:
 41      * 它们含义为开启(关闭)系统类的assertion功能。由于新版本的Java的系统类中,也使了assertion语句,因此如果用户需要观察它们的运行情况,
 42      * 就需要打开系统类的assertion功能 ,我们可使用-esa参数打开,使用 -dsa参数关闭。
 43      * -esa和-dsa的全名为-enablesystemassertions和-disenablesystemassertions,全名和缩写名有同样的功能。
 44      */
 45     assert (paramArrayOfByte.length == 16);
 46     for (int i = 0; i < 8; i++) {
 47       l1 = l1 << 8 | paramArrayOfByte[i] & 0xFF;
 48     }
 49     for (int i = 8; i < 16; i++) {
 50       l2 = l2 << 8 | paramArrayOfByte[i] & 0xFF;
 51     }
 52     this.mostSigBits = l1;
 53     this.leastSigBits = l2;
 54   }
 55   
 56   public UUID(long paramLong1, long paramLong2)
 57   {
 58     this.mostSigBits = paramLong1;
 59     this.leastSigBits = paramLong2;
 60   }
 61   
 62   public static UUID randomUUID()
 63   {
 64     SecureRandom localSecureRandom = Holder.numberGenerator;
 65     byte[] arrayOfByte = new byte[16];
 66     localSecureRandom.nextBytes(arrayOfByte);
 67     byte[] tmp17_14 = arrayOfByte;
 68     tmp17_14[6] = ((byte)(tmp17_14[6] & 0xF));
 69     byte[] tmp27_24 = arrayOfByte;
 70     tmp27_24[6] = ((byte)(tmp27_24[6] | 0x40));
 71     byte[] tmp37_34 = arrayOfByte;
 72     tmp37_34[8] = ((byte)(tmp37_34[8] & 0x3F));
 73     byte[] tmp47_44 = arrayOfByte;
 74     tmp47_44[8] = ((byte)(tmp47_44[8] | 0x80));
 75     return new UUID(arrayOfByte);
 76   }
 77   
 78   public static UUID nameUUIDFromBytes(byte[] paramArrayOfByte)
 79   {
 80     MessageDigest localMessageDigest;
 81     try
 82     {
 83       localMessageDigest = MessageDigest.getInstance("MD5");
 84     }
 85     catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
 86     {
 87       throw new InternalError("MD5 not supported");
 88     }
 89     byte[] arrayOfByte = localMessageDigest.digest(paramArrayOfByte);
 90     byte[] tmp29_26 = arrayOfByte;
 91     tmp29_26[6] = ((byte)(tmp29_26[6] & 0xF));
 92     byte[] tmp39_36 = arrayOfByte;
 93     tmp39_36[6] = ((byte)(tmp39_36[6] | 0x30));
 94     byte[] tmp49_46 = arrayOfByte;
 95     tmp49_46[8] = ((byte)(tmp49_46[8] & 0x3F));
 96     byte[] tmp59_56 = arrayOfByte;
 97     tmp59_56[8] = ((byte)(tmp59_56[8] | 0x80));
 98     return new UUID(arrayOfByte);
 99   }
100   
101   public static UUID fromString(String paramString)
102   {
103     String[] arrayOfString = paramString.split("-");
104     if (arrayOfString.length != 5) {
105       throw new IllegalArgumentException("Invalid UUID string: " + paramString);
106     }
107     for (int i = 0; i < 5; i++) {
108       arrayOfString[i] = ("0x" + arrayOfString[i]);
109     }
110     long l1 = Long.decode(arrayOfString[0]).longValue();
111     l1 <<= 16;
112     l1 |= Long.decode(arrayOfString[1]).longValue();
113     l1 <<= 16;
114     l1 |= Long.decode(arrayOfString[2]).longValue();
115     long l2 = Long.decode(arrayOfString[3]).longValue();
116     l2 <<= 48;
117     l2 |= Long.decode(arrayOfString[4]).longValue();
118     return new UUID(l1, l2);
119   }
120   
121   public long getLeastSignificantBits()
122   {
123     return this.leastSigBits;
124   }
125   
126   public long getMostSignificantBits()
127   {
128     return this.mostSigBits;
129   }
130   
131   public int version()
132   {
133     if (this.version < 0) {
134       this.version = ((int)(this.mostSigBits >> 12 & 0xF));
135     }
136     return this.version;
137   }
138   
139   public int variant()
140   {
141     if (this.variant < 0) {
142       if (this.leastSigBits >>> 63 == 0L) {
143         this.variant = 0;
144       } else if (this.leastSigBits >>> 62 == 2L) {
145         this.variant = 2;
146       } else {
147         this.variant = ((int)(this.leastSigBits >>> 61));
148       }
149     }
150     return this.variant;
151   }
152   
153   public long timestamp()
154   {
155     if (version() != 1) {
156       throw new UnsupportedOperationException("Not a time-based UUID");
157     }
158     long l = this.timestamp;
159     if (l < 0L)
160     {
161       l = (this.mostSigBits & 0xFFF) << 48;
162       l |= (this.mostSigBits >> 16 & 0xFFFF) << 32;
163       l |= this.mostSigBits >>> 32;
164       this.timestamp = l;
165     }
166     return l;
167   }
168   
169   public int clockSequence()
170   {
171     if (version() != 1) {
172       throw new UnsupportedOperationException("Not a time-based UUID");
173     }
174     if (this.sequence < 0) {
175       this.sequence = ((int)((this.leastSigBits & Integer.parseInt(String.valueOf("0x3FFF000000000000"))) >>> 48));
176     }
177     return this.sequence;
178   }
179   
180   public long node()
181   {
182     if (version() != 1) {
183       throw new UnsupportedOperationException("Not a time-based UUID");
184     }
185     if (this.node < 0L) {
186       this.node = (this.leastSigBits & Integer.parseInt(String.valueOf("0xFFFFFFFFFFFF")));
187     }
188     return this.node;
189   }
190   
191   public String toString()
192   {
193     return digits(this.mostSigBits >> 32, 8) + "-" + digits(this.mostSigBits >> 16, 4) + "-" + digits(this.mostSigBits, 4) + "-" + digits(this.leastSigBits >> 48, 4) + "-" + digits(this.leastSigBits, 12);
194   }
195   
196   private static String digits(long paramLong, int paramInt)
197   {
198     long l = 1L << paramInt * 4;
199     return Long.toHexString(l | paramLong & l - 1L).substring(1);
200   }
201   
202   public int hashCode()
203   {
204     if (this.hashCode == -1) {
205       this.hashCode = ((int)(this.mostSigBits >> 32 ^ this.mostSigBits ^ this.leastSigBits >> 32 ^ this.leastSigBits));
206     }
207     return this.hashCode;
208   }
209   
210   public boolean equals(Object paramObject)
211   {
212     if (!(paramObject instanceof UUID)) {
213       return false;
214     }
215     if (((UUID)paramObject).variant() != variant()) {
216       return false;
217     }
218     UUID localUUID = (UUID)paramObject;
219     return (this.mostSigBits == localUUID.mostSigBits) && (this.leastSigBits == localUUID.leastSigBits);
220   }
221   
222   public int compareTo(UUID paramUUID)
223   {
224     return this.leastSigBits > paramUUID.leastSigBits ? 1 : this.leastSigBits < paramUUID.leastSigBits ? -1 : this.mostSigBits > paramUUID.mostSigBits ? 1 : this.mostSigBits < paramUUID.mostSigBits ? -1 : 0;
225   }
226   
227   private void readObject(ObjectInputStream paramObjectInputStream)
228     throws IOException, ClassNotFoundException
229   {
230     paramObjectInputStream.defaultReadObject();
231     this.version = -1;
232     this.variant = -1;
233     this.timestamp = -1L;
234     this.sequence = -1;
235     this.node = -1L;
236     this.hashCode = -1;
237   }
238   
239   private static class Holder
240   {
241     static final SecureRandom numberGenerator = new SecureRandom();
242     
243     private Holder() {}
244   }
245 }
点击+展开

 

posted @ 2017-04-27 14:17  音动九箫  阅读(154)  评论(0编辑  收藏  举报