Skip to content

Commit 79b05f3

Browse files
committed
support for replacing ObjectUuid random generators
1 parent a6c2145 commit 79b05f3

File tree

3 files changed

+79
-46
lines changed

3 files changed

+79
-46
lines changed

modules/typed-ids/src/main/java/org/framefork/typedIds/uuid/ObjectUuid.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.io.Serial;
1212
import java.util.Objects;
1313
import java.util.UUID;
14+
import java.util.concurrent.ConcurrentHashMap;
15+
import java.util.concurrent.atomic.AtomicReference;
1416
import java.util.function.Function;
1517

1618
/**
@@ -46,7 +48,7 @@ protected static <SelfType extends ObjectUuid<SelfType>> SelfType randomUUID(
4648
final Function<UUID, SelfType> constructor
4749
)
4850
{
49-
return constructor.apply(ObjectUuidGenerator.randomUUid(constructor));
51+
return constructor.apply(Generators.randomUuid(constructor));
5052
}
5153

5254
@NotNull
@@ -211,4 +213,43 @@ private void readObject(final ObjectInputStream stream)
211213
throw new UnsupportedOperationException("Java Serialization is not supported. Do you have properly configured mapping for this type?");
212214
}
213215

216+
/**
217+
* This class exists for two reasons: 1) to allow reusing generator instances per-type 2) to allow configuring a different generators factory.
218+
* This could be useful, if you want e.g. deterministic IDs in your tests.
219+
*/
220+
public static final class Generators
221+
{
222+
223+
private static final AtomicReference<UuidGenerator.Factory> FACTORY = new AtomicReference<>(new UuidGenerator.Factory.DefaultUuidV7GeneratorFactory());
224+
225+
private static final ConcurrentHashMap<Function<?, ?>, UuidGenerator> GENERATORS = new ConcurrentHashMap<>();
226+
227+
private Generators()
228+
{
229+
}
230+
231+
static UUID randomUuid(final Function<?, ?> constructor)
232+
{
233+
return getGenerator(constructor).generate();
234+
}
235+
236+
static UuidGenerator getGenerator(final Function<?, ?> constructor)
237+
{
238+
return GENERATORS.computeIfAbsent(
239+
constructor,
240+
c -> FACTORY.get().getGenerator(c)
241+
);
242+
}
243+
244+
/**
245+
* Replaces the factory with provided implementation and throws away any pre-existing generators.
246+
*/
247+
static void setFactory(final UuidGenerator.Factory factory)
248+
{
249+
FACTORY.set(factory);
250+
GENERATORS.clear();
251+
}
252+
253+
}
254+
214255
}

modules/typed-ids/src/main/java/org/framefork/typedIds/uuid/ObjectUuidGenerator.java

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.framefork.typedIds.uuid;
2+
3+
import com.fasterxml.uuid.Generators;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
import java.util.UUID;
7+
import java.util.function.Function;
8+
9+
public interface UuidGenerator
10+
{
11+
12+
@NotNull
13+
UUID generate();
14+
15+
/**
16+
* This could be useful, if you want e.g. deterministic IDs in your tests. See {@link ObjectUuid.Generators}
17+
*/
18+
interface Factory
19+
{
20+
21+
@NotNull
22+
UuidGenerator getGenerator(final Function<?, ?> constructor);
23+
24+
final class DefaultUuidV7GeneratorFactory implements Factory
25+
{
26+
27+
@Override
28+
public UuidGenerator getGenerator(final Function<?, ?> constructor)
29+
{
30+
return Generators.timeBasedEpochRandomGenerator()::generate;
31+
}
32+
33+
}
34+
35+
}
36+
37+
}

0 commit comments

Comments
 (0)