Browse Source

SimpleIdGenerator rolls over at Long.MAX_VALUE

Closes gh-25485
pull/25798/head
Rossen Stoyanchev 5 years ago
parent
commit
49356b2c0f
  1. 13
      spring-core/src/main/java/org/springframework/util/SimpleIdGenerator.java

13
spring-core/src/main/java/org/springframework/util/SimpleIdGenerator.java

@ -1,5 +1,5 @@ @@ -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.
@ -20,25 +20,20 @@ import java.util.UUID; @@ -20,25 +20,20 @@ import java.util.UUID;
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());
}
}

Loading…
Cancel
Save