Revise contribution

This commit reverts changes to AbstractCacheManager since iterating
over the caches in a for-loop and a stream is duplicated effort.

This commit reverts changes to DefaultRenderingResponseBuilder,
RouterFunctions, and OriginHandshakeInterceptor since order matters for
those use cases: they were originally based on the semantics of
LinkedHashSet or LinkedHashMap; whereas, Set.copyOf() and Map.copyOf()
do not provide any guarantees regarding ordering.

This commit also applies analogous changes to "sibling" implementations
across Servlet mocks as well as Web MVC and WebFlux.

See gh-29321
This commit is contained in:
Sam Brannen 2022-11-08 13:50:45 +01:00
parent ba136dcf40
commit 95f3337bb5
13 changed files with 31 additions and 43 deletions

View File

@ -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<String> 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);
}
}

View File

@ -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.

View File

@ -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.

View File

@ -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<String> getStringValues() {
List<String> 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

View File

@ -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 <T> List<T> unmodifiableCopy(List<? extends T> list) {
return List.copyOf(list);
this.readers = List.copyOf(this.codecConfigurer.getReaders());
this.writers = List.copyOf(this.codecConfigurer.getWriters());
}
@Override

View File

@ -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<WebExceptionHandler> 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 <T> List<T> unmodifiableCopy(List<? extends T> list) {
return List.copyOf(list);
}
@Override
public List<HttpMessageReader<?>> messageReaders() {
return this.messageReaders;

View File

@ -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

View File

@ -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.

View File

@ -1175,7 +1175,7 @@ public abstract class RouterFunctions {
return Collections.emptyMap();
}
else {
return Map.copyOf(attributes);
return Collections.unmodifiableMap(new LinkedHashMap<>(attributes));
}
}

View File

@ -95,7 +95,7 @@ class DefaultServerRequest implements ServerRequest {
public DefaultServerRequest(HttpServletRequest servletRequest, List<HttpMessageConverter<?>> 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));

View File

@ -147,9 +147,8 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
public Map<T, HandlerMethod> 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();

View File

@ -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<String> getAllowedOrigins() {
return (CollectionUtils.isEmpty(this.corsConfiguration.getAllowedOrigins()) ? Collections.emptySet() :
Set.copyOf(this.corsConfiguration.getAllowedOrigins()));
List<String> 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<String> getAllowedOriginPatterns() {
return (CollectionUtils.isEmpty(this.corsConfiguration.getAllowedOriginPatterns()) ? Collections.emptySet() :
Set.copyOf(this.corsConfiguration.getAllowedOriginPatterns()));
List<String> allowedOriginPatterns = this.corsConfiguration.getAllowedOriginPatterns();
return (CollectionUtils.isEmpty(allowedOriginPatterns) ? Collections.emptySet() :
Collections.unmodifiableSet(new LinkedHashSet<>(allowedOriginPatterns)));
}

View File

@ -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<String, TransportType> 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);