View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at:
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package io.netty.handler.codec;
16  
17  import io.netty.util.internal.PlatformDependent;
18  
19  import java.text.ParseException;
20  
21  import static io.netty.handler.codec.AsciiString.*;
22  
23  public class DefaultBinaryHeaders extends DefaultHeaders<AsciiString> implements BinaryHeaders {
24      private static final HashCodeGenerator<AsciiString> ASCII_HASH_CODE_GENERATOR =
25              new HashCodeGenerator<AsciiString>() {
26          @Override
27          public int generateHashCode(AsciiString name) {
28              return AsciiString.caseInsensitiveHashCode(name);
29          }
30      };
31  
32      private static final ValueConverter<AsciiString> OBJECT_TO_ASCII = new ValueConverter<AsciiString>() {
33          @Override
34          public AsciiString convertObject(Object value) {
35              if (value instanceof AsciiString) {
36                  return (AsciiString) value;
37              }
38              if (value instanceof CharSequence) {
39                  return new AsciiString((CharSequence) value);
40              }
41              return new AsciiString(value.toString());
42          }
43  
44          @Override
45          public AsciiString convertInt(int value) {
46              return new AsciiString(String.valueOf(value));
47          }
48  
49          @Override
50          public AsciiString convertLong(long value) {
51              return new AsciiString(String.valueOf(value));
52          }
53  
54          @Override
55          public AsciiString convertDouble(double value) {
56              return new AsciiString(String.valueOf(value));
57          }
58  
59          @Override
60          public AsciiString convertChar(char value) {
61              return new AsciiString(String.valueOf(value));
62          }
63  
64          @Override
65          public AsciiString convertBoolean(boolean value) {
66              return new AsciiString(String.valueOf(value));
67          }
68  
69          @Override
70          public AsciiString convertFloat(float value) {
71              return new AsciiString(String.valueOf(value));
72          }
73  
74          @Override
75          public int convertToInt(AsciiString value) {
76              return value.parseInt();
77          }
78  
79          @Override
80          public long convertToLong(AsciiString value) {
81              return value.parseLong();
82          }
83  
84          @Override
85          public AsciiString convertTimeMillis(long value) {
86              return new AsciiString(String.valueOf(value));
87          }
88  
89          @Override
90          public long convertToTimeMillis(AsciiString value) {
91              try {
92                  return HeaderDateFormat.get().parse(value.toString());
93              } catch (ParseException e) {
94                  PlatformDependent.throwException(e);
95              }
96              return 0;
97          }
98  
99          @Override
100         public double convertToDouble(AsciiString value) {
101             return value.parseDouble();
102         }
103 
104         @Override
105         public char convertToChar(AsciiString value) {
106             return value.charAt(0);
107         }
108 
109         @Override
110         public boolean convertToBoolean(AsciiString value) {
111             return value.byteAt(0) != 0;
112         }
113 
114         @Override
115         public float convertToFloat(AsciiString value) {
116             return value.parseFloat();
117         }
118 
119         @Override
120         public AsciiString convertShort(short value) {
121             return new AsciiString(String.valueOf(value));
122         }
123 
124         @Override
125         public short convertToShort(AsciiString value) {
126             return value.parseShort();
127         }
128 
129         @Override
130         public AsciiString convertByte(byte value) {
131             return new AsciiString(String.valueOf(value));
132         }
133 
134         @Override
135         public byte convertToByte(AsciiString value) {
136             return value.byteAt(0);
137         }
138     };
139 
140     private static final NameConverter<AsciiString> ASCII_TO_LOWER_CONVERTER = new NameConverter<AsciiString>() {
141         @Override
142         public AsciiString convertName(AsciiString name) {
143             return name.toLowerCase();
144         }
145     };
146 
147     private static final NameConverter<AsciiString> ASCII_IDENTITY_CONVERTER = new NameConverter<AsciiString>() {
148         @Override
149         public AsciiString convertName(AsciiString name) {
150             return name;
151         }
152     };
153 
154     public DefaultBinaryHeaders() {
155         this(false);
156     }
157 
158     public DefaultBinaryHeaders(boolean forceKeyToLower) {
159         super(CASE_INSENSITIVE_ORDER, CASE_INSENSITIVE_ORDER, ASCII_HASH_CODE_GENERATOR, OBJECT_TO_ASCII,
160                 forceKeyToLower ? ASCII_TO_LOWER_CONVERTER : ASCII_IDENTITY_CONVERTER);
161     }
162 
163     @Override
164     public BinaryHeaders add(AsciiString name, AsciiString value) {
165         super.add(name, value);
166         return this;
167     }
168 
169     @Override
170     public BinaryHeaders add(AsciiString name, Iterable<? extends AsciiString> values) {
171         super.add(name, values);
172         return this;
173     }
174 
175     @Override
176     public BinaryHeaders add(AsciiString name, AsciiString... values) {
177         super.add(name, values);
178         return this;
179     }
180 
181     @Override
182     public BinaryHeaders addObject(AsciiString name, Object value) {
183         super.addObject(name, value);
184         return this;
185     }
186 
187     @Override
188     public BinaryHeaders addObject(AsciiString name, Iterable<?> values) {
189         super.addObject(name, values);
190         return this;
191     }
192 
193     @Override
194     public BinaryHeaders addObject(AsciiString name, Object... values) {
195         super.addObject(name, values);
196         return this;
197     }
198 
199     @Override
200     public BinaryHeaders addBoolean(AsciiString name, boolean value) {
201         super.addBoolean(name, value);
202         return this;
203     }
204 
205     @Override
206     public BinaryHeaders addChar(AsciiString name, char value) {
207         super.addChar(name, value);
208         return this;
209     }
210 
211     @Override
212     public BinaryHeaders addByte(AsciiString name, byte value) {
213         super.addByte(name, value);
214         return this;
215     }
216 
217     @Override
218     public BinaryHeaders addShort(AsciiString name, short value) {
219         super.addShort(name, value);
220         return this;
221     }
222 
223     @Override
224     public BinaryHeaders addInt(AsciiString name, int value) {
225         super.addInt(name, value);
226         return this;
227     }
228 
229     @Override
230     public BinaryHeaders addLong(AsciiString name, long value) {
231         super.addLong(name, value);
232         return this;
233     }
234 
235     @Override
236     public BinaryHeaders addFloat(AsciiString name, float value) {
237         super.addFloat(name, value);
238         return this;
239     }
240 
241     @Override
242     public BinaryHeaders addDouble(AsciiString name, double value) {
243         super.addDouble(name, value);
244         return this;
245     }
246 
247     @Override
248     public BinaryHeaders addTimeMillis(AsciiString name, long value) {
249         super.addTimeMillis(name, value);
250         return this;
251     }
252 
253     @Override
254     public BinaryHeaders add(BinaryHeaders headers) {
255         super.add(headers);
256         return this;
257     }
258 
259     @Override
260     public BinaryHeaders set(AsciiString name, AsciiString value) {
261         super.set(name, value);
262         return this;
263     }
264 
265     @Override
266     public BinaryHeaders set(AsciiString name, Iterable<? extends AsciiString> values) {
267         super.set(name, values);
268         return this;
269     }
270 
271     @Override
272     public BinaryHeaders set(AsciiString name, AsciiString... values) {
273         super.set(name, values);
274         return this;
275     }
276 
277     @Override
278     public BinaryHeaders setObject(AsciiString name, Object value) {
279         super.setObject(name, value);
280         return this;
281     }
282 
283     @Override
284     public BinaryHeaders setObject(AsciiString name, Iterable<?> values) {
285         super.setObject(name, values);
286         return this;
287     }
288 
289     @Override
290     public BinaryHeaders setObject(AsciiString name, Object... values) {
291         super.setObject(name, values);
292         return this;
293     }
294 
295     @Override
296     public BinaryHeaders setBoolean(AsciiString name, boolean value) {
297         super.setBoolean(name, value);
298         return this;
299     }
300 
301     @Override
302     public BinaryHeaders setChar(AsciiString name, char value) {
303         super.setChar(name, value);
304         return this;
305     }
306 
307     @Override
308     public BinaryHeaders setByte(AsciiString name, byte value) {
309         super.setByte(name, value);
310         return this;
311     }
312 
313     @Override
314     public BinaryHeaders setShort(AsciiString name, short value) {
315         super.setShort(name, value);
316         return this;
317     }
318 
319     @Override
320     public BinaryHeaders setInt(AsciiString name, int value) {
321         super.setInt(name, value);
322         return this;
323     }
324 
325     @Override
326     public BinaryHeaders setLong(AsciiString name, long value) {
327         super.setLong(name, value);
328         return this;
329     }
330 
331     @Override
332     public BinaryHeaders setFloat(AsciiString name, float value) {
333         super.setFloat(name, value);
334         return this;
335     }
336 
337     @Override
338     public BinaryHeaders setDouble(AsciiString name, double value) {
339         super.setDouble(name, value);
340         return this;
341     }
342 
343     @Override
344     public BinaryHeaders setTimeMillis(AsciiString name, long value) {
345         super.setTimeMillis(name, value);
346         return this;
347     }
348 
349     @Override
350     public BinaryHeaders set(BinaryHeaders headers) {
351         super.set(headers);
352         return this;
353     }
354 
355     @Override
356     public BinaryHeaders setAll(BinaryHeaders headers) {
357         super.setAll(headers);
358         return this;
359     }
360 
361     @Override
362     public BinaryHeaders clear() {
363         super.clear();
364         return this;
365     }
366 }