From 49356b2c0f498ae6580c1ef7d7ac0787629de7c8 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Sat, 12 Sep 2020 03:27:15 +0100 Subject: [PATCH] SimpleIdGenerator rolls over at Long.MAX_VALUE Closes gh-25485 --- .../org/springframework/util/SimpleIdGenerator.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/SimpleIdGenerator.java b/spring-core/src/main/java/org/springframework/util/SimpleIdGenerator.java index c8d810f45b4c..484d4ffc9a0a 100644 --- a/spring-core/src/main/java/org/springframework/util/SimpleIdGenerator.java +++ b/spring-core/src/main/java/org/springframework/util/SimpleIdGenerator.java @@ -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.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()); } }