KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > jdbc > UIDGenerator


1 /*
2  *
3  * de.mweb.cjl.util.UIDGenerator
4  *
5  * Copyright (C) 2001 Frank Mueller / Oldenburg / Germnay
6  *
7  * 12-JUN-2001 Frank Mueller <frank@mweb.de>
8  * - initial class creation
9  * 06-SEP-2001 Frank Mueller <frank@mweb.de>
10  * - changed random to use static detail instead
11  * 30-SEP-2001 Frank Mueller <frank@mweb.de>
12  * - generate fixed length string
13  *
14  */

15
16 package org.snipsnap.jdbc;
17
18 /* import */
19
20 import java.math.BigInteger JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.net.UnknownHostException JavaDoc;
23 import java.util.Random JavaDoc;
24
25 /**
26  *
27  * The UIDGenerator generates worldwide unique strings
28  * for identifying purposes. The length of this
29  * string is 55 chars.
30  *
31  * @author Frank Mueller
32  * @version $Id: UIDGenerator.java 1257 2003-12-11 13:36:55Z leo $
33  *
34  */

35
36 public final class UIDGenerator {
37
38   /* public statics */
39
40   public final static int MAX_LENGTH = 55;
41
42   /* private statics */
43
44   private final static String JavaDoc ZEROS = "0000000000000";
45   private final static int ZEROSLENGTH = ZEROS.length();
46
47   private static long detail = normalize((new Random JavaDoc()).nextLong());
48
49   /**
50    *
51    * Generates a UID string.
52    *
53    * @param aCode A classification code.
54    * @return The UID string.
55    *
56    */

57
58   public final static String JavaDoc generate(long aCode) {
59     /* get base values */
60
61     long code = normalize(aCode);
62     long ip = getIP();
63     long timestamp = System.currentTimeMillis();
64
65     /* convert to string (BASE36 = 0-9 and a-z) */
66
67     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
68
69     buffer.append(createString(code));
70     buffer.append("-");
71     buffer.append(createString(ip));
72     buffer.append("-");
73     buffer.append(createString(timestamp));
74     buffer.append("-");
75     buffer.append(createString(detail));
76
77     return buffer.toString().toUpperCase();
78   }
79
80   /**
81    *
82    * Generates a UID string for a given class.
83    *
84    * @param aClass The class specifying the classification code.
85    * @return The UID string.
86    *
87    */

88
89   public final static String JavaDoc generate(Class JavaDoc aClass) {
90     return UIDGenerator.generate(aClass.hashCode());
91   }
92
93   /**
94    *
95    * Gets the IP address of the local host as a long.
96    *
97    * @return The IP address as a long.
98    *
99    */

100
101   private final static long getIP() {
102     try {
103       InetAddress JavaDoc localHost = InetAddress.getLocalHost();
104       BigInteger JavaDoc bigIP = new BigInteger JavaDoc(localHost.getAddress());
105
106       return normalize(bigIP.longValue());
107     }
108     catch (UnknownHostException JavaDoc uhex) {
109       return 0;
110     }
111   }
112
113   /**
114    *
115    * Normalizes a long value.
116    *
117    * @param value The original value.
118    * @return The normalized value.
119    *
120    */

121
122   private final static long normalize(long value) {
123     if (value > 0) return value;
124
125     if (value == Long.MIN_VALUE)
126       return Long.MAX_VALUE;
127     else
128       return value * -1;
129   }
130
131   /**
132    *
133    * Creates a fixed length string out of a long value.
134    *
135    * @param value The long value.
136    * @return The fixed length string.
137    *
138    */

139
140   private final static String JavaDoc createString(long value) {
141     String JavaDoc baseString = Long.toString(value, 36);
142     String JavaDoc result = ZEROS.substring(0, ZEROSLENGTH - baseString.length()) + baseString;
143
144     return result;
145   }
146 }
147
148 /* EOF */
149
Popular Tags