Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

package modelengine.fit.waterflow.domain.utils;

import modelengine.fitframework.inspection.Validation;

import java.util.UUID;
import java.util.function.Supplier;

/**
* Uuid的Utils类。
Expand All @@ -15,12 +18,27 @@
* @since 1.0
*/
public class UUIDUtil {
private static Supplier<String> uuidGenerator = () -> UUID.randomUUID().toString().replace("-", "");

/**
* 随机生成uuid。
*
* @return 随机生成的uuid的 {@link String}。
*/
public static String uuid() {
return UUID.randomUUID().toString().replace("-", "");
return uuidGenerator.get();
}

/**
* 全局替换 uuid 的生成器。
*
* @param uuidGenerator 表示要设置的 uuid 生成器的 {@link Supplier}{@code <}{@link String}{@code >}。
* @return 表示设置前使用的 uuid 生成器的 {@link Supplier}{@code <}{@link String}{@code >}。
*/
public static synchronized Supplier<String> setUuidGenerator(Supplier<String> uuidGenerator) {
Validation.notNull(uuidGenerator, "The uuid generator should not be null.");
Supplier<String> oldUuidGenerator = UUIDUtil.uuidGenerator;
UUIDUtil.uuidGenerator = uuidGenerator;
return oldUuidGenerator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

package modelengine.fit.waterflow.domain.utils;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;

/**
* {@link UUIDUtil} 的测试。
*
* @author 宋永坦
* @since 2025-09-18
*/
class UUIDUtilTest {
@Test
void shouldGetIdWhenExecuteUuidGivenNewIdGenerator() {
AtomicInteger idBase = new AtomicInteger(1);
Supplier<String> oldGenerator = UUIDUtil.setUuidGenerator(() -> Integer.toString(idBase.getAndIncrement()));

String uuid1 = UUIDUtil.uuid();
String uuid2 = UUIDUtil.uuid();
UUIDUtil.setUuidGenerator(oldGenerator);

Assertions.assertEquals("1", uuid1);
Assertions.assertEquals("2", uuid2);
}
}