View Javadoc
1   /*
2    * Copyright 2015 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.AsciiString;
18  import io.netty.util.internal.PlatformDependent;
19  
20  import java.text.ParseException;
21  import java.util.Date;
22  
23  /**
24   * Converts to/from native types, general {@link Object}, and {@link CharSequence}s.
25   */
26  public class CharSequenceValueConverter implements ValueConverter<CharSequence> {
27      public static final CharSequenceValueConverter INSTANCE = new CharSequenceValueConverter();
28  
29      @Override
30      public CharSequence convertObject(Object value) {
31          if (value instanceof CharSequence) {
32              return (CharSequence) value;
33          }
34          return value.toString();
35      }
36  
37      @Override
38      public CharSequence convertInt(int value) {
39          return String.valueOf(value);
40      }
41  
42      @Override
43      public CharSequence convertLong(long value) {
44          return String.valueOf(value);
45      }
46  
47      @Override
48      public CharSequence convertDouble(double value) {
49          return String.valueOf(value);
50      }
51  
52      @Override
53      public CharSequence convertChar(char value) {
54          return String.valueOf(value);
55      }
56  
57      @Override
58      public CharSequence convertBoolean(boolean value) {
59          return String.valueOf(value);
60      }
61  
62      @Override
63      public CharSequence convertFloat(float value) {
64          return String.valueOf(value);
65      }
66  
67      @Override
68      public boolean convertToBoolean(CharSequence value) {
69          if (value instanceof AsciiString) {
70              return ((AsciiString) value).parseBoolean();
71          }
72          return Boolean.parseBoolean(value.toString());
73      }
74  
75      @Override
76      public CharSequence convertByte(byte value) {
77          return String.valueOf(value);
78      }
79  
80      @Override
81      public byte convertToByte(CharSequence value) {
82          if (value instanceof AsciiString) {
83              return ((AsciiString) value).byteAt(0);
84          }
85          return Byte.parseByte(value.toString());
86      }
87  
88      @Override
89      public char convertToChar(CharSequence value) {
90          return value.charAt(0);
91      }
92  
93      @Override
94      public CharSequence convertShort(short value) {
95          return String.valueOf(value);
96      }
97  
98      @Override
99      public short convertToShort(CharSequence value) {
100         if (value instanceof AsciiString) {
101             return ((AsciiString) value).parseShort();
102         }
103         return Short.parseShort(value.toString());
104     }
105 
106     @Override
107     public int convertToInt(CharSequence value) {
108         if (value instanceof AsciiString) {
109             return ((AsciiString) value).parseInt();
110         }
111         return Integer.parseInt(value.toString());
112     }
113 
114     @Override
115     public long convertToLong(CharSequence value) {
116         if (value instanceof AsciiString) {
117             return ((AsciiString) value).parseLong();
118         }
119         return Long.parseLong(value.toString());
120     }
121 
122     @Override
123     public CharSequence convertTimeMillis(long value) {
124         return String.valueOf(value);
125     }
126 
127     @Override
128     public long convertToTimeMillis(CharSequence value) {
129         Date date = DateFormatter.parseHttpDate(value);
130         if (date == null) {
131             PlatformDependent.throwException(new ParseException("header can't be parsed into a Date: " + value, 0));
132             return 0;
133         }
134         return date.getTime();
135     }
136 
137     @Override
138     public float convertToFloat(CharSequence value) {
139         if (value instanceof AsciiString) {
140             return ((AsciiString) value).parseFloat();
141         }
142         return Float.parseFloat(value.toString());
143     }
144 
145     @Override
146     public double convertToDouble(CharSequence value) {
147         if (value instanceof AsciiString) {
148             return ((AsciiString) value).parseDouble();
149         }
150         return Double.parseDouble(value.toString());
151     }
152 }