refactor: 使用 CollUtils 替代部分 Stream 集合转换

This commit is contained in:
2025-07-17 20:49:38 +08:00
parent 2f2aae08ab
commit e05e0de7b8
5 changed files with 14 additions and 47 deletions

View File

@@ -48,6 +48,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.continew.starter.apidoc.handler.BaseEnumParameterHandler;
import top.continew.starter.apidoc.handler.OpenApiHandler;
import top.continew.starter.core.autoconfigure.application.ApplicationProperties;
import top.continew.starter.core.util.CollUtils;
import top.continew.starter.core.util.GeneralPropertySourceFactory;
import java.util.List;
@@ -104,7 +105,7 @@ public class SpringDocAutoConfiguration implements WebMvcConfigurer {
Map<String, SecurityScheme> securitySchemeMap = components.getSecuritySchemes();
if (MapUtil.isNotEmpty(securitySchemeMap)) {
SecurityRequirement securityRequirement = new SecurityRequirement();
List<String> list = securitySchemeMap.values().stream().map(SecurityScheme::getName).toList();
List<String> list = CollUtils.mapToList(securitySchemeMap.values(), SecurityScheme::getName);
list.forEach(securityRequirement::addList);
openApi.addSecurityItem(securityRequirement);
}
@@ -127,10 +128,8 @@ public class SpringDocAutoConfiguration implements WebMvcConfigurer {
Map<String, SecurityScheme> securitySchemeMap = components.getSecuritySchemes();
pathItem.readOperations().forEach(operation -> {
SecurityRequirement securityRequirement = new SecurityRequirement();
List<String> list = securitySchemeMap.values()
.stream()
.map(SecurityScheme::getName)
.toList();
List<String> list = CollUtils.mapToList(securitySchemeMap
.values(), SecurityScheme::getName);
list.forEach(securityRequirement::addList);
operation.addSecurityItem(securityRequirement);
});

View File

@@ -37,11 +37,11 @@ import org.springdoc.core.utils.PropertyResolverUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.web.method.HandlerMethod;
import top.continew.starter.core.util.CollUtils;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -176,11 +176,8 @@ public class OpenApiHandler extends OpenAPIService {
buildTagsFromClass(handlerMethod.getBeanType(), tags, tagsStr, locale);
if (CollUtil.isNotEmpty(tagsStr)) {
tagsStr = tagsStr.stream()
.map(str -> propertyResolverUtils.resolve(str, locale))
.collect(Collectors.toSet());
tagsStr = CollUtils.mapToSet(tagsStr, str -> propertyResolverUtils.resolve(str, locale));
}
if (springdocTags.containsKey(handlerMethod)) {
Tag tag = springdocTags.get(handlerMethod);
tagsStr.add(tag.getName());
@@ -256,7 +253,7 @@ public class OpenApiHandler extends OpenAPIService {
methodTags.addAll(AnnotatedElementUtils
.findAllMergedAnnotations(method, io.swagger.v3.oas.annotations.tags.Tag.class));
if (CollUtil.isNotEmpty(methodTags)) {
tagsStr.addAll(toSet(methodTags, tag -> propertyResolverUtils.resolve(tag.name(), locale)));
tagsStr.addAll(CollUtils.mapToSet(methodTags, tag -> propertyResolverUtils.resolve(tag.name(), locale)));
List<io.swagger.v3.oas.annotations.tags.Tag> allTags = new ArrayList<>(methodTags);
addTags(allTags, tags, locale);
}
@@ -275,22 +272,4 @@ public class OpenApiHandler extends OpenAPIService {
});
});
}
/**
* 将collection转化为Set集合但是两者的泛型不同<br>
* <B>{@code Collection<E> ------> Set<T> } </B>
*
* @param collection 需要转化的集合
* @param function collection中的泛型转化为set泛型的lambda表达式
* @param <E> collection中的泛型
* @param <T> Set中的泛型
* @return 转化后的Set
*/
public static <E, T> Set<T> toSet(Collection<E> collection, Function<E, T> function) {
if (CollUtil.isEmpty(collection) || function == null) {
return CollUtil.newHashSet();
}
return collection.stream().map(function).filter(Objects::nonNull).collect(Collectors.toSet());
}
}

View File

@@ -45,7 +45,7 @@ public class ReflectUtils {
*/
public static List<String> getNonStaticFieldsName(Class<?> beanClass) throws SecurityException {
List<Field> nonStaticFields = getNonStaticFields(beanClass);
return nonStaticFields.stream().map(Field::getName).collect(Collectors.toList());
return CollUtils.mapToList(nonStaticFields, Field::getName);
}
/**

View File

@@ -23,6 +23,7 @@ import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.continew.starter.core.util.CollUtils;
import top.continew.starter.license.exception.LicenseException;
import top.continew.starter.license.model.LicenseExtraModel;
@@ -33,7 +34,6 @@ import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 服务器信息工具类
@@ -264,10 +264,7 @@ public class ServerInfoUtils {
// 获取所有网络接口
Set<InetAddress> inetAddresses = getLocalAllInetAddress();
if (CollectionUtil.isNotEmpty(inetAddresses)) {
return inetAddresses.stream()
.map(ServerInfoUtils::getMacByInetAddress)
.distinct()
.collect(Collectors.toSet());
return CollUtils.mapToSet(inetAddresses, ServerInfoUtils::getMacByInetAddress);
}
return Collections.emptySet();
}
@@ -282,11 +279,7 @@ public class ServerInfoUtils {
// 获取所有网络接口
Set<InetAddress> inetAddresses = getLocalAllInetAddress();
if (CollectionUtil.isNotEmpty(inetAddresses)) {
return inetAddresses.stream()
.map(InetAddress::getHostAddress)
.distinct()
.map(String::toLowerCase)
.collect(Collectors.toSet());
return CollUtils.mapToSet(inetAddresses, InetAddress::getHostAddress);
}
return Collections.emptySet();
}

View File

@@ -31,8 +31,7 @@ import io.undertow.Undertow;
import io.undertow.server.handlers.DisallowedMethodsHandler;
import io.undertow.util.HttpString;
import top.continew.starter.core.constant.PropertiesConstants;
import java.util.stream.Collectors;
import top.continew.starter.core.util.CollUtils;
/**
* Undertow 自动配置
@@ -57,11 +56,8 @@ public class UndertowAutoConfiguration {
public WebServerFactoryCustomizer<UndertowServletWebServerFactory> customize(ServerExtensionProperties properties) {
return factory -> {
factory.addDeploymentInfoCustomizers(deploymentInfo -> deploymentInfo
.addInitialHandlerChainWrapper(handler -> new DisallowedMethodsHandler(handler, properties
.getDisallowedMethods()
.stream()
.map(HttpString::tryFromString)
.collect(Collectors.toSet()))));
.addInitialHandlerChainWrapper(handler -> new DisallowedMethodsHandler(handler, CollUtils
.mapToSet(properties.getDisallowedMethods(), HttpString::tryFromString))));
log.debug("[ContiNew Starter] - Disallowed HTTP methods on Server Undertow: {}.", properties
.getDisallowedMethods());
log.debug("[ContiNew Starter] - Auto Configuration 'Web-Server Undertow' completed initialization.");