File tree Expand file tree Collapse file tree 2 files changed +46
-1
lines changed
framework/waterflow/java/waterflow-core/src
main/java/modelengine/fit/waterflow/domain/utils
test/java/modelengine/fit/waterflow/domain/utils Expand file tree Collapse file tree 2 files changed +46
-1
lines changed Original file line number Diff line number Diff line change 77package modelengine .fit .waterflow .domain .utils ;
88
99import java .util .UUID ;
10+ import java .util .function .Supplier ;
1011
1112/**
1213 * Uuid的Utils类。
1516 * @since 1.0
1617 */
1718public 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments