Class ValueUtils

java.lang.Object
net.mtrop.doom.struct.utils.ValueUtils

public final class ValueUtils extends Object
Simple utility functions around values.
Author:
Matthew Tropiano
  • Method Details

    • get

      public static <T> T get(Callable<T> callable)
      This function calls the provided callable and returns its value. This is mostly for filling static fields on classes that would otherwise only need to be put in a static block.
      Type Parameters:
      T - the return type.
      Parameters:
      callable - the callable to call.
      Returns:
      the result of the callable.
      Throws:
      RuntimeException - if an exception occurs.
    • parse

      public static <T> T parse(String s, Function<String,T> parseFunction)
      Attempts to parse a string to another object.
      Type Parameters:
      T - the parse function return type.
      Parameters:
      s - the input string.
      parseFunction - the parsing function.
      Returns:
      the interpreted object.
    • parseBoolean

      public static Boolean parseBoolean(String s)
      Attempts to parse a boolean from a string. If the string is empty or null, this returns null. 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 null.
      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 null.
      Parameters:
      s - the input string.
      Returns:
      the interpreted short.
    • parseChar

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

      public static Integer parseInt(String s)
      Attempts to parse an int from a string. If the string is null or the empty string, this returns null.
      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 null.
      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 null.
      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 null.
      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.
    • parseBooleanArray

      public static boolean[] parseBooleanArray(String s, boolean[] def)
      Attempts to parse an array of booleans from a string. If the string is null or the empty string, this returns def. This assumes that the elements of the array are separated by comma-or-whitespace characters.

      Example: "true, false, apple, false" becomes [true, false, false, false]

      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the array of booleans or def if the input string is blank.
      See Also:
    • parseByteArray

      public static byte[] parseByteArray(String s, byte[] def)
      Attempts to parse an array of bytes from a string. If the string is null or the empty string, this returns def. This assumes that the elements of the array are separated by comma-or-whitespace characters.

      Example: "0, -5, 2, grape" becomes [0, -5, 2, 0]

      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the array of bytes or def if the input string is blank.
      See Also:
    • parseShortArray

      public static short[] parseShortArray(String s, short[] def)
      Attempts to parse an array of shorts from a string. If the string is null or the empty string, this returns def. This assumes that the elements of the array are separated by comma-or-whitespace characters.

      Example: "0, -5, 2, grape" becomes [0, -5, 2, 0]

      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the array of shorts or def if the input string is blank.
      See Also:
    • parseCharArray

      public static char[] parseCharArray(String s, char[] def)
      Attempts to parse an array of chars from a string. If the string is null or the empty string, this returns def. This assumes that the elements of the array are separated by comma-or-whitespace characters.

      Example: "apple, pear, b, g" becomes ['a', 'p', 'b', 'g']

      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the array of characters or def if the input string is blank.
      See Also:
    • parseIntArray

      public static int[] parseIntArray(String s, int[] def)
      Attempts to parse an array of integers from a string. If the string is null or the empty string, this returns def. This assumes that the elements of the array are separated by comma-or-whitespace characters.

      Example: "0, -5, 2.1, grape" becomes [0, -5, 2, 0]

      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the array of integers or def if the input string is blank.
      See Also:
    • parseFloatArray

      public static float[] parseFloatArray(String s, float[] def)
      Attempts to parse an array of floats from a string. If the string is null or the empty string, this returns def. This assumes that the elements of the array are separated by comma-or-whitespace characters.

      Example: "0.5, -5.4, 2, grape" becomes [0.5f, -5.4f, 2.0f, 0f]

      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the array of floats or def if the input string is blank.
      See Also:
    • parseLongArray

      public static long[] parseLongArray(String s, long[] def)
      Attempts to parse an array of longs from a string. If the string is null or the empty string, this returns def. This assumes that the elements of the array are separated by comma-or-whitespace characters.

      Example: "0, -5, 2, grape" becomes [0, -5, 2, 0]

      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the array of long integers or def if the input string is blank.
      See Also:
    • parseDoubleArray

      public static double[] parseDoubleArray(String s, double[] def)
      Attempts to parse an array of doubles from a string. If the string is null or the empty string, this returns def. This assumes that the elements of the array are separated by comma-or-whitespace characters.

      Example: "0.5, -5.4, 2, grape" becomes [0.5, -5.4, 2.0, 0.0]

      Parameters:
      s - the input string.
      def - the fallback value to return.
      Returns:
      the array of doubles or def if the input string is blank.
      See Also:
    • parseBooleanArray

      public static boolean[] parseBooleanArray(String s, String separatorRegex, boolean[] def)
      Attempts to parse an array of booleans from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      separatorRegex - the regular expression to split the string into tokens.
      def - the fallback value to return.
      Returns:
      the array of booleans or def if the input string is blank.
      Throws:
      NullPointerException - if separatorRegex is null.
      See Also:
    • parseByteArray

      public static byte[] parseByteArray(String s, String separatorRegex, byte[] def)
      Attempts to parse an array of bytes from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      separatorRegex - the regular expression to split the string into tokens.
      def - the fallback value to return.
      Returns:
      the array of bytes or def if the input string is blank.
      Throws:
      NullPointerException - if separatorRegex is null.
      See Also:
    • parseShortArray

      public static short[] parseShortArray(String s, String separatorRegex, short[] def)
      Attempts to parse an array of shorts from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      separatorRegex - the regular expression to split the string into tokens.
      def - the fallback value to return.
      Returns:
      the array of shorts or def if the input string is blank.
      Throws:
      NullPointerException - if separatorRegex is null.
      See Also:
    • parseCharArray

      public static char[] parseCharArray(String s, String separatorRegex, char[] def)
      Attempts to parse an array of chars from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      separatorRegex - the regular expression to split the string into tokens.
      def - the fallback value to return.
      Returns:
      the array of characters or def if the input string is blank.
      Throws:
      NullPointerException - if separatorRegex is null.
      See Also:
    • parseIntArray

      public static int[] parseIntArray(String s, String separatorRegex, int[] def)
      Attempts to parse an array of integers from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      separatorRegex - the regular expression to split the string into tokens.
      def - the fallback value to return.
      Returns:
      the array of integers or def if the input string is blank.
      Throws:
      NullPointerException - if separatorRegex is null.
      See Also:
    • parseFloatArray

      public static float[] parseFloatArray(String s, String separatorRegex, float[] def)
      Attempts to parse an array of floats from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      separatorRegex - the regular expression to split the string into tokens.
      def - the fallback value to return.
      Returns:
      the array of floats or def if the input string is blank.
      Throws:
      NullPointerException - if separatorRegex is null.
      See Also:
    • parseLongArray

      public static long[] parseLongArray(String s, String separatorRegex, long[] def)
      Attempts to parse an array of longs from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      separatorRegex - the regular expression to split the string into tokens.
      def - the fallback value to return.
      Returns:
      the array of long integers or def if the input string is blank.
      Throws:
      NullPointerException - if separatorRegex is null.
      See Also:
    • parseDoubleArray

      public static double[] parseDoubleArray(String s, String separatorRegex, double[] def)
      Attempts to parse an array of doubles from a string. If the string is null or the empty string, this returns def.
      Parameters:
      s - the input string.
      separatorRegex - the regular expression to split the string into tokens.
      def - the fallback value to return.
      Returns:
      the array of doubles or def if the input string is blank.
      Throws:
      NullPointerException - if separatorRegex is null.
      See Also: