Skip to content

Commit 78f9767

Browse files
committed
[waterflow] supports configuring uuid generator
1 parent f6e10f6 commit 78f9767

File tree

2 files changed

+46
-1
lines changed
  • framework/waterflow/java/waterflow-core/src

2 files changed

+46
-1
lines changed

framework/waterflow/java/waterflow-core/src/main/java/modelengine/fit/waterflow/domain/utils/UUIDUtil.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package modelengine.fit.waterflow.domain.utils;
88

99
import java.util.UUID;
10+
import java.util.function.Supplier;
1011

1112
/**
1213
* Uuid的Utils类。
@@ -15,12 +16,25 @@
1516
* @since 1.0
1617
*/
1718
public class UUIDUtil {
19+
private static Supplier<String> uuidGenerator = () -> UUID.randomUUID().toString().replace("-", "");
1820
/**
1921
* 随机生成uuid。
2022
*
2123
* @return 随机生成的uuid的 {@link String}。
2224
*/
2325
public static String uuid() {
24-
return UUID.randomUUID().toString().replace("-", "");
26+
return uuidGenerator.get();
27+
}
28+
29+
/**
30+
* 全局替换 uuid 的生成器。
31+
*
32+
* @param uuidGenerator 表示要设置的 uuid 生成器的 {@link Supplier}{@code <}{@link String}{@code >}。
33+
* @return 表示设置前使用的 uuid 生成器的 {@link Supplier}{@code <}{@link String}{@code >}。
34+
*/
35+
public static Supplier<String> setUuidGenerator(Supplier<String> uuidGenerator) {
36+
Supplier<String> oldUuidGenerator = UUIDUtil.uuidGenerator;
37+
UUIDUtil.uuidGenerator = uuidGenerator;
38+
return oldUuidGenerator;
2539
}
2640
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*--------------------------------------------------------------------------------------------*/
6+
7+
package modelengine.fit.waterflow.domain.utils;
8+
9+
import org.junit.jupiter.api.Assertions;
10+
import org.junit.jupiter.api.Test;
11+
12+
import java.util.concurrent.atomic.AtomicInteger;
13+
import java.util.function.Supplier;
14+
15+
/**
16+
* {@link UUIDUtil} 的测试。
17+
*
18+
* @author 宋永坦
19+
* @since 2025-09-18
20+
*/
21+
class UUIDUtilTest {
22+
@Test
23+
void shouldGetIdWhenExecuteUuidGivenNewIdGenerator() {
24+
AtomicInteger idBase = new AtomicInteger(1);
25+
Supplier<String> oldGenerator = UUIDUtil.setUuidGenerator(() -> Integer.toString(idBase.getAndIncrement()));
26+
27+
Assertions.assertEquals("1", UUIDUtil.uuid());
28+
Assertions.assertEquals("2", UUIDUtil.uuid());
29+
Assertions.assertEquals(32, oldGenerator.get().length());
30+
}
31+
}

0 commit comments

Comments
 (0)