Class UtilP

java.lang.Object
org.plumelib.util.UtilP

public final class UtilP extends Object
Utility methods that do not belong elsewhere in the plume package: BitSet; hashing; ProcessBuilder; properties; Throwable.
  • Method Summary

    Modifier and Type
    Method
    Description
    static @Nullable String
    Set the property to its previous value concatenated to the given value.
    static String
    backticks(@Nullable File dir, List<String> command)
    Execute the given command in the given directory, and return all its output as a string.
    static String
    backticks(File dir, String... command)
    Execute the given command in the given directory, and return all its output as a string.
    static String
    backticks(String... command)
    Execute the given command, and return all its output as a string.
    static String
    backticks(List<String> command)
    Execute the given command, and return all its output as a string.
    static <T> @PolyNull @PolySigned T
    clone(@PolyNull @PolySigned T data)
    Clones the given object by calling clone() reflectively.
    static <T> T
    firstNonNull(T first, T second)
    Returns the first argument if it is non-null, otherwise returns the second argument.
    static boolean
    Returns true if a property has a string value that represents true: "true", "yes", or "1".
    static boolean
    getBooleanProperty(Properties p, String key, boolean defaultValue)
    Returns true if a property has a string value that represents true: "true", "yes", or "1".
    static int
    hash(double @Nullable [] a, double @Nullable [] b)
    Returns a hash of the arguments.
    static int
    hash(long @Nullable [] a, long @Nullable [] b)
    Returns a hash of the arguments.
    static @Nullable String
    Set the property only if it was not previously set.
    static String
    Returns a String representation of the stack trace (the backtrace) of the given Throwable.

    Methods inherited from class java.lang.Object

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

    • clone

      public static <T> @PolyNull @PolySigned T clone(@PolyNull @PolySigned T data)
      Clones the given object by calling clone() reflectively. It is not possible to call Object.clone() directly because it has protected visibility.
      Type Parameters:
      T - the type of the object to clone
      Parameters:
      data - the object to clone
      Returns:
      a clone of the object
    • firstNonNull

      public static <T> T firstNonNull(T first, T second)
      Returns the first argument if it is non-null, otherwise returns the second argument. Throws an exception if both arguments are null.
      Type Parameters:
      T - the type of the arguments
      Parameters:
      first - a reference
      second - a reference
      Returns:
      the first argument that is non-null
    • hash

      public static int hash(double @Nullable [] a, double @Nullable [] b)
      Returns a hash of the arguments.
      Parameters:
      a - value to be hashed
      b - value to be hashed
      Returns:
      a hash of the arguments
    • hash

      public static int hash(long @Nullable [] a, long @Nullable [] b)
      Returns a hash of the arguments.
      Parameters:
      a - value to be hashed
      b - value to be hashed
      Returns:
      a hash of the arguments
    • backticks

      public static String backticks(String... command)
      Execute the given command, and return all its output as a string.
      Parameters:
      command - a command to execute on the command line
      Returns:
      all the output of the command
    • backticks

      public static String backticks(File dir, String... command)
      Execute the given command in the given directory, and return all its output as a string.
      Parameters:
      dir - the directory in which to execute the command
      command - a command to execute on the command line
      Returns:
      all the output of the command
    • backticks

      public static String backticks(List<String> command)
      Execute the given command, and return all its output as a string.
      Parameters:
      command - a command to execute on the command line, as a list of strings (the command, then its arguments)
      Returns:
      all the output of the command
    • backticks

      public static String backticks(@Nullable File dir, List<String> command)
      Execute the given command in the given directory, and return all its output as a string.
      Parameters:
      dir - the directory in which to execute the command
      command - a command to execute on the command line, as a list of strings (the command, then its arguments)
      Returns:
      all the output of the command
    • getBooleanProperty

      @Pure public static boolean getBooleanProperty(Properties p, String key, boolean defaultValue)
      Returns true if a property has a string value that represents true: "true", "yes", or "1". Errs if the property is set to a value that is not one of "true", "false", "yes", "no", "1", or "0".
      Parameters:
      p - a Properties object in which to look up the property
      key - name of the property to look up
      defaultValue - the value to return if the property is not set
      Returns:
      true iff the property has value "true", "yes", or "1"
      See Also:
    • getBooleanProperty

      @Pure public static boolean getBooleanProperty(Properties p, String key)
      Returns true if a property has a string value that represents true: "true", "yes", or "1". Errs if the property is set to a value that is not one of "true", "false", "yes", "no", "1", or "0".
      Parameters:
      p - a Properties object in which to look up the property
      key - name of the property to look up
      Returns:
      true iff the property has value "true", "yes", or "1"
      See Also:
    • appendProperty

      public static @Nullable String appendProperty(Properties p, String key, String value)
      Set the property to its previous value concatenated to the given value. Return the previous value.
      Parameters:
      p - a Properties object in which to look up the property
      key - name of the property to look up
      value - value to concatenate to the previous value of the property
      Returns:
      the previous value of the property
      See Also:
    • setDefaultMaybe

      public static @Nullable String setDefaultMaybe(Properties p, String key, String value)
      Set the property only if it was not previously set.
      Parameters:
      p - a Properties object in which to look up the property
      key - name of the property to look up
      value - value to set the property to, if it is not already set
      Returns:
      the previous value of the property
      See Also:
    • stackTraceToString

      public static String stackTraceToString(Throwable t)
      Returns a String representation of the stack trace (the backtrace) of the given Throwable. For a stack trace at the current location, do stackTraceToString(new Throwable()).
      Parameters:
      t - the Throwable to obtain a stack trace of
      Returns:
      a String representation of the stack trace of the given Throwable