diff --git a/spring-web/src/main/java/org/springframework/http/HttpMethod.java b/spring-web/src/main/java/org/springframework/http/HttpMethod.java index 90557d4e108..327d3796789 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpMethod.java +++ b/spring-web/src/main/java/org/springframework/http/HttpMethod.java @@ -17,8 +17,10 @@ package org.springframework.http; import java.io.Serializable; -import java.util.HashMap; +import java.util.Arrays; import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -36,11 +38,6 @@ public final class HttpMethod implements Comparable, Serializable { private static final long serialVersionUID = -70133475680645360L; - private static final HttpMethod[] values; - - private static final Map mappings = new HashMap<>(16); - - /** * The HTTP method {@code GET}. * @see HTTP 1.1, section 9.3 @@ -89,13 +86,10 @@ public final class HttpMethod implements Comparable, Serializable { */ public static final HttpMethod TRACE = new HttpMethod("TRACE"); + private static final HttpMethod[] values = new HttpMethod[] { GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE }; - static { - values = new HttpMethod[]{GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE}; - for (HttpMethod httpMethod : values) { - mappings.put(httpMethod.name(), httpMethod); - } - } + private static final Map mappings = Arrays.stream(values) + .collect(Collectors.toUnmodifiableMap(HttpMethod::name, Function.identity())); private final String name; @@ -126,7 +120,7 @@ public final class HttpMethod implements Comparable, Serializable { * @return the corresponding {@code HttpMethod} */ public static HttpMethod valueOf(String method) { - Assert.notNull(method, "Name must not be null"); + Assert.notNull(method, "Method must not be null"); HttpMethod result = mappings.get(method); if (result != null) { return result;