KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cache > Timestamper


1 //$Id: Timestamper.java,v 1.1 2004/06/03 16:30:05 steveebersole Exp $
2
package org.hibernate.cache;
3
4 /**
5  * Generates increasing identifiers (in a single VM only).
6  * Not valid across multiple VMs. Identifiers are not necessarily
7  * strictly increasing, but usually are.
8  */

9 public final class Timestamper {
10     private static short counter = 0;
11     private static long time;
12     private static final int BIN_DIGITS = 12;
13     public static final short ONE_MS = 1<<BIN_DIGITS;
14     
15     public static long next() {
16         synchronized(Timestamper.class) {
17             long newTime = System.currentTimeMillis() << BIN_DIGITS;
18             if (time<newTime) {
19                 time = newTime;
20                 counter = 0;
21             }
22             else if (counter < ONE_MS - 1 ) {
23                 counter++;
24             }
25             
26             return time + counter;
27         }
28     }
29
30     private Timestamper() {}
31 }
32
33
34
35
36
37
38
Popular Tags