- java.lang.Object
-
- is.codion.common.Text
-
public final class Text extends Object
A utility class for working with text, such as sorting and reading from files
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Text.Alignment
Left or right alignment
-
Field Summary
Fields Modifier and Type Field Description static PropertyValue<String>
DEFAULT_COLLATOR_LANGUAGE
Specifies the default collator locale language.
Value type: String
Default value:Locale.getDefault().getLanguage()
.static PropertyValue<Character>
WILDCARD_CHARACTER
Specifies the wildcard character used
Value type: Character
Default value: %
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
collate(List<?> values)
Sorts the string representations of the list contents, using the space aware collatorstatic int
collateSansSpaces(Collator collator, String stringOne, String stringTwo)
Collates the given strings after replacing spaces with underscoresstatic void
collateSansSpaces(Collator collator, List<?> list)
Collates the contents of the list, replacing spaces with underscores before sortingstatic String
delimitedString(List<String> header, List<List<String>> lines, String columnDelimiter)
Creates a delimited string from the given input lists.static String
padString(String string, int length, char padChar, Text.Alignment alignment)
Pads the given string with the given pad character until a length oflength
has been reachedstatic List<String>
parseCommaSeparatedValues(String commaSeparatedValues)
Parses, splits and trims the given comma separated string.static String
randomString(int minLength, int maximumLength)
Creates a random string from alphanumeric uppercase charactersstatic <T> Comparator<T>
spaceAwareCollator()
Creates a Comparator which compares the string representations of the objects using the default Collator, taking spaces into account.static <T> Comparator<T>
spaceAwareCollator(Locale locale)
Creates a Comparator which compares the string representations of the objects using the default Collator, taking spaces into account.static String
textFileContents(File file, Charset charset)
Fetch the entire contents of a textfile, and return it in a Stringstatic String
textFileContents(InputStream inputStream, Charset charset)
Fetch the entire contents of an InputStream, and return it in a String.static <T> String
textFileContents(Class<T> resourceClass, String resourceName)
Fetch the entire contents of a resource text file, and return it in a String, using the default Charset.static <T> String
textFileContents(Class<T> resourceClass, String resourceName, Charset charset)
Fetch the entire contents of a resource textfile, and return it in a String.static String
textFileContents(String filename, Charset charset)
Fetch the entire contents of a textfile, and return it in a Stringstatic String
underscoreToCamelCase(String text)
Converts a string with underscores into a camelCaseString.
-
-
-
Field Detail
-
DEFAULT_COLLATOR_LANGUAGE
public static final PropertyValue<String> DEFAULT_COLLATOR_LANGUAGE
Specifies the default collator locale language.
Value type: String
Default value:Locale.getDefault().getLanguage()
.- See Also:
spaceAwareCollator()
,collate(List)
,Locale.toLanguageTag()
-
WILDCARD_CHARACTER
public static final PropertyValue<Character> WILDCARD_CHARACTER
Specifies the wildcard character used
Value type: Character
Default value: %
-
-
Method Detail
-
randomString
public static String randomString(int minLength, int maximumLength)
Creates a random string from alphanumeric uppercase characters- Parameters:
minLength
- the minimum lengthmaximumLength
- the maximum length- Returns:
- a random string
-
collate
public static void collate(List<?> values)
Sorts the string representations of the list contents, using the space aware collator- Parameters:
values
- the list to sort (collate)- See Also:
spaceAwareCollator()
-
spaceAwareCollator
public static <T> Comparator<T> spaceAwareCollator()
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
-
spaceAwareCollator
public static <T> Comparator<T> spaceAwareCollator(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 collatorlist
- 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 usestringOne
- the first stringstringTwo
- 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 oflength
has been reached- Parameters:
string
- the string to padlength
- the desired lengthpadChar
- the character to use for paddingalignment
- the padding alignment, left or right side- Returns:
- the padded string
-
delimitedString
public static String delimitedString(List<String> header, List<List<String>> lines, String columnDelimiter)
Creates a delimited string from the given input lists.- Parameters:
header
- the headerlines
- the linescolumnDelimiter
- the column delimiter- Returns:
- a String comprised of the given header and lines using the given column delimiter
-
textFileContents
public static <T> String textFileContents(Class<T> resourceClass, String resourceName) throws IOException
Fetch the entire contents of a resource text file, and return it in a String, using the default Charset.- Type Parameters:
T
- the resource class type- Parameters:
resourceClass
- the resource classresourceName
- the name of the resource to retrieve- Returns:
- the contents of the resource file
- Throws:
IOException
- in case an IOException occurs
-
textFileContents
public static <T> String textFileContents(Class<T> resourceClass, String resourceName, Charset charset) throws IOException
Fetch the entire contents of a resource textfile, and return it in a String.- Type Parameters:
T
- the resource class type- Parameters:
resourceClass
- the resource classresourceName
- the name of the resource to retrievecharset
- the Charset to use when reading the file contents- Returns:
- the contents of the resource file
- Throws:
IOException
- in case an IOException occurs
-
textFileContents
public static String textFileContents(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 filecharset
- the charset to use- Returns:
- the file contents as a String
- Throws:
IOException
- in case of an exception
-
textFileContents
public static String textFileContents(File file, Charset charset) throws IOException
Fetch the entire contents of a textfile, and return it in a String- Parameters:
file
- the filecharset
- the charset to use- Returns:
- the file contents as a String
- Throws:
IOException
- in case of an exception
-
textFileContents
public static String textFileContents(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 readcharset
- 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, splits and trims the given comma separated string. Returns an empty list in case of null or empty string argument.- Parameters:
commaSeparatedValues
- a String with comma separated values- Returns:
- the trimmed values
-
-