类 ConvertUtils


  • public final class ConvertUtils
    extends java.lang.Object
    Value Convert Utils.
    作者:
    liaochuntao
    • 字段概要

      字段 
      修饰符和类型 字段 说明
      static java.util.Set<java.lang.String> FALSE_SET  
      private static java.lang.String NULL_STR  
      static java.util.Set<java.lang.String> TRUE_SET  
    • 方法概要

      所有方法 静态方法 具体方法 
      修饰符和类型 方法 说明
      static boolean toBoolean​(java.lang.String str)
      Converts a String to a boolean (optimised for performance).
      static boolean toBoolean​(java.lang.String val, boolean defaultValue)
      Convert String value to boolean value if parameter value is legal.
      static java.lang.Boolean toBooleanObject​(java.lang.String str)
      Converts a String to a Boolean.
      static int toInt​(java.lang.String val)
      Convert String value to int value if parameter value is legal.
      static int toInt​(java.lang.String val, int defaultValue)
      Convert String value to int value if parameter value is legal.
      static long toLong​(java.lang.Object val)
      Convert Object value to long value if parameter value is legal.
      static long toLong​(java.lang.String val)
      Convert String value to long value if parameter value is legal.
      static long toLong​(java.lang.String val, long defaultValue)
      Convert String value to long value if parameter value is legal.
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 字段详细资料

      • NULL_STR

        private static final java.lang.String NULL_STR
        另请参阅:
        常量字段值
      • TRUE_SET

        public static final java.util.Set<java.lang.String> TRUE_SET
      • FALSE_SET

        public static final java.util.Set<java.lang.String> FALSE_SET
    • 构造器详细资料

      • ConvertUtils

        public ConvertUtils()
    • 方法详细资料

      • toInt

        public static int toInt​(java.lang.String val)
        Convert String value to int value if parameter value is legal. And it automatically defaults to 0 if parameter value is null or blank str.
        参数:
        val - String value which need to be converted to int value.
        返回:
        Converted int value and its default value is 0.
      • toInt

        public static int toInt​(java.lang.String val,
                                int defaultValue)
        Convert String value to int value if parameter value is legal. And return default value if parameter value is null or blank str.
        参数:
        val - value
        defaultValue - default value
        返回:
        int value if input value is legal, otherwise default value
      • toLong

        public static long toLong​(java.lang.Object val)
        Convert Object value to long value if parameter value is legal. And it automatically defaults to 0 if parameter value is null or other object.
        参数:
        val - object value
        返回:
        Converted long value and its default value is 0.
      • toLong

        public static long toLong​(java.lang.String val)
        Convert String value to long value if parameter value is legal. And it automatically defaults to 0 if parameter value is null or blank str.
        参数:
        val - String value which need to be converted to int value.
        返回:
        Converted long value and its default value is 0.
      • toLong

        public static long toLong​(java.lang.String val,
                                  long defaultValue)
        Convert String value to long value if parameter value is legal. And return default value if parameter value is null or blank str.
        参数:
        val - value
        defaultValue - default value
        返回:
        long value if input value is legal, otherwise default value
      • toBoolean

        public static boolean toBoolean​(java.lang.String val,
                                        boolean defaultValue)
        Convert String value to boolean value if parameter value is legal. And return default value if parameter value is null or blank str.
        参数:
        val - value
        defaultValue - default value
        返回:
        boolean value if input value is legal, otherwise default value
      • toBoolean

        public static boolean toBoolean​(java.lang.String str)

        Converts a String to a boolean (optimised for performance).

        'true', 'on', 'y', 't' or 'yes' (case insensitive) will return true. Otherwise, false is returned.

        This method performs 4 times faster (JDK1.4) than Boolean.valueOf(String). However, this method accepts 'on' and 'yes', 't', 'y' as true values.

           BooleanUtils.toBoolean(null)    = false
           BooleanUtils.toBoolean("true")  = true
           BooleanUtils.toBoolean("TRUE")  = true
           BooleanUtils.toBoolean("tRUe")  = true
           BooleanUtils.toBoolean("on")    = true
           BooleanUtils.toBoolean("yes")   = true
           BooleanUtils.toBoolean("false") = false
           BooleanUtils.toBoolean("x gti") = false
           BooleanUtils.toBooleanObject("y") = true
           BooleanUtils.toBooleanObject("n") = false
           BooleanUtils.toBooleanObject("t") = true
           BooleanUtils.toBooleanObject("f") = false
         
        参数:
        str - the String to check
        返回:
        the boolean value of the string, false if no match or the String is null
      • toBooleanObject

        public static java.lang.Boolean toBooleanObject​(java.lang.String str)

        Converts a String to a Boolean.

        'true', 'on', 'y', 't' or 'yes' (case insensitive) will return true. 'false', 'off', 'n', 'f' or 'no' (case insensitive) will return false. Otherwise, null is returned.

        NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.

           // N.B. case is not significant
           BooleanUtils.toBooleanObject(null)    = null
           BooleanUtils.toBooleanObject("true")  = Boolean.TRUE
           BooleanUtils.toBooleanObject("T")     = Boolean.TRUE // i.e. T[RUE]
           BooleanUtils.toBooleanObject("false") = Boolean.FALSE
           BooleanUtils.toBooleanObject("f")     = Boolean.FALSE // i.e. f[alse]
           BooleanUtils.toBooleanObject("No")    = Boolean.FALSE
           BooleanUtils.toBooleanObject("n")     = Boolean.FALSE // i.e. n[o]
           BooleanUtils.toBooleanObject("on")    = Boolean.TRUE
           BooleanUtils.toBooleanObject("ON")    = Boolean.TRUE
           BooleanUtils.toBooleanObject("off")   = Boolean.FALSE
           BooleanUtils.toBooleanObject("oFf")   = Boolean.FALSE
           BooleanUtils.toBooleanObject("yes")   = Boolean.TRUE
           BooleanUtils.toBooleanObject("Y")     = Boolean.TRUE // i.e. Y[ES]
           BooleanUtils.toBooleanObject("blue")  = null
           BooleanUtils.toBooleanObject("true ") = null // trailing space (too long)
           BooleanUtils.toBooleanObject("ono")   = null // does not match on or no
         
        参数:
        str - the String to check; upper and lower case are treated as the same
        返回:
        the Boolean value of the string, null if no match or null input