Class Text


  • public final class Text
    extends Object
    A utility class for working with text, such as sorting and reading from files
    • Method Detail

      • createRandomString

        public static String createRandomString​(int minLength,
                                                int maxLength)
        Creates a random string from alphanumeric uppercase characters
        Parameters:
        minLength - the minimum length
        maxLength - the maximum length
        Returns:
        a random string
      • collate

        public static void collate​(List values)
        Sorts the string representations of this lists contents, using the space aware collator
        Parameters:
        values - the list to sort (collate)
        See Also:
        getSpaceAwareCollator()
      • getSpaceAwareCollator

        public static <T> Comparator<T> getSpaceAwareCollator()
        Creates a Comparator which compares the string representations of the objects using the default Collator, taking spaces into account.
        Type Parameters:
        T - the type of the objects to compare
        Returns:
        a space aware collator
        See Also:
        DEFAULT_COLLATOR_LANGUAGE
      • getSpaceAwareCollator

        public static <T> Comparator<T> getSpaceAwareCollator​(Locale locale)
        Creates a Comparator which compares the string representations of the objects using the default Collator, taking spaces into account.
        Type Parameters:
        T - the type of the objects to compare
        Parameters:
        locale - the collator locale
        Returns:
        a space aware collator
        See Also:
        DEFAULT_COLLATOR_LANGUAGE
      • collateSansSpaces

        public static void collateSansSpaces​(Collator collator,
                                             List list)
        Collates the contents of the list, replacing spaces with underscores before sorting
        Parameters:
        collator - the collator
        list - the list
      • collateSansSpaces

        public static int collateSansSpaces​(Collator collator,
                                            String stringOne,
                                            String stringTwo)
        Collates the given strings after replacing spaces with underscores
        Parameters:
        collator - the collator to use
        stringOne - the first string
        stringTwo - the second string
        Returns:
        the collation result
      • padString

        public static String padString​(String string,
                                       int length,
                                       char padChar,
                                       Text.Alignment alignment)
        Pads the given string with the given pad character until a length of length has been reached
        Parameters:
        string - the string to pad
        length - the desired length
        padChar - the character to use for padding
        alignment - the padding alignment, left or right side
        Returns:
        the padded string
      • getDelimitedString

        public static String getDelimitedString​(List<String> header,
                                                List<List<String>> lines,
                                                String columnDelimiter)
        Creates a delimited string from the given input lists.
        Parameters:
        header - the header
        lines - the lines
        columnDelimiter - the column delimiter
        Returns:
        a String comprised of the given header and lines using the given column delimiter
      • getTextFileContents

        public static String getTextFileContents​(Class resourceClass,
                                                 String resourceName)
                                          throws IOException
        Fetch the entire contents of a resource text file, and return it in a String, using the default Charset.
        Parameters:
        resourceClass - the resource class
        resourceName - the name of the resource to retrieve
        Returns:
        the contents of the resource file
        Throws:
        IOException - in case an IOException occurs
      • getTextFileContents

        public static String getTextFileContents​(Class resourceClass,
                                                 String resourceName,
                                                 Charset charset)
                                          throws IOException
        Fetch the entire contents of a resource textfile, and return it in a String.
        Parameters:
        resourceClass - the resource class
        resourceName - the name of the resource to retrieve
        charset - the Charset to use when reading the file contents
        Returns:
        the contents of the resource file
        Throws:
        IOException - in case an IOException occurs
      • getTextFileContents

        public static String getTextFileContents​(String filename,
                                                 Charset charset)
                                          throws IOException
        Fetch the entire contents of a textfile, and return it in a String
        Parameters:
        filename - the name of the file
        charset - the charset to use
        Returns:
        the file contents as a String
        Throws:
        IOException - in case of an exception
      • getTextFileContents

        public static String getTextFileContents​(File file,
                                                 Charset charset)
                                          throws IOException
        Fetch the entire contents of a textfile, and return it in a String
        Parameters:
        file - the file
        charset - the charset to use
        Returns:
        the file contents as a String
        Throws:
        IOException - in case of an exception
      • getTextFileContents

        public static String getTextFileContents​(InputStream inputStream,
                                                 Charset charset)
                                          throws IOException
        Fetch the entire contents of an InputStream, and return it in a String. Does not close the stream.
        Parameters:
        inputStream - the input stream to read
        charset - the charset to use
        Returns:
        the stream contents as a String
        Throws:
        IOException - in case of an exception
      • parseCommaSeparatedValues

        public static List<String> parseCommaSeparatedValues​(String commaSeparatedValues)
        Parses and trims the given comma separated string.
        Parameters:
        commaSeparatedValues - a String with comma separated values
        Returns:
        the trimmed values