Skip to content

Commit 3cbc375

Browse files
committed
Merge branch '3.5.x'
2 parents 32863cd + ad6c259 commit 3cbc375

File tree

2 files changed

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

2 files changed

+53
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
package modelengine.fit.waterflow.domain.utils;
88

9+
import modelengine.fitframework.inspection.Validation;
10+
911
import java.util.UUID;
12+
import java.util.function.Supplier;
1013

1114
/**
1215
* Uuid的Utils类。
@@ -15,12 +18,27 @@
1518
* @since 1.0
1619
*/
1720
public class UUIDUtil {
21+
private static Supplier<String> uuidGenerator = () -> UUID.randomUUID().toString().replace("-", "");
22+
1823
/**
1924
* 随机生成uuid。
2025
*
2126
* @return 随机生成的uuid的 {@link String}。
2227
*/
2328
public static String uuid() {
24-
return UUID.randomUUID().toString().replace("-", "");
29+
return uuidGenerator.get();
30+
}
31+
32+
/**
33+
* 全局替换 uuid 的生成器。
34+
*
35+
* @param uuidGenerator 表示要设置的 uuid 生成器的 {@link Supplier}{@code <}{@link String}{@code >}。
36+
* @return 表示设置前使用的 uuid 生成器的 {@link Supplier}{@code <}{@link String}{@code >}。
37+
*/
38+
public static synchronized Supplier<String> setUuidGenerator(Supplier<String> uuidGenerator) {
39+
Validation.notNull(uuidGenerator, "The uuid generator should not be null.");
40+
Supplier<String> oldUuidGenerator = UUIDUtil.uuidGenerator;
41+
UUIDUtil.uuidGenerator = uuidGenerator;
42+
return oldUuidGenerator;
2543
}
2644
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
String uuid1 = UUIDUtil.uuid();
28+
String uuid2 = UUIDUtil.uuid();
29+
UUIDUtil.setUuidGenerator(oldGenerator);
30+
31+
Assertions.assertEquals("1", uuid1);
32+
Assertions.assertEquals("2", uuid2);
33+
}
34+
}

0 commit comments

Comments
 (0)