Fix style and add credit where it is due

This commit is contained in:
Emmanuel Bernard 2012-04-18 18:11:47 +02:00
parent 7abdf80ebe
commit 75d684149d
1 changed files with 18 additions and 12 deletions

View File

@ -28,23 +28,29 @@ import java.util.concurrent.atomic.AtomicLong;
/**
* Generates increasing identifiers (in a single VM only). Not valid across multiple VMs. Identifiers are not
* necessarily strictly increasing, but usually are.
*
* Core while loop implemented by Alex Snaps - EHCache project - under ASL 2.0
*
* @author Hibernate team
* @author Alex Snaps
*/
public final class Timestamper {
private static final int BIN_DIGITS = 12;
public static final short ONE_MS = 1<<BIN_DIGITS;
public static final short ONE_MS = 1 << BIN_DIGITS;
private static final AtomicLong VALUE = new AtomicLong();
public static long next() {
while (true) {
long base = System.currentTimeMillis() << BIN_DIGITS;
long maxValue = base + ONE_MS - 1;
for (long current = VALUE.get(), update = Math.max(base, current + 1); update < maxValue;
current = VALUE.get(), update = Math.max(base, current + 1)) {
if (VALUE.compareAndSet(current, update)) {
return update;
}
}
}
public static long next() {
while ( true ) {
long base = System.currentTimeMillis() << BIN_DIGITS;
long maxValue = base + ONE_MS - 1;
for ( long current = VALUE.get(), update = Math.max( base, current + 1 ); update < maxValue;
current = VALUE.get(), update = Math.max( base, current + 1 ) ) {
if ( VALUE.compareAndSet( current, update ) ) {
return update;
}
}
}
}
private Timestamper() {