KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > id > AbstractUUIDGenerator


1 //$Id: AbstractUUIDGenerator.java,v 1.1 2004/12/19 16:24:10 oneovthafew Exp $
2
package org.hibernate.id;
3
4 import java.net.InetAddress JavaDoc;
5 import org.hibernate.util.BytesHelper;
6
7 /**
8  * The base class for identifier generators that use a UUID algorithm. This
9  * class implements the algorithm, subclasses define the identifier
10  * format.
11  *
12  * @see UUIDHexGenerator
13  * @author Gavin King
14  */

15
16 public abstract class AbstractUUIDGenerator implements IdentifierGenerator {
17
18     private static final int IP;
19     static {
20         int ipadd;
21         try {
22             ipadd = BytesHelper.toInt( InetAddress.getLocalHost().getAddress() );
23         }
24         catch (Exception JavaDoc e) {
25             ipadd = 0;
26         }
27         IP = ipadd;
28     }
29     private static short counter = (short) 0;
30     private static final int JVM = (int) ( System.currentTimeMillis() >>> 8 );
31
32     public AbstractUUIDGenerator() {
33     }
34
35     /**
36      * Unique across JVMs on this machine (unless they load this class
37      * in the same quater second - very unlikely)
38      */

39     protected int getJVM() {
40         return JVM;
41     }
42
43     /**
44      * Unique in a millisecond for this JVM instance (unless there
45      * are > Short.MAX_VALUE instances created in a millisecond)
46      */

47     protected short getCount() {
48         synchronized(AbstractUUIDGenerator.class) {
49             if (counter<0) counter=0;
50             return counter++;
51         }
52     }
53
54     /**
55      * Unique in a local network
56      */

57     protected int getIP() {
58         return IP;
59     }
60
61     /**
62      * Unique down to millisecond
63      */

64     protected short getHiTime() {
65         return (short) ( System.currentTimeMillis() >>> 32 );
66     }
67     protected int getLoTime() {
68         return (int) System.currentTimeMillis();
69     }
70
71
72 }
73
74
75
76
77
78
Popular Tags