1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.codec.http;
17
18 import io.netty5.util.AsciiString;
19
20 import static io.netty5.util.internal.MathUtil.findNextPositivePowerOfTwo;
21 import static io.netty5.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
22
23
24
25
26
27
28 public class HttpMethod implements Comparable<HttpMethod> {
29
30
31
32
33
34
35
36 public static final HttpMethod OPTIONS = new HttpMethod("OPTIONS");
37
38
39
40
41
42
43
44 public static final HttpMethod GET = new HttpMethod("GET");
45
46
47
48
49
50 public static final HttpMethod HEAD = new HttpMethod("HEAD");
51
52
53
54
55
56
57 public static final HttpMethod POST = new HttpMethod("POST");
58
59
60
61
62 public static final HttpMethod PUT = new HttpMethod("PUT");
63
64
65
66
67
68 public static final HttpMethod PATCH = new HttpMethod("PATCH");
69
70
71
72
73
74 public static final HttpMethod DELETE = new HttpMethod("DELETE");
75
76
77
78
79
80 public static final HttpMethod TRACE = new HttpMethod("TRACE");
81
82
83
84
85
86 public static final HttpMethod CONNECT = new HttpMethod("CONNECT");
87
88 private static final EnumNameMap<HttpMethod> methodMap;
89
90 static {
91 methodMap = new EnumNameMap<>(
92 new EnumNameMap.Node<>(OPTIONS.toString(), OPTIONS),
93 new EnumNameMap.Node<>(GET.toString(), GET),
94 new EnumNameMap.Node<>(HEAD.toString(), HEAD),
95 new EnumNameMap.Node<>(POST.toString(), POST),
96 new EnumNameMap.Node<>(PUT.toString(), PUT),
97 new EnumNameMap.Node<>(PATCH.toString(), PATCH),
98 new EnumNameMap.Node<>(DELETE.toString(), DELETE),
99 new EnumNameMap.Node<>(TRACE.toString(), TRACE),
100 new EnumNameMap.Node<>(CONNECT.toString(), CONNECT));
101 }
102
103
104
105
106
107
108 public static HttpMethod valueOf(String name) {
109 HttpMethod result = methodMap.get(name);
110 return result != null ? result : new HttpMethod(name);
111 }
112
113 private final AsciiString name;
114
115
116
117
118
119
120
121
122 public HttpMethod(String name) {
123 name = checkNonEmptyAfterTrim(name, "name");
124
125 for (int i = 0; i < name.length(); i ++) {
126 char c = name.charAt(i);
127 if (Character.isISOControl(c) || Character.isWhitespace(c)) {
128 throw new IllegalArgumentException("invalid character in name");
129 }
130 }
131
132 this.name = AsciiString.cached(name);
133 }
134
135
136
137
138 public String name() {
139 return name.toString();
140 }
141
142
143
144
145 public AsciiString asciiName() {
146 return name;
147 }
148
149 @Override
150 public int hashCode() {
151 return name().hashCode();
152 }
153
154 @Override
155 public boolean equals(Object o) {
156 if (this == o) {
157 return true;
158 }
159 if (!(o instanceof HttpMethod)) {
160 return false;
161 }
162
163 HttpMethod that = (HttpMethod) o;
164 return name().equals(that.name());
165 }
166
167 @Override
168 public String toString() {
169 return name.toString();
170 }
171
172 @Override
173 public int compareTo(HttpMethod o) {
174 if (o == this) {
175 return 0;
176 }
177 return name().compareTo(o.name());
178 }
179
180 private static final class EnumNameMap<T> {
181 private final EnumNameMap.Node<T>[] values;
182 private final int valuesMask;
183
184 EnumNameMap(EnumNameMap.Node<T>... nodes) {
185 values = (EnumNameMap.Node<T>[]) new EnumNameMap.Node[findNextPositivePowerOfTwo(nodes.length)];
186 valuesMask = values.length - 1;
187 for (EnumNameMap.Node<T> node : nodes) {
188 int i = hashCode(node.key) & valuesMask;
189 if (values[i] != null) {
190 throw new IllegalArgumentException("index " + i + " collision between values: [" +
191 values[i].key + ", " + node.key + ']');
192 }
193 values[i] = node;
194 }
195 }
196
197 T get(String name) {
198 EnumNameMap.Node<T> node = values[hashCode(name) & valuesMask];
199 return node == null || !node.key.equals(name) ? null : node.value;
200 }
201
202 private static int hashCode(String name) {
203
204
205
206
207
208 return name.hashCode() >>> 6;
209 }
210
211 private static final class Node<T> {
212 final String key;
213 final T value;
214
215 Node(String key, T value) {
216 this.key = key;
217 this.value = value;
218 }
219 }
220 }
221 }