mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-10 08:57:19 +08:00
refactor: 使用 CollUtils 替代部分 Stream 集合转换
This commit is contained in:
@@ -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.BaseEnumParameterHandler;
|
||||||
import top.continew.starter.apidoc.handler.OpenApiHandler;
|
import top.continew.starter.apidoc.handler.OpenApiHandler;
|
||||||
import top.continew.starter.core.autoconfigure.application.ApplicationProperties;
|
import top.continew.starter.core.autoconfigure.application.ApplicationProperties;
|
||||||
|
import top.continew.starter.core.util.CollUtils;
|
||||||
import top.continew.starter.core.util.GeneralPropertySourceFactory;
|
import top.continew.starter.core.util.GeneralPropertySourceFactory;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -104,7 +105,7 @@ public class SpringDocAutoConfiguration implements WebMvcConfigurer {
|
|||||||
Map<String, SecurityScheme> securitySchemeMap = components.getSecuritySchemes();
|
Map<String, SecurityScheme> securitySchemeMap = components.getSecuritySchemes();
|
||||||
if (MapUtil.isNotEmpty(securitySchemeMap)) {
|
if (MapUtil.isNotEmpty(securitySchemeMap)) {
|
||||||
SecurityRequirement securityRequirement = new SecurityRequirement();
|
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);
|
list.forEach(securityRequirement::addList);
|
||||||
openApi.addSecurityItem(securityRequirement);
|
openApi.addSecurityItem(securityRequirement);
|
||||||
}
|
}
|
||||||
@@ -127,10 +128,8 @@ public class SpringDocAutoConfiguration implements WebMvcConfigurer {
|
|||||||
Map<String, SecurityScheme> securitySchemeMap = components.getSecuritySchemes();
|
Map<String, SecurityScheme> securitySchemeMap = components.getSecuritySchemes();
|
||||||
pathItem.readOperations().forEach(operation -> {
|
pathItem.readOperations().forEach(operation -> {
|
||||||
SecurityRequirement securityRequirement = new SecurityRequirement();
|
SecurityRequirement securityRequirement = new SecurityRequirement();
|
||||||
List<String> list = securitySchemeMap.values()
|
List<String> list = CollUtils.mapToList(securitySchemeMap
|
||||||
.stream()
|
.values(), SecurityScheme::getName);
|
||||||
.map(SecurityScheme::getName)
|
|
||||||
.toList();
|
|
||||||
list.forEach(securityRequirement::addList);
|
list.forEach(securityRequirement::addList);
|
||||||
operation.addSecurityItem(securityRequirement);
|
operation.addSecurityItem(securityRequirement);
|
||||||
});
|
});
|
||||||
|
@@ -37,11 +37,11 @@ import org.springdoc.core.utils.PropertyResolverUtils;
|
|||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||||
import org.springframework.web.method.HandlerMethod;
|
import org.springframework.web.method.HandlerMethod;
|
||||||
|
import top.continew.starter.core.util.CollUtils;
|
||||||
|
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@@ -176,11 +176,8 @@ public class OpenApiHandler extends OpenAPIService {
|
|||||||
buildTagsFromClass(handlerMethod.getBeanType(), tags, tagsStr, locale);
|
buildTagsFromClass(handlerMethod.getBeanType(), tags, tagsStr, locale);
|
||||||
|
|
||||||
if (CollUtil.isNotEmpty(tagsStr)) {
|
if (CollUtil.isNotEmpty(tagsStr)) {
|
||||||
tagsStr = tagsStr.stream()
|
tagsStr = CollUtils.mapToSet(tagsStr, str -> propertyResolverUtils.resolve(str, locale));
|
||||||
.map(str -> propertyResolverUtils.resolve(str, locale))
|
|
||||||
.collect(Collectors.toSet());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (springdocTags.containsKey(handlerMethod)) {
|
if (springdocTags.containsKey(handlerMethod)) {
|
||||||
Tag tag = springdocTags.get(handlerMethod);
|
Tag tag = springdocTags.get(handlerMethod);
|
||||||
tagsStr.add(tag.getName());
|
tagsStr.add(tag.getName());
|
||||||
@@ -256,7 +253,7 @@ public class OpenApiHandler extends OpenAPIService {
|
|||||||
methodTags.addAll(AnnotatedElementUtils
|
methodTags.addAll(AnnotatedElementUtils
|
||||||
.findAllMergedAnnotations(method, io.swagger.v3.oas.annotations.tags.Tag.class));
|
.findAllMergedAnnotations(method, io.swagger.v3.oas.annotations.tags.Tag.class));
|
||||||
if (CollUtil.isNotEmpty(methodTags)) {
|
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);
|
List<io.swagger.v3.oas.annotations.tags.Tag> allTags = new ArrayList<>(methodTags);
|
||||||
addTags(allTags, tags, locale);
|
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -45,7 +45,7 @@ public class ReflectUtils {
|
|||||||
*/
|
*/
|
||||||
public static List<String> getNonStaticFieldsName(Class<?> beanClass) throws SecurityException {
|
public static List<String> getNonStaticFieldsName(Class<?> beanClass) throws SecurityException {
|
||||||
List<Field> nonStaticFields = getNonStaticFields(beanClass);
|
List<Field> nonStaticFields = getNonStaticFields(beanClass);
|
||||||
return nonStaticFields.stream().map(Field::getName).collect(Collectors.toList());
|
return CollUtils.mapToList(nonStaticFields, Field::getName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -23,6 +23,7 @@ import cn.hutool.core.io.IoUtil;
|
|||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import top.continew.starter.core.util.CollUtils;
|
||||||
import top.continew.starter.license.exception.LicenseException;
|
import top.continew.starter.license.exception.LicenseException;
|
||||||
import top.continew.starter.license.model.LicenseExtraModel;
|
import top.continew.starter.license.model.LicenseExtraModel;
|
||||||
|
|
||||||
@@ -33,7 +34,6 @@ import java.net.SocketException;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务器信息工具类
|
* 服务器信息工具类
|
||||||
@@ -264,10 +264,7 @@ public class ServerInfoUtils {
|
|||||||
// 获取所有网络接口
|
// 获取所有网络接口
|
||||||
Set<InetAddress> inetAddresses = getLocalAllInetAddress();
|
Set<InetAddress> inetAddresses = getLocalAllInetAddress();
|
||||||
if (CollectionUtil.isNotEmpty(inetAddresses)) {
|
if (CollectionUtil.isNotEmpty(inetAddresses)) {
|
||||||
return inetAddresses.stream()
|
return CollUtils.mapToSet(inetAddresses, ServerInfoUtils::getMacByInetAddress);
|
||||||
.map(ServerInfoUtils::getMacByInetAddress)
|
|
||||||
.distinct()
|
|
||||||
.collect(Collectors.toSet());
|
|
||||||
}
|
}
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
@@ -282,11 +279,7 @@ public class ServerInfoUtils {
|
|||||||
// 获取所有网络接口
|
// 获取所有网络接口
|
||||||
Set<InetAddress> inetAddresses = getLocalAllInetAddress();
|
Set<InetAddress> inetAddresses = getLocalAllInetAddress();
|
||||||
if (CollectionUtil.isNotEmpty(inetAddresses)) {
|
if (CollectionUtil.isNotEmpty(inetAddresses)) {
|
||||||
return inetAddresses.stream()
|
return CollUtils.mapToSet(inetAddresses, InetAddress::getHostAddress);
|
||||||
.map(InetAddress::getHostAddress)
|
|
||||||
.distinct()
|
|
||||||
.map(String::toLowerCase)
|
|
||||||
.collect(Collectors.toSet());
|
|
||||||
}
|
}
|
||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
|
@@ -31,8 +31,7 @@ import io.undertow.Undertow;
|
|||||||
import io.undertow.server.handlers.DisallowedMethodsHandler;
|
import io.undertow.server.handlers.DisallowedMethodsHandler;
|
||||||
import io.undertow.util.HttpString;
|
import io.undertow.util.HttpString;
|
||||||
import top.continew.starter.core.constant.PropertiesConstants;
|
import top.continew.starter.core.constant.PropertiesConstants;
|
||||||
|
import top.continew.starter.core.util.CollUtils;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Undertow 自动配置
|
* Undertow 自动配置
|
||||||
@@ -57,11 +56,8 @@ public class UndertowAutoConfiguration {
|
|||||||
public WebServerFactoryCustomizer<UndertowServletWebServerFactory> customize(ServerExtensionProperties properties) {
|
public WebServerFactoryCustomizer<UndertowServletWebServerFactory> customize(ServerExtensionProperties properties) {
|
||||||
return factory -> {
|
return factory -> {
|
||||||
factory.addDeploymentInfoCustomizers(deploymentInfo -> deploymentInfo
|
factory.addDeploymentInfoCustomizers(deploymentInfo -> deploymentInfo
|
||||||
.addInitialHandlerChainWrapper(handler -> new DisallowedMethodsHandler(handler, properties
|
.addInitialHandlerChainWrapper(handler -> new DisallowedMethodsHandler(handler, CollUtils
|
||||||
.getDisallowedMethods()
|
.mapToSet(properties.getDisallowedMethods(), HttpString::tryFromString))));
|
||||||
.stream()
|
|
||||||
.map(HttpString::tryFromString)
|
|
||||||
.collect(Collectors.toSet()))));
|
|
||||||
log.debug("[ContiNew Starter] - Disallowed HTTP methods on Server Undertow: {}.", properties
|
log.debug("[ContiNew Starter] - Disallowed HTTP methods on Server Undertow: {}.", properties
|
||||||
.getDisallowedMethods());
|
.getDisallowedMethods());
|
||||||
log.debug("[ContiNew Starter] - Auto Configuration 'Web-Server Undertow' completed initialization.");
|
log.debug("[ContiNew Starter] - Auto Configuration 'Web-Server Undertow' completed initialization.");
|
||||||
|
Reference in New Issue
Block a user