java.lang.Object
org.matwoess.jsourceprofiler.common.Util

public class Util extends Object
Provides utility methods for all modules to use.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    isAncestorOf(Path ancestorDir, Path childPath)
    Checks whether a child path starts with a given directory path.
    static boolean
    Checks whether a file path has a ".java" extension and is a file (not a directory).
    static <T> T[]
    prependToArray(T[] array, T... prependValues)
    Extend an array by prepending a number of values with the same type to it.
    static int
    runCommand(String... command)
    Run a generic command using the system command line.
    static String[]
    Run a command line in a specified directory and get the string output as a return value.
    static int
    runCommandInDir(Path cwd, String... command)
    Runs a command line in a specified working directory.

    Methods inherited from class java.lang.Object

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

    • Util

      public Util()
  • Method Details

    • prependToArray

      @SafeVarargs public static <T> T[] prependToArray(T[] array, T... prependValues)
      Extend an array by prepending a number of values with the same type to it.

      First an array is created with the size of the combined lengths. Then the prepend-values are copied to it, followed by copying the original array values.

      Type Parameters:
      T - the common type of both arrays
      Parameters:
      array - the original array to be extended
      prependValues - vararg array of values that will be prepended to it
      Returns:
      the extended array containing the prependValues followed by the original array values
    • isJavaFile

      public static boolean isJavaFile(Path path)
      Checks whether a file path has a ".java" extension and is a file (not a directory).
      Parameters:
      path - the path to be checked
      Returns:
      whether the specified path is a regular file and has a ".java" extension
    • isAncestorOf

      public static boolean isAncestorOf(Path ancestorDir, Path childPath)
      Checks whether a child path starts with a given directory path. Also returns true if the child is a file directly located in the ancestorDir. Will return false if both paths are directories and match exactly.
      Parameters:
      ancestorDir - the ancestor path to check against
      childPath - the path to check whether it is a descendant
      Returns:
      whether the child path is located somewhere inside the parent path
    • runCommand

      public static int runCommand(String... command)
      Run a generic command using the system command line.

      The ProcessBuilder class will be used to execute it.

      Parameters:
      command - an array of strings forming a command by joining it with " "
      Returns:
      the exit code of the executed command
    • runCommandInDir

      public static int runCommandInDir(Path cwd, String... command)
      Runs a command line in a specified working directory.

      like runCommand(java.lang.String...) but with a directory to execute from.

      Parameters:
      cwd - the current working directory that will be passed to the ProcessBuilder
      command - the array of strings forming the command
      Returns:
      the exit code of the executed command
    • runCommandAndGetOutput

      public static String[] runCommandAndGetOutput(Path cwd, String... command)
      Run a command line in a specified directory and get the string output as a return value.
      Parameters:
      cwd - the current working directory that will be passed to the ProcessBuilder
      command - the array of strings forming the command
      Returns:
      an array of strings representing the output of the executed command