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