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  import java.util.regex.Pattern;
21  
22  /**
23   * Conversion utility class to parse a property represented as a string or
24   * an object.
25   */
26  public final class ConversionUtil {
27  
28      /**
29       * Converts the specified object into an integer.
30       */
31      public static int toInt(Object value) {
32          if (value instanceof Number) {
33              return ((Number) value).intValue();
34          } else {
35              return Integer.parseInt(String.valueOf(value));
36          }
37      }
38  
39      /**
40       * Converts the specified object into a boolean.
41       */
42      public static boolean toBoolean(Object value) {
43          if (value instanceof Boolean) {
44              return ((Boolean) value).booleanValue();
45          }
46          if (value instanceof Number) {
47              return ((Number) value).intValue() != 0;
48          } else {
49              String s = String.valueOf(value);
50              if (s.length() == 0) {
51                  return false;
52              }
53  
54              try {
55                  return Integer.parseInt(s) != 0;
56              } catch (NumberFormatException e) {
57                  // Proceed
58              }
59  
60              switch (Character.toUpperCase(s.charAt(0))) {
61              case 'T': case 'Y':
62                  return true;
63              }
64              return false;
65          }
66      }
67  
68      private static final Pattern ARRAY_DELIM = Pattern.compile("[, \\t\\n\\r\\f\\e\\a]");
69  
70      /**
71       * Converts the specified object into an array of strings.
72       */
73      public static String[] toStringArray(Object value) {
74          if (value instanceof String[]) {
75              return (String[]) value;
76          }
77  
78          if (value instanceof Iterable<?>) {
79              List<String> answer = new ArrayList<String>();
80              for (Object v: (Iterable<?>) value) {
81                  if (v == null) {
82                      answer.add(null);
83                  } else {
84                      answer.add(String.valueOf(v));
85                  }
86              }
87              return answer.toArray(new String[answer.size()]);
88          }
89  
90          return ARRAY_DELIM.split(String.valueOf(value));
91      }
92  
93      private static final String[] INTEGERS = {
94          "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",  "8",  "9",
95          "10", "11", "12", "13", "14", "15",
96      };
97  
98      public static String toString(int value) {
99          if (value >= 0 && value < INTEGERS.length) {
100             return INTEGERS[value];
101         } else {
102             return Integer.toString(value);
103         }
104     }
105 
106     private ConversionUtil() {
107         // Unused
108     }
109 }