File tree Expand file tree Collapse file tree 2 files changed +53
-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 +53
-1
lines changed Original file line number Diff line number Diff line change 66
77package modelengine .fit .waterflow .domain .utils ;
88
9+ import modelengine .fitframework .inspection .Validation ;
10+
911import java .util .UUID ;
12+ import java .util .function .Supplier ;
1013
1114/**
1215 * Uuid的Utils类。
1518 * @since 1.0
1619 */
1720public 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}
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+ 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+ }
You can’t perform that action at this time.
0 commit comments