View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a 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
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.util.internal;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  /**
22   * Conversion utility class to parse a property represented as a string or
23   * an object.
24   */
25  public final class ConversionUtil {
26  
27      /**
28       * Converts the specified object into an integer.
29       */
30      public static int toInt(Object value) {
31          if (value instanceof Number) {
32              return ((Number) value).intValue();
33          } else {
34              return Integer.parseInt(String.valueOf(value));
35          }
36      }
37  
38      /**
39       * Converts the specified object into a boolean.
40       */
41      public static boolean toBoolean(Object value) {
42          if (value instanceof Boolean) {
43              return ((Boolean) value).booleanValue();
44          }
45          if (value instanceof Number) {
46              return ((Number) value).intValue() != 0;
47          } else {
48              String s = String.valueOf(value);
49              if (s.length() == 0) {
50                  return false;
51              }
52  
53              try {
54                  return Integer.parseInt(s) != 0;
55              } catch (NumberFormatException e) {
56                  // Proceed
57              }
58  
59              switch (Character.toUpperCase(s.charAt(0))) {
60              case 'T': case 'Y':
61                  return true;
62              }
63              return false;
64          }
65      }
66  
67      /**
68       * Converts the specified object into an array of strings.
69       */
70      public static String[] toStringArray(Object value) {
71          if (value instanceof String[]) {
72              return (String[]) value;
73          }
74  
75          if (value instanceof Iterable<?>) {
76              List<String> answer = new ArrayList<String>();
77              for (Object v: (Iterable<?>) value) {
78                  if (v == null) {
79                      answer.add(null);
80                  } else {
81                      answer.add(String.valueOf(v));
82                  }
83              }
84              return answer.toArray(new String[answer.size()]);
85          }
86  
87          return String.valueOf(value).split("[, \\t\\n\\r\\f\\e\\a]");
88      }
89  
90      private static final String[] INTEGERS = {
91          "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",  "8",  "9",
92          "10", "11", "12", "13", "14", "15",
93      };
94  
95      public static String toString(int value) {
96          if (value >= 0 && value < INTEGERS.length) {
97              return INTEGERS[value];
98          } else {
99              return Integer.toString(value);
100         }
101     }
102 
103     private ConversionUtil() {
104         // Unused
105     }
106 }