Created
November 20, 2024 16:05
-
-
Save fabiolimace/a6110a1490b93e99accd0cfc4a004e6a to your computer and use it in GitHub Desktop.
TSID.diff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/main/java/io/hypersistence/tsid/TSID.java b/src/main/java/io/hypersistence/tsid/TSID.java | |
index 1ca20ca..665f081 100644 | |
--- a/src/main/java/io/hypersistence/tsid/TSID.java | |
+++ b/src/main/java/io/hypersistence/tsid/TSID.java | |
@@ -36,6 +36,7 @@ import java.util.concurrent.atomic.AtomicInteger; | |
import java.util.concurrent.locks.ReentrantLock; | |
import java.util.function.IntFunction; | |
import java.util.function.IntSupplier; | |
+import java.util.function.Supplier; | |
/** | |
* A value object that represents a Time-Sorted Unique Identifier (TSID). | |
@@ -790,17 +791,14 @@ public final class TSID implements Serializable, Comparable<TSID> { | |
* and reuse a single instance of {@link Factory} per node in your | |
* distributed system. | |
*/ | |
- public static final class Factory { | |
+ public static class Factory { | |
private static final ReentrantLock LOCK = new ReentrantLock(); | |
- public static final Factory INSTANCE = new Factory(); | |
- | |
- public static final Factory INSTANCE_256 = newInstance256(); | |
- | |
- public static final Factory INSTANCE_1024 = newInstance1024(); | |
- | |
- public static final Factory INSTANCE_4096 = newInstance4096(); | |
+ public static final Factory INSTANCE = new Proxy(Factory::new); | |
+ public static final Factory INSTANCE_256 = new Proxy(Factory::newInstance256); | |
+ public static final Factory INSTANCE_1024 = new Proxy(Factory::newInstance1024); | |
+ public static final Factory INSTANCE_4096 = new Proxy(Factory::newInstance4096); | |
public static final IntSupplier THREAD_LOCAL_RANDOM_FUNCTION = () -> ThreadLocalRandom.current().nextInt(); | |
@@ -1543,4 +1541,37 @@ public final class TSID implements Serializable, Comparable<TSID> { | |
return INSTANCE_4096.generate(); | |
} | |
} | |
+ | |
+ private static class Proxy extends Factory { | |
+ | |
+ private Factory factory = null; | |
+ private Supplier<Factory> supplier; | |
+ private static final ReentrantLock LOCK = new ReentrantLock(); | |
+ | |
+ public Proxy(Supplier<Factory> supplier) { | |
+ this.supplier = supplier; | |
+ } | |
+ | |
+ private Factory get() { | |
+ | |
+ if (factory != null) { | |
+ return factory; | |
+ } | |
+ | |
+ try { | |
+ LOCK.lock(); | |
+ if (factory == null) { | |
+ this.factory = supplier.get(); | |
+ } | |
+ return this.factory; | |
+ } finally { | |
+ LOCK.unlock(); | |
+ } | |
+ } | |
+ | |
+ @Override | |
+ public TSID generate() { | |
+ return get().generate(); | |
+ } | |
+ } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment