public class Utils
extends java.lang.Object
| Constructor and Description |
|---|
Utils() |
| Modifier and Type | Method and Description |
|---|---|
static <T extends java.util.Collection> |
add(T collection,
java.lang.Object... items)
Fluent override of Collections.addAll()
|
static org.apache.commons.collections4.multimap.ArrayListValuedHashMap |
addToMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap<java.lang.String,java.lang.String> multiMap,
java.lang.String... kvPairs) |
static <M extends java.util.Map<java.lang.String,java.lang.String>> |
addToMap(M map,
java.lang.String... keyValuePairs) |
static java.util.ArrayList |
asList(java.lang.Object... objects)
Similar to Arrays.asList but with raw ArrayList return type assuming that
all objects don't have to be the same type.
|
static java.util.HashMap |
asMap(java.lang.Object... keyValuePairs)
Adds each even and old object as a key/value pair to a HashMap
|
static java.util.HashSet |
asSet(java.lang.Object... objects)
Similar to Arrays.asList but returning a raw HashSet assuming that
all objects don't have to be the same type.
|
static boolean |
atob(java.lang.Object str)
Converst a string to a boolean.
|
static double |
atod(java.lang.Object str)
Convert a string to a double.
|
static float |
atof(java.lang.Object str)
Convert a string to a float.
|
static int |
atoi(java.lang.Object str)
Convert a string to an integer.
|
static long |
atol(java.lang.Object str)
Convert a string to a long.
|
static java.lang.Object |
castDbOutput(java.lang.String type,
java.lang.Object value) |
static java.lang.Object |
castJsonInput(java.lang.String type,
java.lang.Object value) |
static void |
close(java.lang.Object... toClose)
Utility to call a close() method on supplied objects if it exists and completely ignore any exceptions.
|
static boolean |
containsToken(java.lang.String findThisToken,
java.lang.String inThisString)
Checks for a whole word case insensitive match of
findThisToken
in inThisString |
static java.io.File |
createTempFile(java.lang.String fileName) |
static java.util.Date |
date(java.lang.String date)
Attempts to parse a date with several usual formats.
|
static java.util.Date |
date(java.lang.String date,
java.lang.String format)
Faster way to apply a SimpleDateFormat without having to catch ParseException
|
static java.lang.String |
dequote(java.lang.String str)
Removes all matching pairs of '"` characters from the
start and end of a string.
|
static java.lang.String |
dequote(java.lang.String str,
char[] quoteChars)
Removes all matching pairs of leading/trailing
quoteChars from the start and end of a string. |
static boolean |
empty(java.lang.Object... arr) |
static boolean |
endsWith(java.lang.CharSequence seq,
java.lang.String end)
String.endsWith surrogate for StringBuffer and StringBuilder
|
static boolean |
equal(java.lang.Object obj1,
java.lang.Object obj2)
A null safe loose equality checker.
|
static void |
error(java.lang.String message)
Shortcut for throw new RuntimeException(message);
|
static java.util.List<java.lang.String> |
explode(java.lang.String delimiter,
java.lang.String... pieces)
Similar to String.split but trims whitespace and excludes empty strings
|
static java.io.InputStream |
findInputStream(java.lang.String fileOrUrl)
Attempts to locate the stream as a file, url, or classpath resource
|
static java.lang.String |
findSysEnvProp(java.lang.String... names) |
static java.lang.Object |
first(java.util.List list) |
static java.lang.String |
format(java.lang.String format,
java.lang.Object... args)
A heroically forgiving message string formatter.
|
static java.lang.String |
formatDate(java.util.Date date,
java.lang.String format)
Simple one liner to avoid verbosity of using SimpleDateFormat
|
static java.lang.String |
formatIso8601(java.util.Date date) |
static java.lang.Object |
getBeanProperty(java.lang.String propertyName,
java.lang.Object bean)
Tries to find a bean property getter then defaults to returning the Field value
|
static java.lang.Throwable |
getCause(java.lang.Throwable t)
Tries to unwrap nested exceptions looking for the root cause
|
static java.lang.reflect.Field |
getField(java.lang.String fieldName,
java.lang.Class clazz)
Searches the inheritance hierarchy for a field with the the given name and makes sure it is settable via Field.setAccessible().
|
static java.util.List<java.lang.reflect.Field> |
getFields(java.lang.Class clazz)
Gets all the fields from from all classes in the inheritance hierarchy EXCEPT for any class who's packages starts with "java*".
|
static java.lang.reflect.Method |
getMethod(java.lang.Class clazz,
java.lang.String name)
Searches the inheritance hierarchy for the first method of the given name (ignores case).
|
static java.lang.String |
getShortCause(java.lang.Throwable t) |
static java.lang.String |
getShortCause(java.lang.Throwable t,
int lines) |
static java.util.List<java.lang.String> |
getStackTraceLines(java.lang.Throwable stackTrace) |
static java.lang.String |
getStackTraceString(java.lang.Throwable stackTrace) |
static java.lang.String |
getSysEnvProp(java.lang.String name) |
static java.lang.Object |
getSysEnvProp(java.lang.String name,
java.lang.Object defaultValue) |
static boolean |
getSysEnvPropBool(java.lang.String name,
java.lang.Object defaultValue) |
static int |
getSysEnvPropInt(java.lang.String name,
java.lang.Object defaultValue) |
static java.lang.String |
getSysEnvPropStr(java.lang.String name,
java.lang.Object defaultValue) |
static java.lang.String |
hash(byte[] bytes,
java.lang.String algorithm) |
static java.lang.String |
implode(java.lang.String glue,
java.lang.Object... pieces)
Concatenates non empty
pieces separated by glue and
intelligently flattens collections. |
static boolean |
in(java.lang.Object toFind,
java.lang.Object... values)
Checks to see if
toFind is in values array using loose equality checking |
static boolean |
isWildcard(java.lang.String str) |
static java.lang.Object |
last(java.util.List list) |
static java.lang.String |
limitLines(java.lang.String text,
int limit) |
static java.lang.String |
md5(byte[] bytes) |
static java.util.Date |
parseIso8601(java.lang.String date) |
static java.util.LinkedHashMap<java.lang.String,java.lang.String> |
parseQueryString(java.lang.String query) |
static void |
pipe(java.io.InputStream in,
java.io.OutputStream out)
Copy all data from src to dst and close the streams
|
static java.lang.String |
read(java.io.File file)
Read the contents of a file to a string
|
static java.lang.String |
read(java.io.InputStream in)
Read all of the stream to a string and close the stream.
|
static java.lang.String |
read(java.lang.String fileOrUrl)
Finds an input stream for
fileOrUrl and reads it into a string |
static void |
rethrow(java.lang.String message,
java.lang.Throwable error)
Throws the root cause of e as a RuntimeException
|
static void |
rethrow(java.lang.Throwable error)
Throws the root cause of
error as a RuntimeException |
static int |
roundUp(int num,
int divisor) |
static java.lang.String |
sha1(byte[] bytes) |
static void |
sleep(long milliseconds)
Easy way to call Thread.sleep(long) without worrying about try/catch for InterruptedException
|
static java.lang.String |
slugify(java.lang.String str)
Creates a lowercase url safe string.
|
static java.util.List<java.lang.String> |
split(java.lang.String string,
char splitOn,
char... quoteChars)
Breaks the string on
splitOn but not when inside a quoteChars
quoted string. |
static java.lang.String[] |
splitLines(java.lang.String text) |
static boolean |
startsWith(java.lang.CharSequence seq,
java.lang.String start)
String.startsWith surrogate for StringBuffer and StringBuilder
|
static java.lang.String |
substringAfter(java.lang.String string,
java.lang.String breakAfterLast) |
static java.lang.String |
substringBefore(java.lang.String string,
java.lang.String breakBefore) |
static boolean |
testCompare(java.lang.String expected,
java.lang.String actual) |
static long |
time()
Less typing to call System.currentTimeMillis()
|
static java.math.BigDecimal |
toDollarAmount(double amount)
Turns a double value into a rounded double with 2 digits of precision
12.3334 -@gt; 12.33
23.0 -@gt; 23.00
45.677 -@gt; 45.68
|
static boolean |
wildcardMatch(java.lang.String wildcard,
java.lang.String string)
Pattern matches the string using ? to indicate any one single value and * to indicate any 0-n multiple value
|
static java.lang.String |
wildcardToRegex(java.lang.String wildcard)
Converts a * and ? wildcard style patterns into regex style pattern
|
static void |
write(java.io.File file,
java.lang.String text)
Write the string value to a file
|
static void |
write(java.lang.String file,
java.lang.String text)
Convenience overloading of write(File, String)
|
public static boolean equal(java.lang.Object obj1,
java.lang.Object obj2)
obj1 - an objectobj2 - an object to compare to obj1public static boolean in(java.lang.Object toFind,
java.lang.Object... values)
toFind is in values array using loose equality checkingtoFind - the object to findvalues - where to try and find itvaluespublic static boolean empty(java.lang.Object... arr)
arr - an array of objects to check and see if any are not emptypublic static java.lang.Object first(java.util.List list)
public static java.lang.Object last(java.util.List list)
public static <T extends java.util.Collection> T add(T collection,
java.lang.Object... items)
T - a subclass of Collectioncollection - the collection to add items toitems - the items to addCollections.addAll(Collection, Object[])public static boolean endsWith(java.lang.CharSequence seq,
java.lang.String end)
seq - the string to checkend - the ending to check forpublic static boolean startsWith(java.lang.CharSequence seq,
java.lang.String start)
seq - the string to checkstart - the starting substring to check forpublic static java.lang.String implode(java.lang.String glue,
java.lang.Object... pieces)
pieces separated by glue and
intelligently flattens collections.glue - the joining stringpieces - the pieces to joinpublic static java.util.List<java.lang.String> explode(java.lang.String delimiter,
java.lang.String... pieces)
delimiter - the split delimiterpieces - the strings to splitpublic static java.util.List<java.lang.String> split(java.lang.String string,
char splitOn,
char... quoteChars)
splitOn but not when inside a quoteChars
quoted string.string - the string to splitsplitOn - the character to split onquoteChars - quote chars that invalidate the instance of slit charpublic static java.lang.String substringBefore(java.lang.String string,
java.lang.String breakBefore)
public static java.lang.String substringAfter(java.lang.String string,
java.lang.String breakAfterLast)
public static java.lang.String format(java.lang.String format,
java.lang.Object... args)
This method attempts to safely toString all of the args and replaces any "{}" characters with "%s" before formatting via String.format(String, Object[]).
Any Throwables in the args list will have their short cause string appended to the end of the formatted message.
If the format is invalid or contains too few or too many args, the method will make sure that all arg toStrings are in the output.
The goal here is to make sure that no matter what happens, you will get something useful out of this message if not exactly to the format spec.
format - a string containing "{}" arg placeholders of formatted per java.util.Formatterargs - objects that will be replaced into their format placeholders.public static org.apache.commons.collections4.multimap.ArrayListValuedHashMap addToMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap<java.lang.String,java.lang.String> multiMap,
java.lang.String... kvPairs)
public static <M extends java.util.Map<java.lang.String,java.lang.String>> M addToMap(M map,
java.lang.String... keyValuePairs)
public static java.util.ArrayList asList(java.lang.Object... objects)
objects - the objects to addobjectspublic static java.util.HashSet asSet(java.lang.Object... objects)
objects - the objects to addobjectspublic static java.util.HashMap asMap(java.lang.Object... keyValuePairs)
keyValuePairs - a list of key/value pairs that should have an even number of elementspublic static boolean containsToken(java.lang.String findThisToken,
java.lang.String inThisString)
findThisToken
in inThisString
https://www.baeldung.com/java-regexp-escape-char https://stackoverflow.com/questions/7459263/regex-whole-word
findThisToken - the string to findinThisString - in this other stringpublic static java.lang.String dequote(java.lang.String str)
str - the string to dequotepublic static java.lang.String dequote(java.lang.String str,
char[] quoteChars)
quoteChars from the start and end of a string.str - the string to dequotequoteChars - characters to treat as quotespublic static java.math.BigDecimal toDollarAmount(double amount)
amount - the amount to roundpublic static int roundUp(int num,
int divisor)
public static boolean atob(java.lang.Object str)
Easier and null safe way to call Boolean.parseBoolean(str.trim()) that swallows exceptions.
str - the string to parse as a booleanpublic static int atoi(java.lang.Object str)
Easier null safe way to call Integer.parseInt(str.trim()) that swallows exceptions.
str - the string to parsepublic static long atol(java.lang.Object str)
Easier null safe way to call Long.parseLong(str.trim()) that swallows exceptions.
str - the string to parsepublic static float atof(java.lang.Object str)
Easier null safe way to call Float.parseFloat(str.trim()) that swallows exceptions.
str - the string to parsepublic static double atod(java.lang.Object str)
Easier null safe way to call Double.parseDouble(str.trim()) that swallows exceptions.
str - the string to parsepublic static java.lang.String slugify(java.lang.String str)
str - the string to slugifypublic static java.lang.String sha1(byte[] bytes)
bytes - the bytes to hashpublic static java.lang.String md5(byte[] bytes)
bytes - the bytes to hashpublic static java.lang.String hash(byte[] bytes,
java.lang.String algorithm)
bytes - the bytes to hashalgorithm - the hash algorithmpublic static long time()
public static java.util.Date parseIso8601(java.lang.String date)
public static java.lang.String formatIso8601(java.util.Date date)
public static java.lang.String formatDate(java.util.Date date,
java.lang.String format)
date - the date to formatformat - the formatpublic static java.util.Date date(java.lang.String date,
java.lang.String format)
date - the date string to formatformat - the format stringpublic static java.util.Date date(java.lang.String date)
Formats attempted:
date - the date string to parsecastJsonInput(String, Object)public static boolean testCompare(java.lang.String expected,
java.lang.String actual)
public static java.lang.Throwable getCause(java.lang.Throwable t)
t - the error to investigatepublic static void error(java.lang.String message)
throws java.lang.RuntimeException
message - the error messagejava.lang.RuntimeException - alwayspublic static void rethrow(java.lang.Throwable error)
throws java.lang.RuntimeException
error as a RuntimeExceptionerror - error to rethrowjava.lang.RuntimeException - alwayspublic static void rethrow(java.lang.String message,
java.lang.Throwable error)
throws java.lang.RuntimeException
message - the optional message to include in the RuntimeExceptionerror - the error to rethrowjava.lang.RuntimeException - alwayspublic static void sleep(long milliseconds)
throws java.lang.RuntimeException
milliseconds - the number of milliseconds to sleepjava.lang.RuntimeException - if InterruptedException is thrownpublic static java.lang.String getShortCause(java.lang.Throwable t)
public static java.lang.String getShortCause(java.lang.Throwable t,
int lines)
public static java.util.List<java.lang.String> getStackTraceLines(java.lang.Throwable stackTrace)
public static java.lang.String getStackTraceString(java.lang.Throwable stackTrace)
public static java.lang.String[] splitLines(java.lang.String text)
public static java.lang.String limitLines(java.lang.String text,
int limit)
public static java.lang.reflect.Field getField(java.lang.String fieldName,
java.lang.Class clazz)
fieldName - the field to findclazz - the class to find it inpublic static java.util.List<java.lang.reflect.Field> getFields(java.lang.Class clazz)
clazz - the class to searchpublic static java.lang.reflect.Method getMethod(java.lang.Class clazz,
java.lang.String name)
No distinction is made for overloaded method names.
clazz - the class to searchname - the name of a method to findpublic static java.lang.Object getBeanProperty(java.lang.String propertyName,
java.lang.Object bean)
propertyName - the bean property value to findbean - the bean to find it inpublic static java.lang.String read(java.lang.String fileOrUrl)
fileOrUrl and reads it into a stringfileOrUrl - the resource to readcode as a StringfindInputStream(String)public static java.lang.String read(java.io.InputStream in)
throws java.lang.RuntimeException
in - the data to stringifyjava.lang.RuntimeException - when an IOException is thrownpublic static java.lang.String read(java.io.File file)
throws java.io.IOException
file - the file to read and stringifyjava.io.IOException - when an IOException is thrownread(InputStream)public static void write(java.io.File file,
java.lang.String text)
throws java.io.IOException
file - the file to writetext - the content to writejava.io.IOException - if unable to create the parent directory or when IO failspublic static void write(java.lang.String file,
java.lang.String text)
throws java.io.IOException
file - the file path to writetext - the text to write to the filejava.io.IOException - when thrownwrite(File, String)public static void pipe(java.io.InputStream in,
java.io.OutputStream out)
throws java.io.IOException
in - the data to be writtenout - where the data should be written tojava.io.IOException - when thrownpublic static java.io.File createTempFile(java.lang.String fileName)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.InputStream findInputStream(java.lang.String fileOrUrl)
throws java.lang.RuntimeException
fileOrUrl - a stream resource identifierjava.lang.RuntimeException - when and IOException is thrownpublic static boolean isWildcard(java.lang.String str)
str - the string to chec to see if it is a wildcard pattern.public static boolean wildcardMatch(java.lang.String wildcard,
java.lang.String string)
wildcard - a wildcard patternstring - the string to check to see if it matches the wildcardpublic static java.lang.String wildcardToRegex(java.lang.String wildcard)
wildcard - the wildcard expression to convert to a regexpublic static java.util.LinkedHashMap<java.lang.String,java.lang.String> parseQueryString(java.lang.String query)
public static java.lang.String findSysEnvProp(java.lang.String... names)
public static java.lang.String getSysEnvPropStr(java.lang.String name,
java.lang.Object defaultValue)
name - - name to look for in sysprops and envpropsdefaultValue - - will be returned if prop not foundpublic static int getSysEnvPropInt(java.lang.String name,
java.lang.Object defaultValue)
public static boolean getSysEnvPropBool(java.lang.String name,
java.lang.Object defaultValue)
public static java.lang.Object getSysEnvProp(java.lang.String name,
java.lang.Object defaultValue)
name - - name to look for in sysprops and envprops if 'value' is null;defaultValue - - will be returned if not found on sys or envpublic static java.lang.String getSysEnvProp(java.lang.String name)
public static java.lang.Object castDbOutput(java.lang.String type,
java.lang.Object value)
public static java.lang.Object castJsonInput(java.lang.String type,
java.lang.Object value)
public static void close(java.lang.Object... toClose)
toClose - the object to close.Copyright © 2021 Rocket Partners, LLC. All rights reserved.