Class SafeKit

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

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

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Return the boolean value, or false if value is null.
    boolean
    getBoolean(Boolean value, boolean defaultValue)
    Return the boolean value, or defaultValue if value is null.
    int
    getInt(Integer value)
    Return the int value, or 0 if value is null.
    int
    getInt(Integer value, int defaultValue)
    Return the int value, or defaultValue if value is null.
    int
    getInt(String value, int defaultValue)
    Parse the string as an int, or return defaultValue on failure.
    long
    getLong(Long value)
    Return the long value, or 0 if value is null.
    long
    getLong(Long value, long defaultValue)
    Return the long value, or defaultValue if value is null.
    long
    getLong(String value, long defaultValue)
    Parse the string as a long, or return defaultValue on failure.
    getString(String value, String defaultValue)
    Return the string value, or defaultValue if it is null or empty.
    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
  • Constructor Details

    • SafeKit

      public SafeKit()
  • Method Details

    • getInt

      public 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 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 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 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 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 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 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 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 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 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