Class Utils

java.lang.Object
com.blackrook.json.struct.Utils

public final class Utils extends Object
A utility class.
Author:
Matthew Tropiano
  • Method Details

    • isNull

      public static <T> T isNull(T testObject, T nullReturn)
      Returns the first object if it is not null, otherwise returns the second.
      Type Parameters:
      T - class that extends Object.
      Parameters:
      testObject - the first ("tested") object.
      nullReturn - the object to return if testObject is null.
      Returns:
      testObject if not null, nullReturn otherwise.
    • setFieldValue

      public static void setFieldValue(Object instance, Field field, Object value)
      Sets the value of a field on an object.
      Parameters:
      instance - the object instance to set the field on.
      field - the field to set.
      value - the value to set.
      Throws:
      NullPointerException - if the field or object provided is null.
      ClassCastException - if the value could not be cast to the proper type.
      RuntimeException - if anything goes wrong (bad field name, bad target, bad argument, or can't access the field).
      See Also:
    • getFieldValue

      public static Object getFieldValue(Object instance, Field field)
      Gets the value of a field on an object.
      Parameters:
      instance - the object instance to get the field value of.
      field - the field to get the value of.
      Returns:
      the current value of the field.
      Throws:
      NullPointerException - if the field or object provided is null.
      RuntimeException - if anything goes wrong (bad target, bad argument, or can't access the field).
      See Also:
    • invokeBlind

      public static Object invokeBlind(Method method, Object instance, Object... params)
      Blindly invokes a method, only throwing a RuntimeException if something goes wrong. Here for the convenience of not making a billion try/catch clauses for a method invocation.
      Parameters:
      method - the method to invoke.
      instance - the object instance that is the method target.
      params - the parameters to pass to the method.
      Returns:
      the return value from the method invocation. If void, this is null.
      Throws:
      ClassCastException - if one of the parameters could not be cast to the proper type.
      RuntimeException - if anything goes wrong (bad target, bad argument, or can't access the method).
      See Also:
    • isEmpty

      public static boolean isEmpty(Object obj)
      Checks if a value is "empty." The following is considered "empty":
      Parameters:
      obj - the object to check.
      Returns:
      true if the provided object is considered "empty", false otherwise.
    • isArray

      public static boolean isArray(Class<?> clazz)
      Tests if a class is actually an array type.
      Parameters:
      clazz - the class to test.
      Returns:
      true if so, false if not.
    • isArray

      public static boolean isArray(Object object)
      Tests if an object is actually an array type.
      Parameters:
      object - the object to test.
      Returns:
      true if so, false if not.
    • getArrayDimensions

      public static int getArrayDimensions(Class<?> arrayType)
      Gets how many dimensions that this array, represented by the provided type, has.
      Parameters:
      arrayType - the type to inspect.
      Returns:
      the number of array dimensions, or 0 if not an array.
    • getArrayDimensions

      public static int getArrayDimensions(Object array)
      Gets how many array dimensions that an object (presumably an array) has.
      Parameters:
      array - the object to inspect.
      Returns:
      the number of array dimensions, or 0 if not an array.
    • getArrayType

      public static Class<?> getArrayType(Class<?> arrayType)
      Gets the class type of this array type, if this is an array type.
      Parameters:
      arrayType - the type to inspect.
      Returns:
      this array's type, or null if the provided type is not an array, or if the found class is not on the classpath.
    • getArrayType

      public static Class<?> getArrayType(Object object)
      Gets the class type of this array, if this is an array.
      Parameters:
      object - the object to inspect.
      Returns:
      this array's type, or null if the provided object is not an array, or if the found class is not on the classpath.
    • create

      public static <T> T create(Class<T> clazz)
      Creates a new instance of a class from a class type. This essentially calls Class.newInstance(), but wraps the call in a try/catch block that only throws an exception if something goes wrong.
      Type Parameters:
      T - the return object type.
      Parameters:
      clazz - the class type to instantiate.
      Returns:
      a new instance of an object.
      Throws:
      RuntimeException - if instantiation cannot happen, either due to a non-existent constructor or a non-visible constructor.
    • construct

      public static <T> T construct(Constructor<T> constructor, Object... params)
      Creates a new instance of a class from a class type. This essentially calls Class.newInstance(), but wraps the call in a try/catch block that only throws an exception if something goes wrong.
      Type Parameters:
      T - the return object type.
      Parameters:
      constructor - the constructor to call.
      params - the constructor parameters.
      Returns:
      a new instance of an object created via the provided constructor.
      Throws:
      RuntimeException - if instantiation cannot happen, either due to a non-existent constructor or a non-visible constructor.
    • getEnumInstance

      public static <T extends Enum<T>> T getEnumInstance(String value, Class<T> enumClass)
      Returns the enum instance of a class given class and name, or null if not a valid name. If value is null, this returns null.
      Type Parameters:
      T - the Enum object type.
      Parameters:
      value - the value to search for.
      enumClass - the Enum class to inspect.
      Returns:
      the enum value or null if the target does not exist.
    • parseBoolean

      public static boolean parseBoolean(String s)
      Attempts to parse a boolean from a string. If the string is null, this returns false. If the string does not equal "true" (case ignored), this returns false.
      Parameters:
      s - the input string.
      Returns:
      the interpreted boolean.
    • parseByte

      public static byte parseByte(String s)
      Attempts to parse a byte from a string. If the string is null or the empty string, this returns 0.
      Parameters:
      s - the input string.
      Returns:
      the interpreted byte.
    • parseShort

      public static short parseShort(String s)
      Attempts to parse a short from a string. If the string is null or the empty string, this returns 0.
      Parameters:
      s - the input string.
      Returns:
      the interpreted short.
    • parseChar

      public static char parseChar(String s)
      Attempts to parse a char from a string. If the string is null or the empty string, this returns '\0'.
      Parameters:
      s - the input string.
      Returns:
      the first character in the string.
    • parseInt

      public static int parseInt(String s)
      Attempts to parse an int from a string. If the string is null or the empty string, this returns 0.
      Parameters:
      s - the input string.
      Returns:
      the interpreted integer.
    • parseLong

      public static long parseLong(String s)
      Attempts to parse a long from a string. If the string is null or the empty string, this returns 0.
      Parameters:
      s - the input string.
      Returns:
      the interpreted long integer.
    • parseFloat

      public static float parseFloat(String s)
      Attempts to parse a float from a string. If the string is null or the empty string, this returns 0.0f.
      Parameters:
      s - the input string.
      Returns:
      the interpreted float.
    • parseDouble

      public static double parseDouble(String s)
      Attempts to parse a double from a string. If the string is null or the empty string, this returns 0.0.
      Parameters:
      s - the input string.
      Returns:
      the interpreted double.
    • parseBoolean

      public static boolean parseBoolean(String s, boolean def)
      Attempts to parse a boolean from a string. If the string is null or the empty string, this returns def. If the string does not equal "true," this returns false.
      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the interpreted boolean or def if the input string is blank.
    • parseByte

      public static byte parseByte(String s, byte def)
      Attempts to parse a byte from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the interpreted byte or def if the input string is blank.
    • parseShort

      public static short parseShort(String s, short def)
      Attempts to parse a short from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the interpreted short or def if the input string is blank.
    • parseChar

      public static char parseChar(String s, char def)
      Attempts to parse a byte from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the first character in the string or def if the input string is blank.
    • parseInt

      public static int parseInt(String s, int def)
      Attempts to parse an int from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the interpreted integer or def if the input string is blank.
    • parseLong

      public static long parseLong(String s, long def)
      Attempts to parse a long from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the interpreted long integer or def if the input string is blank.
    • parseFloat

      public static float parseFloat(String s, float def)
      Attempts to parse a float from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the interpreted float or def if the input string is blank.
    • parseDouble

      public static double parseDouble(String s, double def)
      Attempts to parse a double from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the interpreted double or def if the input string is blank.