View Javadoc

    1   /*
    2    * Copyright 2009 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * 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   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    26   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    27   *
    28   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    29   *
    30   */
    31  public class ConversionUtil {
    32
    33      /**
    34       * Converts the specified object into an integer.
    35       */
    36      public static int toInt(Object value) {
    37          if (value instanceof Number) {
    38              return ((Number) value).intValue();
    39          } else {
    40              return Integer.parseInt(String.valueOf(value));
    41          }
    42      }
    43
    44      /**
    45       * Converts the specified object into a boolean.
    46       */
    47      public static boolean toBoolean(Object value) {
    48          if (value instanceof Boolean) {
    49              return ((Boolean) value).booleanValue();
    50          }
    51          if (value instanceof Number) {
    52              return ((Number) value).intValue() != 0;
    53          } else {
    54              String s = String.valueOf(value);
    55              if (s.length() == 0) {
    56                  return false;
    57              }
    58
    59              try {
    60                  return Integer.parseInt(s) != 0;
    61              } catch (NumberFormatException e) {
    62                  // Proceed
    63              }
    64
    65              switch (Character.toUpperCase(s.charAt(0))) {
    66              case 'T': case 'Y':
    67                  return true;
    68              }
    69              return false;
    70          }
    71      }
    72
    73      /**
    74       * Converts the specified object into an array of strings.
    75       */
    76      public static String[] toStringArray(Object value) {
    77          if (value instanceof String[]) {
    78              return (String[]) value;
    79          }
    80
    81          if (value instanceof Iterable<?>) {
    82              List<String> answer = new ArrayList<String>();
    83              for (Object v: (Iterable<?>) value) {
    84                  if (v == null) {
    85                      answer.add(null);
    86                  } else {
    87                      answer.add(String.valueOf(v));
    88                  }
    89              }
    90              return answer.toArray(new String[answer.size()]);
    91          }
    92
    93          return String.valueOf(value).split("[, \\t\\n\\r\\f\\e\\a]");
    94      }
    95
    96      private static final String[] INTEGERS = {
    97          "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
    98          "10","11","12","13","14","15",
    99      };
    100
    101     public static String toString(int value) {
    102         if (value >= 0 && value < INTEGERS.length) {
    103             return INTEGERS[value];
    104         } else {
    105             return Integer.toString(value);
    106         }
    107     }
    108
    109     private ConversionUtil() {
    110         // Unused
    111     }
    112 }