Use Collection factory methods when applicable

This commit replaces the use of Collections.unmodifiableList/Set/Map
with the corresponding 'of(...)' factory methods introduced in Java 9.

Closes gh-27824
This commit is contained in:
Marten Deinum
2021-12-16 07:50:41 +01:00
committed by Sam Brannen
parent ab240f5d8e
commit 941b6af9ac
14 changed files with 68 additions and 137 deletions

View File

@@ -19,7 +19,6 @@ package org.springframework.http.codec.json;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -75,11 +74,10 @@ public abstract class Jackson2CodecSupport {
private static final String JSON_VIEW_HINT_ERROR =
"@JsonView only supported for write hints with exactly 1 class argument: ";
private static final List<MimeType> DEFAULT_MIME_TYPES = Collections.unmodifiableList(
Arrays.asList(
MediaType.APPLICATION_JSON,
new MediaType("application", "*+json"),
MediaType.APPLICATION_NDJSON));
private static final List<MimeType> DEFAULT_MIME_TYPES = List.of(
MediaType.APPLICATION_JSON,
new MediaType("application", "*+json"),
MediaType.APPLICATION_NDJSON);
protected final Log logger = HttpLogging.forLogName(getClass());
@@ -99,7 +97,7 @@ public abstract class Jackson2CodecSupport {
Assert.notNull(objectMapper, "ObjectMapper must not be null");
this.defaultObjectMapper = objectMapper;
this.mimeTypes = !ObjectUtils.isEmpty(mimeTypes) ?
Collections.unmodifiableList(Arrays.asList(mimeTypes)) : DEFAULT_MIME_TYPES;
List.of(mimeTypes) : DEFAULT_MIME_TYPES;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@@ -17,9 +17,7 @@
package org.springframework.http.codec.multipart;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -56,9 +54,10 @@ public class MultipartHttpMessageReader extends LoggingCodecSupport
private static final ResolvableType MULTIPART_VALUE_TYPE = ResolvableType.forClassWithGenerics(
MultiValueMap.class, String.class, Part.class);
static final List<MediaType> MIME_TYPES = Collections.unmodifiableList(Arrays.asList(
MediaType.MULTIPART_FORM_DATA, MediaType.MULTIPART_MIXED, MediaType.MULTIPART_RELATED));
static final List<MediaType> MIME_TYPES = List.of(
MediaType.MULTIPART_FORM_DATA,
MediaType.MULTIPART_MIXED,
MediaType.MULTIPART_RELATED);
private final HttpMessageReader<Part> partReader;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 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,8 +16,6 @@
package org.springframework.http.codec.protobuf;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.springframework.lang.Nullable;
@@ -31,11 +29,10 @@ import org.springframework.util.MimeType;
*/
public abstract class ProtobufCodecSupport {
static final List<MimeType> MIME_TYPES = Collections.unmodifiableList(
Arrays.asList(
static final List<MimeType> MIME_TYPES = List.of(
new MimeType("application", "x-protobuf"),
new MimeType("application", "octet-stream"),
new MimeType("application", "vnd.google.protobuf")));
new MimeType("application", "vnd.google.protobuf"));
static final String DELIMITED_KEY = "delimited";

View File

@@ -18,7 +18,6 @@ package org.springframework.web.cors;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
@@ -65,11 +64,10 @@ public class CorsConfiguration {
private static final List<String> DEFAULT_PERMIT_ALL = Collections.singletonList(ALL);
private static final List<HttpMethod> DEFAULT_METHODS = Collections.unmodifiableList(
Arrays.asList(HttpMethod.GET, HttpMethod.HEAD));
private static final List<HttpMethod> DEFAULT_METHODS = List.of(HttpMethod.GET, HttpMethod.HEAD);
private static final List<String> DEFAULT_PERMIT_METHODS = Collections.unmodifiableList(
Arrays.asList(HttpMethod.GET.name(), HttpMethod.HEAD.name(), HttpMethod.POST.name()));
private static final List<String> DEFAULT_PERMIT_METHODS = List.of(HttpMethod.GET.name(),
HttpMethod.HEAD.name(), HttpMethod.POST.name());
@Nullable

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 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.
@@ -17,8 +17,6 @@
package org.springframework.web.filter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
@@ -57,8 +55,7 @@ import org.springframework.web.util.WebUtils;
public class HiddenHttpMethodFilter extends OncePerRequestFilter {
private static final List<String> ALLOWED_METHODS =
Collections.unmodifiableList(Arrays.asList(HttpMethod.PUT.name(),
HttpMethod.DELETE.name(), HttpMethod.PATCH.name()));
List.of(HttpMethod.PUT.name(), HttpMethod.DELETE.name(), HttpMethod.PATCH.name());
/** Default method parameter: {@code _method}. */
public static final String DEFAULT_METHOD_PARAM = "_method";

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 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,8 +16,6 @@
package org.springframework.web.filter.reactive;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
@@ -49,8 +47,7 @@ import org.springframework.web.server.WebFilterChain;
public class HiddenHttpMethodFilter implements WebFilter {
private static final List<HttpMethod> ALLOWED_METHODS =
Collections.unmodifiableList(Arrays.asList(HttpMethod.PUT,
HttpMethod.DELETE, HttpMethod.PATCH));
List.of(HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.PATCH);
/** Default name of the form parameter with the HTTP method to use. */
public static final String DEFAULT_METHOD_PARAMETER_NAME = "_method";

View File

@@ -22,7 +22,6 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;
@@ -898,7 +897,7 @@ final class HierarchicalUriComponents extends UriComponents {
@Override
public List<String> getPathSegments() {
String[] segments = StringUtils.tokenizeToStringArray(getPath(), PATH_DELIMITER_STRING);
return Collections.unmodifiableList(Arrays.asList(segments));
return List.of(segments);
}
@Override