Package com.everdro1d.libs.core
Class Utils
java.lang.Object
com.everdro1d.libs.core.Utils
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 Summary
Modifier and TypeMethodDescriptionstatic boolean
arrayContains
(String[][] matchingArray, String testString) Checks if the given 2D array contains the specified test string.static boolean
arrayContains
(String[][] matchingArray, String testString, boolean treatAsKey) Checks if the given 2D array contains the specified test string.static void
copyToClipboard
(String copyString) Copies the specified string to the system clipboard.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.static String
getCurrentTime
(boolean includeDate, boolean includeTime, boolean includeMillis) Returns the current date and/or time as a formatted string.static String
getSanitizedCurrentTime
(boolean includeDate, boolean includeTime, boolean includeMillis) Returns a file-system-safe version of the current date and/or time.static String
Retrieves the user's configuration directory based on the operating system.static void
Opens the specified URL in the default web browser.static void
printlnList
(List<?> list) Prints each element of the provided list to the console in a newline.static void
Recursively prints a nested map in a JSON-like formatted structure.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.static <T> String
reverseKeyFromValueInMap
(T value, Map<String, T> map) Reverses the key associated with the given value in the provided map.static <T> String[]
reverseKeysFromValueInMap
(T value, Map<String, T> map) Reverses all keys associated with the given value in the provided map.static void
runCommand
(List<String> cmd, boolean pipeToSysOut) Executes a command in the system shell.static void
runCommand
(List<String> cmd, String pwd, boolean pipeToSysOut) Executes a command in the system shell.static void
runCommand
(List<String> cmd, String pwd, boolean debug, boolean pipeToSysOut) Executes a command in the system shell.static boolean
stringContainsAny
(String[] matchingArray, String testString) Checks if the specified string contains any of the substrings in the given array.static boolean
validateVersion
(String version) Validates the version string against a regex pattern.static boolean
validateVersion
(String version, String expectedVersionRegex) Validates the version string against a regex pattern.
-
Method Details
-
openLink
Opens the specified URL in the default web browser.- Parameters:
url
- the URL to open
-
copyToClipboard
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 formatyyyy-MM-dd
includeTime
- whether to include the time in the formatHH: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 formatyyyy-MM-dd
includeTime
- whether to include the time in the formatHH: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:
-
stringContainsAny
Checks if the specified string contains any of the substrings in the given array.- Parameters:
matchingArray
- the array of substrings to check fortestString
- the string to test- Returns:
true
if the test string contains any of the substrings,false
otherwiseExample:
stringContainsAny(new String[]{"a", "b", "ab", "c"}, "abc")
->true
stringContainsAny(new String[]{"a", "b", "c"}, "def")
->false
-
arrayContains
Checks if the given 2D array contains the specified test string.- Parameters:
matchingArray
- the 2D array to check intestString
- the string to test- Returns:
true
if the array contains the test string,false
otherwise
-
arrayContains
public static boolean arrayContains(String[][] matchingArray, String testString, boolean treatAsKey) Checks if the given 2D array contains the specified test string.- Parameters:
matchingArray
- the 2D array to check intestString
- the string to testtreatAsKey
- whether to treat the test string as a key (2D array pretending to be a map)- Returns:
true
if the array contains the test string,false
otherwise
-
replaceCharAt
Replaces the character at the specified index in the given string with the provided replacement string.- Parameters:
string
- the original stringi
- the index of the character to replaces
- 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 extractedfilter
- the condition to filter the values (can benull
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
Recursively prints a nested map in a JSON-like formatted structure. Allows for infinite nesting.- Parameters:
map
- the nested map to printExample:
{ "Key1": { "Value1": { "SubKey1-1": "SubValue1-1", "SubKey1-2": "SubValue1-2", "SubKey1-3": "SubValue1-3" } }, ... }
-
printlnList
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 printExample:
List<String> items = List.of("Item1", "Item2", "Item3"); Utils.printlnList(items); // Output: // Item1 // Item2 // Item3
-
runCommand
Executes a command in the system shell.- Parameters:
cmd
- the command to execute as a list of stringspipeToSysOut
- whether to pipe the command's output toSystem.out
-
runCommand
Executes a command in the system shell.- Parameters:
cmd
- the command to execute as a list of stringspwd
- the working directory to execute the command inpipeToSysOut
- whether to pipe the command's output toSystem.out
-
runCommand
Executes a command in the system shell.- Parameters:
cmd
- the command to runpwd
- the working directory to execute the command indebug
- whether to print debug information toSystem.out
pipeToSysOut
- whether to pipe the command's output toSystem.out
-
reverseKeyFromValueInMap
Reverses the key associated with the given value in the provided map.- Type Parameters:
T
- the type of the value in the map- Parameters:
value
- the value to search for in the mapmap
- the map to search- Returns:
- the key associated with the given value, or
null
if not found
-
reverseKeysFromValueInMap
Reverses all keys associated with the given value in the provided map.- Type Parameters:
T
- the type of the value in the map- Parameters:
value
- the value to search for in the mapmap
- the map to search- Returns:
- an array of keys associated with the given value, or null if not found
-
getUserConfigDirectory
Retrieves the user's configuration directory based on the operating system.- Returns:
- the path to the user's configuration directory
-
validateVersion
Validates the version string against a regex pattern.This overload matches versioning: "X.Y.Z".
- Parameters:
version
- the version string to validate- Returns:
true
if the version string matches the pattern,false
otherwise- See Also:
-
validateVersion
Validates the version string against a regex pattern.- Parameters:
version
- the version string to validateexpectedVersionRegex
- the regex pattern to match against (ex:"^(\\d+\\.\\d+\\.\\d+)"
)- Returns:
true
if the version string matches the pattern,false
otherwise- See Also:
-