Class Utils

java.lang.Object
nz.ac.waikato.cms.core.Utils

public class Utils extends Object
Class implementing some simple utility methods.
Version:
$Revision: 10906 $
Author:
Eibe Frank, Yong Wang, Len Trigg, Julien Prados, FracPete (fracpete at waikat dot ac dot nz)
See Also:
  • weka.core.Utils
  • Field Details

  • Constructor Details

    • Utils

      public Utils()
  • Method Details

    • getArrayClass

      public static Class getArrayClass(Class c)
      Returns the basic class of an array class (handles multi-dimensional arrays).
      Parameters:
      c - the array to inspect
      Returns:
      the class of the innermost elements
    • getArrayDimensions

      public static int getArrayDimensions(Class array)
      Returns the dimensions of the given array. Even though the parameter is of type "Object" one can hand over primitve arrays, e.g. int[3] or double[2][4].
      Parameters:
      array - the array to determine the dimensions for
      Returns:
      the dimensions of the array
    • getArrayDimensions

      public static int getArrayDimensions(Object array)
      Returns the dimensions of the given array. Even though the parameter is of type "Object" one can hand over primitve arrays, e.g. int[3] or double[2][4].
      Parameters:
      array - the array to determine the dimensions for
      Returns:
      the dimensions of the array
    • arrayToString

      public static String arrayToString(Object array, boolean outputClass)
      Returns the given Array in a string representation. Even though the parameter is of type "Object" one can hand over primitve arrays, e.g. int[3] or double[2][4].
      Parameters:
      array - the array to return in a string representation
      outputClass - whether to output the class name instead of calling the object's "toString()" method
      Returns:
      the array as string
    • arrayToString

      public static String arrayToString(Object array)
      Returns the given Array in a string representation. Even though the parameter is of type "Object" one can hand over primitve arrays, e.g. int[3] or double[2][4].
      Parameters:
      array - the array to return in a string representation
      Returns:
      the array as string
    • flatten

      public static String flatten(List lines, String sep)
      Flattens the list into a single, long string. The separator string gets added between the objects, but not after the last one.
      Parameters:
      lines - the lines to flatten
      sep - the separator
      Returns:
      the generated string
    • flatten

      public static String flatten(Object[] lines, String sep)
      Flattens the array into a single, long string. The separator string gets added between the objects, but not after the last one. Uses the "toString()" method of the objects to turn them into a string.
      Parameters:
      lines - the lines to flatten
      sep - the separator
      Returns:
      the generated string
    • stringToClass

      public static Class stringToClass(String classname)
      Turns a class name into a Class instance. Arrays are indicated by "[]" at the end of the name. Multiple array indicators can be used.
      Parameters:
      classname - the class name to return the Class instance for
      Returns:
      the generated class instance, null if failed to create
    • newArray

      public static Object newArray(String elementClass, int length)
      Creates a new array of the specified element type with the specified number of elements. Arrays are indicated by "[]" at the end of the clas name. Multiple array indicators can be used. E.g., newArray("java.lang.Double[]", 5) will generate "Double[5][]".
      Parameters:
      elementClass - the class type for the array elements
      Returns:
      the generated array instance, null if failed to create
    • classToString

      public static String classToString(Class c)
      Turns a class into a string.
      Parameters:
      c - the class to turn into a string
      Returns:
      the string
    • classesToString

      public static String classesToString(Class[] c)
      Turns a class array into a string.
      Parameters:
      c - the class array to turn into a string
      Returns:
      the string
    • classesToString

      public static String classesToString(Class[] c, String separator)
      Turns a class array into a string.
      Parameters:
      c - the class array to turn into a string
      separator - the separator between the classes
      Returns:
      the string
    • toFloat

      public static float[] toFloat(double[] array)
      Turns the double array into a float array.
      Parameters:
      array - the array to convert
      Returns:
      the converted array
    • toDouble

      public static double[] toDouble(float[] array)
      Turns the float array into a double array.
      Parameters:
      array - the array to convert
      Returns:
      the converted array
    • padLeft

      public static String padLeft(String s, char padding, int width)
      Pads the string with a padding character with to at most "width" width. Does not truncate the string.
      Parameters:
      s - the string to pad
      padding - the padding character
      width - the maximum width
      Returns:
      the padded string
    • padLeft

      public static String padLeft(String s, char padding, int width, boolean truncate)
      Pads the string with a padding character with to at most "width" width. Truncating, if string is longer than "width", is optional.
      Parameters:
      s - the string to pad
      padding - the padding character
      width - the maximum width
      truncate - if true then the string can be truncated (on the left) to fit width
      Returns:
      the padded string
    • throwableToString

      public static String throwableToString(Throwable t)
      Returns the stacktrace of the throwable as string.
      Parameters:
      t - the throwable to get the stacktrace for
      Returns:
      the stacktrace
    • throwableToString

      public static String throwableToString(Throwable t, int maxLines)
      Returns the stacktrace of the throwable as string.
      Parameters:
      t - the throwable to get the stacktrace for
      maxLines - the maximum number of lines to print, <= 0 for all
      Returns:
      the stacktrace
    • getStackTrace

      public static String getStackTrace(int maxDepth)
      Returns the current stack trace.
      Parameters:
      maxDepth - the maximum depth of the stack trace, <= 0 for full trace
      Returns:
      the stack trace as string (multiple lines)
    • toBase

      public static List<Integer> toBase(int n, int base)
      Converts the given decimal number into a different base.
      Parameters:
      n - the decimal number to convert
      base - the base
      Returns:
      the digits in the new base; index refers to power, ie, 0 = base^0, 3 = base^3
    • split

      public static String[] split(String line, char delimiter)
      Splits the row into separate cells based on the delimiter character. String.split(regexp) does not work with empty cells (eg ",,,").
      Parameters:
      line - the row to split
      delimiter - the delimiter to use
      Returns:
      the cells
    • split

      public static String[] split(String line, String delimiter)
      Splits the row into separate cells based on the delimiter string. String.split(regexp) does not work with empty cells (eg ",,,").
      Parameters:
      line - the row to split
      delimiter - the delimiter to use
      Returns:
      the cells