From e05e0de7b81329512ea1f0ad5e9ed3c04bdfe752 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Thu, 17 Jul 2025 20:49:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=20CollUtils=20?= =?UTF-8?q?=E6=9B=BF=E4=BB=A3=E9=83=A8=E5=88=86=20Stream=20=E9=9B=86?= =?UTF-8?q?=E5=90=88=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SpringDocAutoConfiguration.java | 9 +++---- .../apidoc/handler/OpenApiHandler.java | 27 +++---------------- .../starter/core/util/ReflectUtils.java | 2 +- .../starter/license/util/ServerInfoUtils.java | 13 +++------ .../server/UndertowAutoConfiguration.java | 10 +++---- 5 files changed, 14 insertions(+), 47 deletions(-) diff --git a/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java b/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java index 3e626748..6e106005 100644 --- a/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java +++ b/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java @@ -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 securitySchemeMap = components.getSecuritySchemes(); if (MapUtil.isNotEmpty(securitySchemeMap)) { SecurityRequirement securityRequirement = new SecurityRequirement(); - List list = securitySchemeMap.values().stream().map(SecurityScheme::getName).toList(); + List list = CollUtils.mapToList(securitySchemeMap.values(), SecurityScheme::getName); list.forEach(securityRequirement::addList); openApi.addSecurityItem(securityRequirement); } @@ -127,10 +128,8 @@ public class SpringDocAutoConfiguration implements WebMvcConfigurer { Map securitySchemeMap = components.getSecuritySchemes(); pathItem.readOperations().forEach(operation -> { SecurityRequirement securityRequirement = new SecurityRequirement(); - List list = securitySchemeMap.values() - .stream() - .map(SecurityScheme::getName) - .toList(); + List list = CollUtils.mapToList(securitySchemeMap + .values(), SecurityScheme::getName); list.forEach(securityRequirement::addList); operation.addSecurityItem(securityRequirement); }); diff --git a/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/handler/OpenApiHandler.java b/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/handler/OpenApiHandler.java index 217e2410..bf3eaa81 100644 --- a/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/handler/OpenApiHandler.java +++ b/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/handler/OpenApiHandler.java @@ -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 allTags = new ArrayList<>(methodTags); addTags(allTags, tags, locale); } @@ -275,22 +272,4 @@ public class OpenApiHandler extends OpenAPIService { }); }); } - - /** - * 将collection转化为Set集合,但是两者的泛型不同
- * {@code Collection ------> Set } - * - * @param collection 需要转化的集合 - * @param function collection中的泛型转化为set泛型的lambda表达式 - * @param collection中的泛型 - * @param Set中的泛型 - * @return 转化后的Set - */ - public static Set toSet(Collection collection, Function function) { - if (CollUtil.isEmpty(collection) || function == null) { - return CollUtil.newHashSet(); - } - return collection.stream().map(function).filter(Objects::nonNull).collect(Collectors.toSet()); - } - } diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/util/ReflectUtils.java b/continew-starter-core/src/main/java/top/continew/starter/core/util/ReflectUtils.java index c852bca3..2339e0b9 100644 --- a/continew-starter-core/src/main/java/top/continew/starter/core/util/ReflectUtils.java +++ b/continew-starter-core/src/main/java/top/continew/starter/core/util/ReflectUtils.java @@ -45,7 +45,7 @@ public class ReflectUtils { */ public static List getNonStaticFieldsName(Class beanClass) throws SecurityException { List nonStaticFields = getNonStaticFields(beanClass); - return nonStaticFields.stream().map(Field::getName).collect(Collectors.toList()); + return CollUtils.mapToList(nonStaticFields, Field::getName); } /** diff --git a/continew-starter-license/continew-starter-license-core/src/main/java/top/continew/starter/license/util/ServerInfoUtils.java b/continew-starter-license/continew-starter-license-core/src/main/java/top/continew/starter/license/util/ServerInfoUtils.java index ca188d9e..d87e7e26 100644 --- a/continew-starter-license/continew-starter-license-core/src/main/java/top/continew/starter/license/util/ServerInfoUtils.java +++ b/continew-starter-license/continew-starter-license-core/src/main/java/top/continew/starter/license/util/ServerInfoUtils.java @@ -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 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 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(); } diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/server/UndertowAutoConfiguration.java b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/server/UndertowAutoConfiguration.java index 3c0c6d23..e44a6025 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/server/UndertowAutoConfiguration.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/server/UndertowAutoConfiguration.java @@ -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 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.");