类 ConvertUtils

java.lang.Object
com.alibaba.nacos.common.utils.ConvertUtils

public final class ConvertUtils extends Object
Value Convert Utils.
作者:
liaochuntao
  • 构造器概要

    构造器
    构造器
    说明
     
  • 方法概要

    修饰符和类型
    方法
    说明
    static boolean
    toBoolean​(String str)
    Converts a String to a boolean (optimised for performance).
    static boolean
    toBoolean​(String val, boolean defaultValue)
    Convert String value to boolean value if parameter value is legal.
    static Boolean
    Converts a String to a Boolean.
    static int
    toInt​(String val)
    Convert String value to int value if parameter value is legal.
    static int
    toInt​(String val, int defaultValue)
    Convert String value to int value if parameter value is legal.
    static long
    toLong​(Object val)
    Convert Object value to long value if parameter value is legal.
    static long
    toLong​(String val)
    Convert String value to long value if parameter value is legal.
    static long
    toLong​(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
  • 构造器详细资料

    • ConvertUtils

      public ConvertUtils()
  • 方法详细资料

    • toInt

      public static int toInt(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(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(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(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(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(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(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 Boolean toBooleanObject(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