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 90557d4e10..1c85894bf7 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,10 +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}. @@ -90,12 +88,9 @@ public final class HttpMethod implements Comparable, Serializable { public static final HttpMethod TRACE = new HttpMethod("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 HttpMethod[] values = new HttpMethod[] { GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE }; + + private static final Map mappings = Arrays.stream(values).collect(Collectors.toUnmodifiableMap(HttpMethod::name, Function.identity())); private final String name; @@ -126,7 +121,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;