Class Utils


  • public class Utils
    extends java.lang.Object
    Collection of utility methods designed to make java programming less verbose
    • Constructor Summary

      Constructors 
      Constructor Description
      Utils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends java.util.Collection>
      T
      add​(T collection, java.lang.Object... items)
      Fluent override of Collections.addAll()
      static java.util.Map addToMap​(java.util.Map map, java.lang.Object... 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.LinkedHashMap 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.String beautifyName​(java.lang.String name)
      Try to make an attractive camelCase valid javascript variable name.
      static java.lang.String bytesToHex​(byte[] bytes)  
      static java.lang.String capitalize​(java.lang.String str)  
      static java.lang.Object cast​(java.lang.Class clazz, java.lang.String string)  
      static boolean checkSame​(java.lang.String str1, java.lang.String str2)  
      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 java.util.Set disjunction​(java.util.Collection a, java.util.Collection b)
      Returns the items of 'a' that are not in 'b' and the items of 'b' that are not in 'a'.
      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.lang.RuntimeException ex​(java.lang.String messageFormat, java.lang.Object... args)  
      static java.lang.RuntimeException ex​(java.lang.Throwable ex)  
      static java.lang.RuntimeException ex​(java.lang.Throwable cause, java.lang.String messageFormat, java.lang.Object... args)  
      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 <M extends java.util.Map<java.lang.String,​?>>
      M
      filter​(M map, java.lang.String... tokensToRemove)
      Removes any keys that contain a whole word token contained in tokensToRemove
      static java.io.InputStream findInputStream​(java.lang.Object caller, 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.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.Object getProperty​(java.lang.String name, java.lang.Object object)
      Tries to find a bean property getter then defaults to returning the Field value
      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.String getSysEnvProp​(java.lang.String name, java.lang.String defaultValue)  
      static java.lang.String hash​(byte[] bytes, java.lang.String algorithm)  
      static byte[] hexToBytes​(java.lang.String hexString)  
      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 isRegex​(java.lang.String string)
      Checks string for wildcard control characters.
      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.lang.String pad​(java.lang.String input, int len)  
      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 void pipe​(java.io.InputStream in, java.io.OutputStream out, boolean closeInputStream, boolean closeOutputStream)  
      static java.lang.String printTable​(java.util.List<java.util.List> table)  
      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.Object caller, java.lang.String fileOrUrl)
      Finds an input stream for fileOrUrl and reads it into a string
      static java.lang.String replaceVars​(java.lang.String string, java.util.Map<java.lang.String,​java.lang.Object> replacements)
      Replaces '{mapKeyName}' with 'mapValue' in the supplied 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 java.lang.String toPluralForm​(java.lang.String str)  
      static java.lang.String toSingularForm​(java.lang.String str)  
      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)
      • Methods inherited from class java.lang.Object

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

      • Utils

        public Utils()
    • Method Detail

      • equal

        public static boolean equal​(java.lang.Object obj1,
                                    java.lang.Object obj2)
        A null safe loose equality checker.
        Parameters:
        obj1 - an object
        obj2 - an object to compare to obj1
        Returns:
        true when args are strictly equal or toString equal
      • in

        public static boolean in​(java.lang.Object toFind,
                                 java.lang.Object... values)
        Checks to see if toFind is in values array using loose equality checking
        Parameters:
        toFind - the object to find
        values - where to try and find it
        Returns:
        true if toFind is loosely equal to any of values
      • empty

        public static boolean empty​(java.lang.Object... arr)
        Parameters:
        arr - an array of objects to check and see if any are not empty
        Returns:
        false if any args are not null with a null safe toString().trim().length() @gt; 0
      • first

        public static java.lang.Object first​(java.util.List list)
      • last

        public static java.lang.Object last​(java.util.List list)
      • add

        public static <T extends java.util.Collection> T add​(T collection,
                                                             java.lang.Object... items)
        Fluent override of Collections.addAll()
        Type Parameters:
        T - a subclass of Collection
        Parameters:
        collection - the collection to add items to
        items - the items to add
        Returns:
        the collection passed in
        See Also:
        Collections.addAll(Collection, Object[])
      • endsWith

        public static boolean endsWith​(java.lang.CharSequence seq,
                                       java.lang.String end)
        String.endsWith surrogate for StringBuffer and StringBuilder
        Parameters:
        seq - the string to check
        end - the ending to check for
        Returns:
        true if seq ends with end
      • startsWith

        public static boolean startsWith​(java.lang.CharSequence seq,
                                         java.lang.String start)
        String.startsWith surrogate for StringBuffer and StringBuilder
        Parameters:
        seq - the string to check
        start - the starting substring to check for
        Returns:
        true if seq ends with end
      • implode

        public static java.lang.String implode​(java.lang.String glue,
                                               java.lang.Object... pieces)
        Concatenates non empty pieces separated by glue and intelligently flattens collections.
        Parameters:
        glue - the joining string
        pieces - the pieces to join
        Returns:
        a concatenation of pieces separated by glue
      • explode

        public 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
        Parameters:
        delimiter - the split delimiter
        pieces - the strings to split
        Returns:
        all non empty strings from all pieces
      • split

        public 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.
        Parameters:
        string - the string to split
        splitOn - the character to split on
        quoteChars - quote chars that invalidate the instance of slit char
        Returns:
        the split parts
      • substringBefore

        public static java.lang.String substringBefore​(java.lang.String string,
                                                       java.lang.String breakBefore)
      • substringAfter

        public static java.lang.String substringAfter​(java.lang.String string,
                                                      java.lang.String breakAfterLast)
      • ex

        public static java.lang.RuntimeException ex​(java.lang.Throwable ex)
      • ex

        public static java.lang.RuntimeException ex​(java.lang.String messageFormat,
                                                    java.lang.Object... args)
      • ex

        public static java.lang.RuntimeException ex​(java.lang.Throwable cause,
                                                    java.lang.String messageFormat,
                                                    java.lang.Object... args)
      • format

        public static java.lang.String format​(java.lang.String format,
                                              java.lang.Object... args)
        A heroically forgiving message string formatter.

        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.

        Parameters:
        format - a string containing "{}" arg placeholders of formatted per java.util.Formatter
        args - objects that will be replaced into their format placeholders.
        Returns:
        the formatted string
      • addToMap

        public static java.util.Map addToMap​(java.util.Map map,
                                             java.lang.Object... keyValuePairs)
      • asList

        public 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.
        Parameters:
        objects - the objects to add
        Returns:
        a new ArrayList containing objects
      • asSet

        public 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.
        Parameters:
        objects - the objects to add
        Returns:
        a new HashSet containing objects
      • asMap

        public static java.util.LinkedHashMap asMap​(java.lang.Object... keyValuePairs)
        Adds each even and old object as a key/value pair to a HashMap
        Parameters:
        keyValuePairs - a list of key/value pairs that should have an even number of elements
        Returns:
        a new HashMap containing keyValuePairs
      • disjunction

        public static java.util.Set disjunction​(java.util.Collection a,
                                                java.util.Collection b)
        Returns the items of 'a' that are not in 'b' and the items of 'b' that are not in 'a'.
      • replaceVars

        public static java.lang.String replaceVars​(java.lang.String string,
                                                   java.util.Map<java.lang.String,​java.lang.Object> replacements)
        Replaces '{mapKeyName}' with 'mapValue' in the supplied string.
        Parameters:
        string -
        replacements -
        Returns:
      • containsToken

        public static boolean containsToken​(java.lang.String findThisToken,
                                            java.lang.String inThisString)
        Checks for a whole word case insensitive match of findThisToken in inThisString

        https://www.baeldung.com/java-regexp-escape-char https://stackoverflow.com/questions/7459263/regex-whole-word

        Parameters:
        findThisToken - the string to find
        inThisString - in this other string
        Returns:
        true if findThisToken exists as a whole world in inThisString
      • filter

        public static <M extends java.util.Map<java.lang.String,​?>> M filter​(M map,
                                                                                   java.lang.String... tokensToRemove)
        Removes any keys that contain a whole word token contained in tokensToRemove
        Type Parameters:
        M -
        Parameters:
        map -
        tokensToRemove -
        Returns:
        the same map instance, filtered
      • dequote

        public static java.lang.String dequote​(java.lang.String str)
        Removes all matching pairs of '"` characters from the start and end of a string.
        Parameters:
        str - the string to dequote
        Returns:
        str with matched pairs of leading/trailing '"` characters removed
      • dequote

        public 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.
        Parameters:
        str - the string to dequote
        quoteChars - characters to treat as quotes
        Returns:
        str with matched pairs of leading/trailing quoteChars removed
      • toDollarAmount

        public 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
        Parameters:
        amount - the amount to round
        Returns:
        the amount rounded to two decimal places
      • roundUp

        public static int roundUp​(int num,
                                  int divisor)
      • atob

        public static boolean atob​(java.lang.Object str)
        Converst a string to a boolean.

        Easier and null safe way to call Boolean.parseBoolean(str.trim()) that swallows exceptions.

        Parameters:
        str - the string to parse as a boolean
        Returns:
        true if the trimmed lower case str is "0" or "false"
      • atoi

        public static int atoi​(java.lang.Object str)
        Convert a string to an integer.

        Easier null safe way to call Integer.parseInt(str.trim()) that swallows exceptions.

        Parameters:
        str - the string to parse
        Returns:
        the parsed value or -1 if the string does not parse
      • atol

        public static long atol​(java.lang.Object str)
        Convert a string to a long.

        Easier null safe way to call Long.parseLong(str.trim()) that swallows exceptions.

        Parameters:
        str - the string to parse
        Returns:
        the parsed value or -1 if the string does not parse
      • atof

        public static float atof​(java.lang.Object str)
        Convert a string to a float.

        Easier null safe way to call Float.parseFloat(str.trim()) that swallows exceptions.

        Parameters:
        str - the string to parse
        Returns:
        the parsed value or -1 if the string does not parse
      • atod

        public static double atod​(java.lang.Object str)
        Convert a string to a double.

        Easier null safe way to call Double.parseDouble(str.trim()) that swallows exceptions.

        Parameters:
        str - the string to parse
        Returns:
        the parsed value or -1 if the string does not parse
      • slugify

        public static java.lang.String slugify​(java.lang.String str)
        Creates a lowercase url safe string.
        Parameters:
        str - the string to slugify
        Returns:
        the slugified string
      • sha1

        public static java.lang.String sha1​(byte[] bytes)
        Parameters:
        bytes - the bytes to hash
        Returns:
        Hash the bytes with SHA-1
      • md5

        public static java.lang.String md5​(byte[] bytes)
        Parameters:
        bytes - the bytes to hash
        Returns:
        Hash the bytes with MD5
      • hash

        public static java.lang.String hash​(byte[] bytes,
                                            java.lang.String algorithm)
        Parameters:
        bytes - the bytes to hash
        algorithm - the hash algorithm
        Returns:
        Hash the bytes with the given algorithm
      • time

        public static long time()
        Less typing to call System.currentTimeMillis()
        Returns:
        the current time in milliseconds
      • parseIso8601

        public static java.util.Date parseIso8601​(java.lang.String date)
      • formatIso8601

        public static java.lang.String formatIso8601​(java.util.Date date)
      • formatDate

        public static java.lang.String formatDate​(java.util.Date date,
                                                  java.lang.String format)
        Simple one liner to avoid verbosity of using SimpleDateFormat
        Parameters:
        date - the date to format
        format - the format
        Returns:
        the formatted date
      • date

        public static java.util.Date date​(java.lang.String date,
                                          java.lang.String format)
        Faster way to apply a SimpleDateFormat without having to catch ParseException
        Parameters:
        date - the date string to format
        format - the format string
        Returns:
        the formatted date
      • date

        public static java.util.Date date​(java.lang.String date)
        Attempts to parse a date with several usual formats.

        Formats attempted:

        1. an ISO8601 data
        2. EEE MMM dd HH:mm:ss zzz yyyy
        3. then yyyy-MM-dd
        4. then MM/dd/yy
        5. then MM/dd/yyyy
        6. then yyyyMMdd
        Parameters:
        date - the date string to parse
        Returns:
        the parsed date
      • testCompare

        public static boolean testCompare​(java.lang.String expected,
                                          java.lang.String actual)
      • getCause

        public static java.lang.Throwable getCause​(java.lang.Throwable t)
        Tries to unwrap nested exceptions looking for the root cause
        Parameters:
        t - the error to investigate
        Returns:
        the recursively root cause
      • error

        public static void error​(java.lang.String message)
                          throws java.lang.RuntimeException
        Shortcut for throw new RuntimeException(message);
        Parameters:
        message - the error message
        Throws:
        java.lang.RuntimeException - always
      • rethrow

        public static void rethrow​(java.lang.Throwable error)
                            throws java.lang.RuntimeException
        Throws the root cause of error as a RuntimeException
        Parameters:
        error - error to rethrow
        Throws:
        java.lang.RuntimeException - always
      • rethrow

        public static void rethrow​(java.lang.String message,
                                   java.lang.Throwable error)
                            throws java.lang.RuntimeException
        Throws the root cause of e as a RuntimeException
        Parameters:
        message - the optional message to include in the RuntimeException
        error - the error to rethrow
        Throws:
        java.lang.RuntimeException - always
      • sleep

        public static void sleep​(long milliseconds)
                          throws java.lang.RuntimeException
        Easy way to call Thread.sleep(long) without worrying about try/catch for InterruptedException
        Parameters:
        milliseconds - the number of milliseconds to sleep
        Throws:
        java.lang.RuntimeException - if InterruptedException is thrown
      • getShortCause

        public static java.lang.String getShortCause​(java.lang.Throwable t)
      • getShortCause

        public static java.lang.String getShortCause​(java.lang.Throwable t,
                                                     int lines)
      • getStackTraceLines

        public static java.util.List<java.lang.String> getStackTraceLines​(java.lang.Throwable stackTrace)
      • getStackTraceString

        public static java.lang.String getStackTraceString​(java.lang.Throwable stackTrace)
      • splitLines

        public static java.lang.String[] splitLines​(java.lang.String text)
      • limitLines

        public static java.lang.String limitLines​(java.lang.String text,
                                                  int limit)
      • checkSame

        public static boolean checkSame​(java.lang.String str1,
                                        java.lang.String str2)
      • cast

        public static java.lang.Object cast​(java.lang.Class clazz,
                                            java.lang.String string)
      • getField

        public 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().
        Parameters:
        fieldName - the field to find
        clazz - the class to find it in
        Returns:
        the first Field found with name
      • getFields

        public 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*".
        Parameters:
        clazz - the class to search
        Returns:
        all Fields in the inheritance hierarchy other "java*" packages classes.
      • getMethod

        public 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).

        No distinction is made for overloaded method names.

        Parameters:
        clazz - the class to search
        name - the name of a method to find
        Returns:
        the first method with name
      • getProperty

        public static java.lang.Object getProperty​(java.lang.String name,
                                                   java.lang.Object object)
        Tries to find a bean property getter then defaults to returning the Field value
        Parameters:
        name - the bean property value to find
        object - the object to find it in
        Returns:
        the value of the bean property with name
      • read

        public static java.lang.String read​(java.lang.Object caller,
                                            java.lang.String fileOrUrl)
        Finds an input stream for fileOrUrl and reads it into a string
        Parameters:
        fileOrUrl - the resource to read
        Returns:
        the content of code as a String
        See Also:
        findInputStream(Object, String)
      • read

        public static java.lang.String read​(java.io.InputStream in)
                                     throws java.lang.RuntimeException
        Read all of the stream to a string and close the stream.
        Parameters:
        in - the data to stringify
        Returns:
        the data from in as a string
        Throws:
        java.lang.RuntimeException - when an IOException is thrown
      • read

        public static java.lang.String read​(java.io.File file)
                                     throws java.io.IOException
        Read the contents of a file to a string
        Parameters:
        file - the file to read and stringify
        Returns:
        the file text
        Throws:
        java.io.IOException - when an IOException is thrown
        See Also:
        read(InputStream)
      • write

        public static void write​(java.io.File file,
                                 java.lang.String text)
                          throws java.io.IOException
        Write the string value to a file
        Parameters:
        file - the file to write
        text - the content to write
        Throws:
        java.io.IOException - if unable to create the parent directory or when IO fails
      • write

        public static void write​(java.lang.String file,
                                 java.lang.String text)
                          throws java.io.IOException
        Convenience overloading of write(File, String)
        Parameters:
        file - the file path to write
        text - the text to write to the file
        Throws:
        java.io.IOException - when thrown
        See Also:
        write(File, String)
      • pipe

        public static void pipe​(java.io.InputStream in,
                                java.io.OutputStream out)
                         throws java.io.IOException
        Copy all data from src to dst and close the streams
        Parameters:
        in - the data to be written
        out - where the data should be written to
        Throws:
        java.io.IOException - when thrown
      • pipe

        public static void pipe​(java.io.InputStream in,
                                java.io.OutputStream out,
                                boolean closeInputStream,
                                boolean closeOutputStream)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • createTempFile

        public static java.io.File createTempFile​(java.lang.String fileName)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • findInputStream

        public static java.io.InputStream findInputStream​(java.lang.Object caller,
                                                          java.lang.String fileOrUrl)
                                                   throws java.lang.RuntimeException
        Attempts to locate the stream as a file, url, or classpath resource
        Parameters:
        fileOrUrl - a stream resource identifier
        Returns:
        an input stream reading fileOrUrl
        Throws:
        java.lang.RuntimeException - when and IOException is thrown
      • isRegex

        public static boolean isRegex​(java.lang.String string)
        Checks string for wildcard control characters.
        Parameters:
        string -
        Returns:
        true when string contains a regex control character
      • isWildcard

        public static boolean isWildcard​(java.lang.String str)
        Parameters:
        str - the string to chec to see if it is a wildcard pattern.
        Returns:
        true if the string contains a * or a ?
      • wildcardMatch

        public 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
        Parameters:
        wildcard - a wildcard pattern
        string - the string to check to see if it matches the wildcard
        Returns:
        true if string matches wildcard
      • wildcardToRegex

        public static java.lang.String wildcardToRegex​(java.lang.String wildcard)
        Converts a * and ? wildcard style patterns into regex style pattern
        Parameters:
        wildcard - the wildcard expression to convert to a regex
        Returns:
        a wildcard pattern converted to a regex
        See Also:
        Code example found here
      • parseQueryString

        public static java.util.LinkedHashMap<java.lang.String,​java.lang.String> parseQueryString​(java.lang.String query)
      • findSysEnvProp

        public static java.lang.String findSysEnvProp​(java.lang.String... names)
      • getSysEnvProp

        public static java.lang.String getSysEnvProp​(java.lang.String name,
                                                     java.lang.String defaultValue)
      • getSysEnvProp

        public static java.lang.String getSysEnvProp​(java.lang.String name)
      • close

        public static void close​(java.lang.Object... toClose)
        Utility to call a close() method on supplied objects if it exists and completely ignore any exceptions.
        Parameters:
        toClose - the object to close.
      • toSingularForm

        public static java.lang.String toSingularForm​(java.lang.String str)
      • toPluralForm

        public static java.lang.String toPluralForm​(java.lang.String str)
      • capitalize

        public static java.lang.String capitalize​(java.lang.String str)
      • bytesToHex

        public static java.lang.String bytesToHex​(byte[] bytes)
      • hexToBytes

        public static byte[] hexToBytes​(java.lang.String hexString)
      • beautifyName

        public static java.lang.String beautifyName​(java.lang.String name)
        Try to make an attractive camelCase valid javascript variable name.

        Lots of sql db designers use things like SNAKE_CASE_COLUMN_NAMES that look terrible as json property names.

        Parameters:
        name - the to beautify
        Returns:
        a camelCased version of name
        See Also:
        JSON property name
      • printTable

        public static java.lang.String printTable​(java.util.List<java.util.List> table)
      • pad

        public static java.lang.String pad​(java.lang.String input,
                                           int len)