-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuid.java
More file actions
56 lines (46 loc) · 1.48 KB
/
Guid.java
File metadata and controls
56 lines (46 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package javaxt.exchange;
//******************************************************************************
//** GUID Class
//******************************************************************************
/**
* Used to generate a random sequence of characters used to resemble a
* Microsoft GUID.
*
* "[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}"
*
******************************************************************************/
public class Guid {
private final static String chars = "0123456789ABCDEF"; //GHIJKLMNOPQRSTUVWXYZ
private String id;
public Guid(){
id =
getRandomChars(8) + "-" +
getRandomChars(4) + "-" +
getRandomChars(4) + "-" +
getRandomChars(4) + "-" +
getRandomChars(12);
//"c11ff724-aa03-4555-9952-8fa248a11c3e"
}
public String toString(){
return id;
}
public int hashCode(){
return id.hashCode();
}
public boolean equals(Object obj){
if (obj!=null){
if (obj instanceof String || obj instanceof Guid){
return obj.toString().equalsIgnoreCase(id);
}
}
return false;
}
private String getRandomChars(int numChars){
StringBuffer str = new StringBuffer();
for (int i = 1; i<=numChars; i++){
int x = new java.util.Random().nextInt(chars.length());
str.append(chars.substring(x,x+1));
}
return str.toString();
}
}