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    * https://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.netty5.handler.codec;
16  
17  import io.netty5.util.AsciiString;
18  
19  import java.time.format.DateTimeParseException;
20  import java.util.Date;
21  
22  /**
23   * Converts to/from native types, general {@link Object}, and {@link CharSequence}s.
24   */
25  public class CharSequenceValueConverter implements ValueConverter<CharSequence> {
26      public static final CharSequenceValueConverter INSTANCE = new CharSequenceValueConverter();
27      private static final AsciiString TRUE_ASCII = new AsciiString("true");
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          return AsciiString.contentEqualsIgnoreCase(value, TRUE_ASCII);
70      }
71  
72      @Override
73      public CharSequence convertByte(byte value) {
74          return String.valueOf(value);
75      }
76  
77      @Override
78      public byte convertToByte(CharSequence value) {
79          if (value instanceof AsciiString && value.length() == 1) {
80              return ((AsciiString) value).byteAt(0);
81          }
82          return Byte.parseByte(value.toString());
83      }
84  
85      @Override
86      public char convertToChar(CharSequence value) {
87          return value.charAt(0);
88      }
89  
90      @Override
91      public CharSequence convertShort(short value) {
92          return String.valueOf(value);
93      }
94  
95      @Override
96      public short convertToShort(CharSequence value) {
97          if (value instanceof AsciiString) {
98              return ((AsciiString) value).parseShort();
99          }
100         return Short.parseShort(value.toString());
101     }
102 
103     @Override
104     public int convertToInt(CharSequence value) {
105         if (value instanceof AsciiString) {
106             return ((AsciiString) value).parseInt();
107         }
108         return Integer.parseInt(value.toString());
109     }
110 
111     @Override
112     public long convertToLong(CharSequence value) {
113         if (value instanceof AsciiString) {
114             return ((AsciiString) value).parseLong();
115         }
116         return Long.parseLong(value.toString());
117     }
118 
119     @Override
120     public CharSequence convertTimeMillis(long value) {
121         return DateFormatter.format(new Date(value));
122     }
123 
124     @Override
125     public long convertToTimeMillis(CharSequence value) {
126         Date date = DateFormatter.parseHttpDate(value);
127         if (date == null) {
128             throw new DateTimeParseException("header can't be parsed into a Date: " + value, value, 0);
129         }
130         return date.getTime();
131     }
132 
133     @Override
134     public float convertToFloat(CharSequence value) {
135         if (value instanceof AsciiString) {
136             return ((AsciiString) value).parseFloat();
137         }
138         return Float.parseFloat(value.toString());
139     }
140 
141     @Override
142     public double convertToDouble(CharSequence value) {
143         if (value instanceof AsciiString) {
144             return ((AsciiString) value).parseDouble();
145         }
146         return Double.parseDouble(value.toString());
147     }
148 }