Replace use of Collections.unmodifiable*() methods where appropriate
Closes gh-29321
This commit is contained in:
parent
86d45578d9
commit
ba136dcf40
|
@ -22,6 +22,7 @@ import java.util.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.cache.Cache;
|
import org.springframework.cache.Cache;
|
||||||
|
@ -64,13 +65,13 @@ public abstract class AbstractCacheManager implements CacheManager, Initializing
|
||||||
synchronized (this.cacheMap) {
|
synchronized (this.cacheMap) {
|
||||||
this.cacheNames = Collections.emptySet();
|
this.cacheNames = Collections.emptySet();
|
||||||
this.cacheMap.clear();
|
this.cacheMap.clear();
|
||||||
Set<String> cacheNames = new LinkedHashSet<>(caches.size());
|
|
||||||
for (Cache cache : caches) {
|
for (Cache cache : caches) {
|
||||||
String name = cache.getName();
|
String name = cache.getName();
|
||||||
this.cacheMap.put(name, decorateCache(cache));
|
this.cacheMap.put(name, decorateCache(cache));
|
||||||
cacheNames.add(name);
|
|
||||||
}
|
}
|
||||||
this.cacheNames = Collections.unmodifiableSet(cacheNames);
|
this.cacheNames = caches.stream()
|
||||||
|
.map(Cache::getName)
|
||||||
|
.collect(Collectors.toUnmodifiableSet());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,9 +19,7 @@ package org.springframework.format.datetime;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.EnumMap;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
@ -49,15 +47,10 @@ public class DateFormatter implements Formatter<Date> {
|
||||||
|
|
||||||
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
|
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
|
||||||
|
|
||||||
private static final Map<ISO, String> ISO_PATTERNS;
|
private static final Map<ISO, String> ISO_PATTERNS = Map.of(
|
||||||
|
ISO.DATE, "yyyy-MM-dd",
|
||||||
static {
|
ISO.TIME, "HH:mm:ss.SSSXXX",
|
||||||
Map<ISO, String> formats = new EnumMap<>(ISO.class);
|
ISO.DATE_TIME, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
|
||||||
formats.put(ISO.DATE, "yyyy-MM-dd");
|
|
||||||
formats.put(ISO.TIME, "HH:mm:ss.SSSXXX");
|
|
||||||
formats.put(ISO.DATE_TIME, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
|
|
||||||
ISO_PATTERNS = Collections.unmodifiableMap(formats);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.mock.web;
|
package org.springframework.mock.web;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
@ -61,11 +60,7 @@ class HeaderValueHolder {
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> getStringValues() {
|
List<String> getStringValues() {
|
||||||
List<String> stringList = new ArrayList<>(this.values.size());
|
return this.values.stream().map(Object::toString).toList();
|
||||||
for (Object value : this.values) {
|
|
||||||
stringList.add(value.toString());
|
|
||||||
}
|
|
||||||
return Collections.unmodifiableList(stringList);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|
|
@ -16,8 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.web.reactive.function.client;
|
package org.springframework.web.reactive.function.client;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
@ -88,7 +86,7 @@ final class DefaultExchangeStrategiesBuilder implements ExchangeStrategies.Build
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> List<T> unmodifiableCopy(List<? extends T> list) {
|
private static <T> List<T> unmodifiableCopy(List<? extends T> list) {
|
||||||
return Collections.unmodifiableList(new ArrayList<>(list));
|
return List.copyOf(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -17,7 +17,6 @@
|
||||||
package org.springframework.web.reactive.function.server;
|
package org.springframework.web.reactive.function.server;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
@ -135,7 +134,7 @@ class DefaultHandlerStrategiesBuilder implements HandlerStrategies.Builder {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> List<T> unmodifiableCopy(List<? extends T> list) {
|
private static <T> List<T> unmodifiableCopy(List<? extends T> list) {
|
||||||
return Collections.unmodifiableList(new ArrayList<>(list));
|
return List.copyOf(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -171,7 +171,7 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder
|
||||||
|
|
||||||
super(statusCode, headers, cookies, Collections.emptyMap());
|
super(statusCode, headers, cookies, Collections.emptyMap());
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.model = Collections.unmodifiableMap(new LinkedHashMap<>(model));
|
this.model = Map.copyOf(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -21,7 +21,6 @@ import java.net.URI;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
@ -88,7 +87,7 @@ class DefaultServerRequest implements ServerRequest {
|
||||||
|
|
||||||
DefaultServerRequest(ServerWebExchange exchange, List<HttpMessageReader<?>> messageReaders) {
|
DefaultServerRequest(ServerWebExchange exchange, List<HttpMessageReader<?>> messageReaders) {
|
||||||
this.exchange = exchange;
|
this.exchange = exchange;
|
||||||
this.messageReaders = Collections.unmodifiableList(new ArrayList<>(messageReaders));
|
this.messageReaders = List.copyOf(messageReaders);
|
||||||
this.headers = new DefaultHeaders();
|
this.headers = new DefaultHeaders();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1175,7 +1175,7 @@ public abstract class RouterFunctions {
|
||||||
return Collections.emptyMap();
|
return Collections.emptyMap();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return Collections.unmodifiableMap(new LinkedHashMap<>(attributes));
|
return Map.copyOf(attributes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -107,9 +107,8 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||||
public Map<T, HandlerMethod> getHandlerMethods() {
|
public Map<T, HandlerMethod> getHandlerMethods() {
|
||||||
this.mappingRegistry.acquireReadLock();
|
this.mappingRegistry.acquireReadLock();
|
||||||
try {
|
try {
|
||||||
return Collections.unmodifiableMap(
|
return this.mappingRegistry.getRegistrations().entrySet().stream()
|
||||||
this.mappingRegistry.getRegistrations().entrySet().stream()
|
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, entry -> entry.getValue().handlerMethod));
|
||||||
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().handlerMethod)));
|
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
this.mappingRegistry.releaseReadLock();
|
this.mappingRegistry.releaseReadLock();
|
||||||
|
|
|
@ -19,9 +19,9 @@ package org.springframework.web.socket.server.support;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
@ -95,9 +95,8 @@ public class OriginHandshakeInterceptor implements HandshakeInterceptor {
|
||||||
* @since 4.1.5
|
* @since 4.1.5
|
||||||
*/
|
*/
|
||||||
public Collection<String> getAllowedOrigins() {
|
public Collection<String> getAllowedOrigins() {
|
||||||
List<String> allowedOrigins = this.corsConfiguration.getAllowedOrigins();
|
return (CollectionUtils.isEmpty(this.corsConfiguration.getAllowedOrigins()) ? Collections.emptySet() :
|
||||||
return (CollectionUtils.isEmpty(allowedOrigins) ? Collections.emptySet() :
|
Set.copyOf(this.corsConfiguration.getAllowedOrigins()));
|
||||||
Collections.unmodifiableSet(new LinkedHashSet<>(allowedOrigins)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -119,9 +118,8 @@ public class OriginHandshakeInterceptor implements HandshakeInterceptor {
|
||||||
* @since 5.3.2
|
* @since 5.3.2
|
||||||
*/
|
*/
|
||||||
public Collection<String> getAllowedOriginPatterns() {
|
public Collection<String> getAllowedOriginPatterns() {
|
||||||
List<String> allowedOriginPatterns = this.corsConfiguration.getAllowedOriginPatterns();
|
return (CollectionUtils.isEmpty(this.corsConfiguration.getAllowedOriginPatterns()) ? Collections.emptySet() :
|
||||||
return (CollectionUtils.isEmpty(allowedOriginPatterns) ? Collections.emptySet() :
|
Set.copyOf(this.corsConfiguration.getAllowedOriginPatterns()));
|
||||||
Collections.unmodifiableSet(new LinkedHashSet<>(allowedOriginPatterns)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -17,10 +17,9 @@
|
||||||
package org.springframework.web.socket.sockjs.transport;
|
package org.springframework.web.socket.sockjs.transport;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
@ -47,15 +46,8 @@ public enum TransportType {
|
||||||
HTML_FILE("htmlfile", HttpMethod.GET, "cors", "jsessionid", "no_cache");
|
HTML_FILE("htmlfile", HttpMethod.GET, "cors", "jsessionid", "no_cache");
|
||||||
|
|
||||||
|
|
||||||
private static final Map<String, TransportType> TRANSPORT_TYPES;
|
private static final Map<String, TransportType> TRANSPORT_TYPES =
|
||||||
|
Arrays.stream(values()).collect(Collectors.toUnmodifiableMap(type -> type.value, type -> type));
|
||||||
static {
|
|
||||||
Map<String, TransportType> transportTypes = new HashMap<>();
|
|
||||||
for (TransportType type : values()) {
|
|
||||||
transportTypes.put(type.value, type);
|
|
||||||
}
|
|
||||||
TRANSPORT_TYPES = Collections.unmodifiableMap(transportTypes);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static TransportType fromValue(String value) {
|
public static TransportType fromValue(String value) {
|
||||||
|
|
Loading…
Reference in New Issue