Class SafeKit

java.lang.Object
com.iohao.net.common.kit.SafeKit

public final class SafeKit extends Object
Null-safe value retrieval utilities. Guarantees non-null return values by providing defaults.
Author:
渔民小镇
date:
2021-12-20
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    Return the boolean value, or false if value is null.
    static boolean
    getBoolean(Boolean value, boolean defaultValue)
    Return the boolean value, or defaultValue if value is null.
    static int
    getInt(Integer value)
    Return the int value, or 0 if value is null.
    static int
    getInt(Integer value, int defaultValue)
    Return the int value, or defaultValue if value is null.
    static int
    getInt(String value, int defaultValue)
    Parse the string as an int, or return defaultValue on failure.
    static long
    getLong(Long value)
    Return the long value, or 0 if value is null.
    static long
    getLong(Long value, long defaultValue)
    Return the long value, or defaultValue if value is null.
    static long
    getLong(String value, long defaultValue)
    Parse the string as a long, or return defaultValue on failure.
    static String
    getString(String value, String defaultValue)
    Return the string value, or defaultValue if it is null or empty.
    static int
    size(List<?> list)
    Return the size of the list, or 0 if the list is null.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getInt

      public static int getInt(Integer value)
      Return the int value, or 0 if value is null.
      Parameters:
      value - the nullable Integer
      Returns:
      the unboxed int, or 0
    • getInt

      public static int getInt(Integer value, int defaultValue)
      Return the int value, or defaultValue if value is null.
      Parameters:
      value - the nullable Integer
      defaultValue - the fallback value
      Returns:
      the unboxed int, or defaultValue
    • getInt

      public static int getInt(String value, int defaultValue)
      Parse the string as an int, or return defaultValue on failure.
      Parameters:
      value - the string to parse
      defaultValue - the fallback value
      Returns:
      the parsed int, or defaultValue if parsing fails
    • getLong

      public static long getLong(Long value)
      Return the long value, or 0 if value is null.
      Parameters:
      value - the nullable Long
      Returns:
      the unboxed long, or 0
    • getLong

      public static long getLong(Long value, long defaultValue)
      Return the long value, or defaultValue if value is null.
      Parameters:
      value - the nullable Long
      defaultValue - the fallback value
      Returns:
      the unboxed long, or defaultValue
    • getLong

      public static long getLong(String value, long defaultValue)
      Parse the string as a long, or return defaultValue on failure.
      Parameters:
      value - the string to parse
      defaultValue - the fallback value
      Returns:
      the parsed long, or defaultValue if parsing fails
    • getBoolean

      public static boolean getBoolean(Boolean value)
      Return the boolean value, or false if value is null.
      Parameters:
      value - the nullable Boolean
      Returns:
      the unboxed boolean, or false
    • getBoolean

      public static boolean getBoolean(Boolean value, boolean defaultValue)
      Return the boolean value, or defaultValue if value is null.
      Parameters:
      value - the nullable Boolean
      defaultValue - the fallback value
      Returns:
      the unboxed boolean, or defaultValue
    • getString

      public static String getString(String value, String defaultValue)
      Return the string value, or defaultValue if it is null or empty.
      Parameters:
      value - the string to check
      defaultValue - the fallback value
      Returns:
      the original string, or defaultValue if blank/null
    • size

      public static int size(List<?> list)
      Return the size of the list, or 0 if the list is null.
      Parameters:
      list - the nullable list
      Returns:
      the list size, or 0