Skip to content

Commit

Permalink
SimpleIdGenerator rolls over at Long.MAX_VALUE
Browse files Browse the repository at this point in the history
Closes gh-25485
  • Loading branch information
rstoyanchev committed Sep 13, 2020
1 parent 852718e commit 49356b2
Showing 1 changed file with 4 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,25 +20,20 @@
import java.util.concurrent.atomic.AtomicLong;

/**
* A simple {@link IdGenerator} that starts at 1 and increments by 1 with each call.
* A simple {@link IdGenerator} that starts at 1, increments up to
* {@link Long#MAX_VALUE}, and then rolls over.
*
* @author Rossen Stoyanchev
* @since 4.1.5
*/
public class SimpleIdGenerator implements IdGenerator {

private final AtomicLong mostSigBits = new AtomicLong(0);

private final AtomicLong leastSigBits = new AtomicLong(0);


@Override
public UUID generateId() {
long leastSigBits = this.leastSigBits.incrementAndGet();
if (leastSigBits == 0) {
this.mostSigBits.incrementAndGet();
}
return new UUID(this.mostSigBits.get(), leastSigBits);
return new UUID(0, this.leastSigBits.incrementAndGet());
}

}

0 comments on commit 49356b2

Please sign in to comment.