Class Utils

java.lang.Object
com.everdro1d.libs.core.Utils

public final class Utils extends Object
A utility class providing various helper methods for common operations.

This class includes methods for string manipulation, date and time formatting, clipboard operations, map processing, command execution, and more.

All methods are static and can be accessed directly without instantiating the class.

Note: This class is not meant to be instantiated.

Key Features:

  • String operations like replacing characters and checking substrings.
  • Date and time formatting with file-system-safe options.
  • Clipboard operations for copying text.
  • Map utilities for extracting values, printing, and reversing key-value pairs.
  • Command execution in the system shell.
  • Operating system detection and configuration directory retrieval.
  • Method Details

    • openLink

      public static void openLink(String url)
      Opens the specified URL in the default web browser.
      Parameters:
      url - the URL to open
    • copyToClipboard

      public static void copyToClipboard(String copyString)
      Copies the specified string to the system clipboard.
      Parameters:
      copyString - the string to copy to the clipboard
    • getCurrentTime

      public static String getCurrentTime(boolean includeDate, boolean includeTime, boolean includeMillis)
      Returns the current date and/or time as a formatted string.
      Parameters:
      includeDate - whether to include the date in the format yyyy-MM-dd
      includeTime - whether to include the time in the format HH:mm:ss
      includeMillis - whether to include milliseconds in the format .SSS
      Returns:
      the current date and/or time as a string
    • getSanitizedCurrentTime

      public static String getSanitizedCurrentTime(boolean includeDate, boolean includeTime, boolean includeMillis)
      Returns a file-system-safe version of the current date and/or time.

      Replaces invalid file system characters with underscores.

      Parameters:
      includeDate - whether to include the date in the format yyyy-MM-dd
      includeTime - whether to include the time in the format HH:mm:ss
      includeMillis - whether to include milliseconds in the format .SSS
      Returns:
      a sanitized string representation of the current date and/or time
      See Also:
    • containsAny

      public static boolean containsAny(String[] matchingArray, String testString)
      Checks if the specified string contains any of the substrings in the given array.
      Parameters:
      matchingArray - the array of substrings to check for
      testString - the string to test
      Returns:
      true if the test string contains any of the substrings, false otherwise

      Example:

      containsAny(new String[]{"a", "b", "ab", "c"}, "abc") -> true

      containsAny(new String[]{"a", "b", "c"}, "def") -> false

    • replaceCharAt

      public static String replaceCharAt(String string, int i, String s)
      Replaces the character at the specified index in the given string with the provided replacement string.
      Parameters:
      string - the original string
      i - the index of the character to replace
      s - the replacement string
      Returns:
      a new string with the character at the specified index replaced
    • extractUniqueValuesByPredicate

      public static Set<String> extractUniqueValuesByPredicate(String property, Predicate<Map<String,String>> filter, Map<String, Map<String,String>> map)
      Extracts a set of unique values from a nested map based on a specified property and filter condition.
      Parameters:
      property - the key whose values are to be extracted
      filter - the condition to filter the values (can be null for no filtering)
      map - the nested map to process
      Returns:
      a set of unique values matching the specified property and filter

      Example:

       // Input nested map:
       Map<String, Map<String, String>> map = Map.of(
           "18", Map.of("EXT", "mp4", "ACODEC", "mp4a.40.2"),
           "22", Map.of("EXT", "webm", "ACODEC", "video only"),
           "37", Map.of("EXT", "mkv", "ACODEC", "video only")
       );
      
       // Extract unique extensions where ACODEC equals "video only":
       Set<String> values = extractUniqueValuesByPredicate(
           "EXT",
           option -> "video only".equals(option.get("ACODEC")),
           map
       );
      
       // Output:
       System.out.println(values); // [webm, mkv]
       
    • printNestedMapFormatted

      public static void printNestedMapFormatted(Object map)
      Recursively prints a nested map in a JSON-like formatted structure. Allows for infinite nesting.
      Parameters:
      map - the nested map to print

      Example:

       {
         "Key1": {
           "Value1": {
             "SubKey1-1": "SubValue1-1",
             "SubKey1-2": "SubValue1-2",
             "SubKey1-3": "SubValue1-3"
            }
         },
         ...
       }
       
    • printlnList

      public static void printlnList(List<?> list)
      Prints each element of the provided list to the console in a newline.

      This method iterates through the list and prints each element on a new line for better readability.

      Parameters:
      list - the list of elements to print

      Example:

       List<String> items = List.of("Item1", "Item2", "Item3");
       Utils.printlnList(items);
       // Output:
       // Item1
       // Item2
       // Item3
       
    • runCommand

      public static void runCommand(List<String> cmd, boolean pipeToSysOut)
      Executes a command in the system shell.
      Parameters:
      cmd - the command to execute as a list of strings
      pipeToSysOut - whether to pipe the command's output to System.out
    • runCommand

      public static void runCommand(List<String> cmd, String pwd, boolean pipeToSysOut)
      Executes a command in the system shell.
      Parameters:
      cmd - the command to execute as a list of strings
      pwd - the working directory to execute the command in
      pipeToSysOut - whether to pipe the command's output to System.out
    • runCommand

      public static void runCommand(List<String> cmd, String pwd, boolean debug, boolean pipeToSysOut)
      Executes a command in the system shell.
      Parameters:
      cmd - the command to run
      pwd - the working directory to execute the command in
      debug - whether to print debug information to System.out
      pipeToSysOut - whether to pipe the command's output to System.out
    • reverseKeyFromValueInMap

      public static String reverseKeyFromValueInMap(String value, Map<String,String> map)
      Reverses the key associated with the given value in the provided map.
      Parameters:
      value - the value to search for in the map
      map - the map to search
      Returns:
      the key associated with the given value, or null if not found
    • getUserConfigDirectory

      public static String getUserConfigDirectory()
      Retrieves the user's configuration directory based on the operating system.
      Returns:
      the path to the user's configuration directory