diff --git a/spring-context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java b/spring-context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java index e6b162f642..c7f7ef5da0 100644 --- a/spring-context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java +++ b/spring-context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java @@ -22,7 +22,6 @@ import java.util.LinkedHashSet; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -import java.util.stream.Collectors; import org.springframework.beans.factory.InitializingBean; import org.springframework.cache.Cache; @@ -65,13 +64,13 @@ public abstract class AbstractCacheManager implements CacheManager, Initializing synchronized (this.cacheMap) { this.cacheNames = Collections.emptySet(); this.cacheMap.clear(); + Set cacheNames = new LinkedHashSet<>(caches.size()); for (Cache cache : caches) { String name = cache.getName(); this.cacheMap.put(name, decorateCache(cache)); + cacheNames.add(name); } - this.cacheNames = caches.stream() - .map(Cache::getName) - .collect(Collectors.toUnmodifiableSet()); + this.cacheNames = Collections.unmodifiableSet(cacheNames); } } diff --git a/spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java b/spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java index 0125c9d705..84bd7f9161 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-test/src/main/java/org/springframework/mock/web/HeaderValueHolder.java b/spring-test/src/main/java/org/springframework/mock/web/HeaderValueHolder.java index 6f9a57ab0a..a0a739765e 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/HeaderValueHolder.java +++ b/spring-test/src/main/java/org/springframework/mock/web/HeaderValueHolder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/HeaderValueHolder.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/HeaderValueHolder.java index 9884d2a746..c1659d97cd 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/HeaderValueHolder.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/HeaderValueHolder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ package org.springframework.web.testfixture.servlet; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.LinkedList; @@ -61,11 +60,7 @@ class HeaderValueHolder { } List getStringValues() { - List stringList = new ArrayList<>(this.values.size()); - for (Object value : this.values) { - stringList.add(value.toString()); - } - return Collections.unmodifiableList(stringList); + return this.values.stream().map(Object::toString).toList(); } @Nullable diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultExchangeStrategiesBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultExchangeStrategiesBuilder.java index c05763c547..697cf5a0af 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultExchangeStrategiesBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultExchangeStrategiesBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -81,12 +81,8 @@ final class DefaultExchangeStrategiesBuilder implements ExchangeStrategies.Build public DefaultExchangeStrategies(ClientCodecConfigurer codecConfigurer) { this.codecConfigurer = codecConfigurer; - this.readers = unmodifiableCopy(this.codecConfigurer.getReaders()); - this.writers = unmodifiableCopy(this.codecConfigurer.getWriters()); - } - - private static List unmodifiableCopy(List list) { - return List.copyOf(list); + this.readers = List.copyOf(this.codecConfigurer.getReaders()); + this.writers = List.copyOf(this.codecConfigurer.getWriters()); } @Override diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultHandlerStrategiesBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultHandlerStrategiesBuilder.java index 9e000cb26b..f5782e054a 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultHandlerStrategiesBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultHandlerStrategiesBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -125,18 +125,14 @@ class DefaultHandlerStrategiesBuilder implements HandlerStrategies.Builder { List exceptionHandlers, LocaleContextResolver localeContextResolver) { - this.messageReaders = unmodifiableCopy(messageReaders); - this.messageWriters = unmodifiableCopy(messageWriters); - this.viewResolvers = unmodifiableCopy(viewResolvers); - this.webFilters = unmodifiableCopy(webFilters); - this.exceptionHandlers = unmodifiableCopy(exceptionHandlers); + this.messageReaders = List.copyOf(messageReaders); + this.messageWriters = List.copyOf(messageWriters); + this.viewResolvers = List.copyOf(viewResolvers); + this.webFilters = List.copyOf(webFilters); + this.exceptionHandlers = List.copyOf(exceptionHandlers); this.localeContextResolver = localeContextResolver; } - private static List unmodifiableCopy(List list) { - return List.copyOf(list); - } - @Override public List> messageReaders() { return this.messageReaders; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java index d3086b19ca..3dea118d31 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java @@ -171,7 +171,7 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder super(statusCode, headers, cookies, Collections.emptyMap()); this.name = name; - this.model = Map.copyOf(model); + this.model = Collections.unmodifiableMap(new LinkedHashMap<>(model)); } @Override diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java index 4c7fee9cca..2cc9491867 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java index dd7a473d51..f55838488e 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java @@ -1175,7 +1175,7 @@ public abstract class RouterFunctions { return Collections.emptyMap(); } else { - return Map.copyOf(attributes); + return Collections.unmodifiableMap(new LinkedHashMap<>(attributes)); } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequest.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequest.java index 96e0a94e71..a2223c29ea 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequest.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequest.java @@ -95,7 +95,7 @@ class DefaultServerRequest implements ServerRequest { public DefaultServerRequest(HttpServletRequest servletRequest, List> messageConverters) { this.serverHttpRequest = new ServletServerHttpRequest(servletRequest); - this.messageConverters = Collections.unmodifiableList(new ArrayList<>(messageConverters)); + this.messageConverters = List.copyOf(messageConverters); this.headers = new DefaultRequestHeaders(this.serverHttpRequest.getHeaders()); this.params = CollectionUtils.toMultiValueMap(new ServletParametersMap(servletRequest)); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java index 0dc6875f01..85d61f2ce6 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java @@ -147,9 +147,8 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap public Map getHandlerMethods() { this.mappingRegistry.acquireReadLock(); try { - return Collections.unmodifiableMap( - this.mappingRegistry.getRegistrations().entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().handlerMethod))); + return this.mappingRegistry.getRegistrations().entrySet().stream() + .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, entry -> entry.getValue().handlerMethod)); } finally { this.mappingRegistry.releaseReadLock(); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/OriginHandshakeInterceptor.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/OriginHandshakeInterceptor.java index a23c16ad8b..5e3cc4d8dd 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/OriginHandshakeInterceptor.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/OriginHandshakeInterceptor.java @@ -19,9 +19,9 @@ package org.springframework.web.socket.server.support; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; -import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -95,8 +95,9 @@ public class OriginHandshakeInterceptor implements HandshakeInterceptor { * @since 4.1.5 */ public Collection getAllowedOrigins() { - return (CollectionUtils.isEmpty(this.corsConfiguration.getAllowedOrigins()) ? Collections.emptySet() : - Set.copyOf(this.corsConfiguration.getAllowedOrigins())); + List allowedOrigins = this.corsConfiguration.getAllowedOrigins(); + return (CollectionUtils.isEmpty(allowedOrigins) ? Collections.emptySet() : + Collections.unmodifiableSet(new LinkedHashSet<>(allowedOrigins))); } /** @@ -118,8 +119,9 @@ public class OriginHandshakeInterceptor implements HandshakeInterceptor { * @since 5.3.2 */ public Collection getAllowedOriginPatterns() { - return (CollectionUtils.isEmpty(this.corsConfiguration.getAllowedOriginPatterns()) ? Collections.emptySet() : - Set.copyOf(this.corsConfiguration.getAllowedOriginPatterns())); + List allowedOriginPatterns = this.corsConfiguration.getAllowedOriginPatterns(); + return (CollectionUtils.isEmpty(allowedOriginPatterns) ? Collections.emptySet() : + Collections.unmodifiableSet(new LinkedHashSet<>(allowedOriginPatterns))); } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportType.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportType.java index f321b21171..872e74cb80 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportType.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportType.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,6 +49,7 @@ public enum TransportType { private static final Map TRANSPORT_TYPES = Arrays.stream(values()).collect(Collectors.toUnmodifiableMap(type -> type.value, type -> type)); + @Nullable public static TransportType fromValue(String value) { return TRANSPORT_TYPES.get(value);