com.alee.utils
Class TextUtils

java.lang.Object
  extended by com.alee.utils.TextUtils

public final class TextUtils
extends java.lang.Object

This class provides a set of utilities to work with various text usage cases.

Author:
Mikle Garin

Nested Class Summary
protected static class TextUtils.PartType
          Time part type enumeration used to parse string delay.
 
Constructor Summary
TextUtils()
           
 
Method Summary
static java.lang.String createString(java.lang.String character, int length)
          Creates new string filled with specified amount of same characters.
static
<E extends java.lang.Enum<E>>
java.lang.String
enumListToString(java.util.List<E> enumList)
          Converts list of enumeration constants into string with list of enumeration constants and returns it.
static
<E extends java.lang.Enum<E>>
java.util.List<E>
enumStringToList(java.lang.String enumString, java.lang.Class<E> enumClass)
          Converts string with list of enumeration constants into real list of enumeration constants and returns it.
static java.lang.Integer findFirstNumber(java.lang.String text)
          Returns first number found in text.
static java.lang.String generateId()
          Returns random ID with default prefix and suffix.
static java.lang.String generateId(int length)
          Returns randomly generated ID part with specified length.
static java.lang.String generateId(java.lang.String prefix)
          Returns random ID with specified prefix and default suffix.
static java.lang.String generateId(java.lang.String prefix, java.lang.String suffix)
          Returns random ID with specified prefix and suffix.
static java.lang.String getWord(java.lang.String text, int location)
          Returns a word from text at the specified location.
static int getWordEnd(java.lang.String text, int location)
          Returns a word end index at the specified location.
static int getWordStart(java.lang.String text, int location)
          Returns a word start index at the specified location.
static boolean isEmpty(java.lang.String text)
          Returns whether specified text is empty or not.
static
<T> java.lang.String
listToString(java.util.List<T> list, java.lang.String separator, TextProvider<T> textProvider)
          Returns single text combined using list of strings and specified separator.
static
<T> java.lang.String
listToString(java.util.List<T> list, java.lang.String separator, TextProvider<T> textProvider, Filter<T> filter)
          Returns single text combined using list of strings and specified separator.
static java.lang.String listToString(java.util.List list, java.lang.String separator)
          Returns single text combined using list of strings and specified separator.
static long parseDelay(java.lang.String delay)
          Either returns delay retrieved from string or throws an exception if it cannot be parsed.
static java.awt.Point parsePoint(java.lang.String text)
          Returns point extracted from text.
static java.awt.Point parsePoint(java.lang.String text, java.lang.String separator)
          Returns point extracted from text.
static java.lang.String removeControlSymbols(java.lang.String text)
          Returns text with all control symbols removed.
static java.lang.String removeFirstLines(java.lang.String text, int count)
          Returns text with first lines removed.
static java.lang.String removeLineBreaks(java.lang.String text)
          Returns text with all line breaks removed.
static java.lang.String replaceAll(java.lang.String text, boolean ignoreCase, java.lang.String str, TextProvider<java.lang.String> provider)
          Replaces all occurences of str found in the specified text with provided text.
static java.lang.String shortenText(java.lang.String text, int maxLength, boolean addDots)
          Returns shortened text.
static java.lang.String shortenTextEnd(java.lang.String text, int maxLength, boolean addDots)
          Returns shortened text.
static java.util.List<java.lang.String> split(java.lang.String text, java.lang.String separator)
          Returns a list of text parts splitted using specified separator.
static java.util.List<java.lang.Integer> stringToIntList(java.lang.String string, java.lang.String separator)
          Returns a list of integer parts splitted using specified separator.
static java.util.List<java.lang.String> stringToList(java.lang.String string, java.lang.String separator)
          Returns a list of text parts splitted using specified separator.
static java.lang.String toStringDelay(long delay)
          Returns delay string representation.
static java.lang.String unite(java.lang.String separator, java.lang.String... parts)
          Converts and returns specified parts which are not null into single string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TextUtils

public TextUtils()
Method Detail

removeLineBreaks

public static java.lang.String removeLineBreaks(java.lang.String text)
Returns text with all line breaks removed.

Parameters:
text - text to remove line breaks from
Returns:
text with all line breaks removed

findFirstNumber

public static java.lang.Integer findFirstNumber(java.lang.String text)
Returns first number found in text.

Parameters:
text - text to search through
Returns:
first found number

getWord

public static java.lang.String getWord(java.lang.String text,
                                       int location)
Returns a word from text at the specified location.

Parameters:
text - text to retrieve the word from
location - word location
Returns:
word

getWordStart

public static int getWordStart(java.lang.String text,
                               int location)
Returns a word start index at the specified location.

Parameters:
text - text to retrieve the word start index from
location - word location
Returns:
word start index

getWordEnd

public static int getWordEnd(java.lang.String text,
                             int location)
Returns a word end index at the specified location.

Parameters:
text - text to retrieve the word end index from
location - word location
Returns:
word end index

removeFirstLines

public static java.lang.String removeFirstLines(java.lang.String text,
                                                int count)
Returns text with first lines removed.

Parameters:
text - text to crop
count - lines count to crop
Returns:
cropped text

split

public static java.util.List<java.lang.String> split(java.lang.String text,
                                                     java.lang.String separator)
Returns a list of text parts splitted using specified separator.

Parameters:
text - text to split
separator - text parts separator
Returns:
list of splitted parts

parsePoint

public static java.awt.Point parsePoint(java.lang.String text)
Returns point extracted from text.

Parameters:
text - text to extract point from
Returns:
extracted point

parsePoint

public static java.awt.Point parsePoint(java.lang.String text,
                                        java.lang.String separator)
Returns point extracted from text.

Parameters:
text - text to extract point from
separator - point values separator
Returns:
extracted point

removeControlSymbols

public static java.lang.String removeControlSymbols(java.lang.String text)
Returns text with all control symbols removed. This method works faster than simple replaceAll("\\p{Cntrl}", "").

Parameters:
text - text to modify
Returns:
text without conytol symbols

shortenText

public static java.lang.String shortenText(java.lang.String text,
                                           int maxLength,
                                           boolean addDots)
Returns shortened text.

Parameters:
text - text to shorten
maxLength - maximum shortened text lenght
addDots - add dots at the end of the text when shortened
Returns:
shortened text

shortenTextEnd

public static java.lang.String shortenTextEnd(java.lang.String text,
                                              int maxLength,
                                              boolean addDots)
Returns shortened text.

Parameters:
text - text to shorten
maxLength - maximum shortened text lenght
addDots - add dots at the end of the text when shortened
Returns:
shortened text

stringToList

public static java.util.List<java.lang.String> stringToList(java.lang.String string,
                                                            java.lang.String separator)
Returns a list of text parts splitted using specified separator.

Parameters:
string - text to split
separator - text parts separator
Returns:
list of splitted parts

stringToIntList

public static java.util.List<java.lang.Integer> stringToIntList(java.lang.String string,
                                                                java.lang.String separator)
Returns a list of integer parts splitted using specified separator.

Parameters:
string - text to split
separator - text parts separator
Returns:
list of splitted parts

listToString

public static java.lang.String listToString(java.util.List list,
                                            java.lang.String separator)
Returns single text combined using list of strings and specified separator.

Parameters:
list - list to combine into single text
separator - text parts separator
Returns:
single text

listToString

public static <T> java.lang.String listToString(java.util.List<T> list,
                                                java.lang.String separator,
                                                TextProvider<T> textProvider)
Returns single text combined using list of strings and specified separator.

Parameters:
list - list to combine into single text
separator - text parts separator
textProvider - text provider
Returns:
single text

listToString

public static <T> java.lang.String listToString(java.util.List<T> list,
                                                java.lang.String separator,
                                                TextProvider<T> textProvider,
                                                Filter<T> filter)
Returns single text combined using list of strings and specified separator.

Parameters:
list - list to combine into single text
separator - text parts separator
textProvider - text provider
filter - list elements filter
Returns:
single text

enumListToString

public static <E extends java.lang.Enum<E>> java.lang.String enumListToString(java.util.List<E> enumList)
Converts list of enumeration constants into string with list of enumeration constants and returns it.

Type Parameters:
E - enumeration type
Parameters:
enumList - enumeration constants list
Returns:
string with list of enumeration constants

enumStringToList

public static <E extends java.lang.Enum<E>> java.util.List<E> enumStringToList(java.lang.String enumString,
                                                                               java.lang.Class<E> enumClass)
Converts string with list of enumeration constants into real list of enumeration constants and returns it.

Type Parameters:
E - enumeration type
Parameters:
enumString - enumeration constants string list
enumClass - enumeration class
Returns:
list of enumeration constants

unite

public static java.lang.String unite(java.lang.String separator,
                                     java.lang.String... parts)
Converts and returns specified parts which are not null into single string.

Parameters:
separator - separator to place between parts
parts - parts to unite
Returns:
united non-null parts

isEmpty

public static boolean isEmpty(java.lang.String text)
Returns whether specified text is empty or not.

Parameters:
text - text to process
Returns:
true if specified text is empty, false otherwise

createString

public static java.lang.String createString(java.lang.String character,
                                            int length)
Creates new string filled with specified amount of same characters.

Parameters:
character - character to fill string with
length - string length
Returns:
new string filled with specified amount of same characters

replaceAll

public static java.lang.String replaceAll(java.lang.String text,
                                          boolean ignoreCase,
                                          java.lang.String str,
                                          TextProvider<java.lang.String> provider)
Replaces all occurences of str found in the specified text with provided text.

Parameters:
text - text to replace string occurences in
ignoreCase - whether should ignore case while searching for occurences or not
str - text to replace
provider - text replacement
Returns:
text with replaced occurences of the specified str

generateId

public static java.lang.String generateId()
Returns random ID with default prefix and suffix.

Returns:
ID

generateId

public static java.lang.String generateId(java.lang.String prefix)
Returns random ID with specified prefix and default suffix.

Parameters:
prefix - id prefix
Returns:
ID

generateId

public static java.lang.String generateId(java.lang.String prefix,
                                          java.lang.String suffix)
Returns random ID with specified prefix and suffix.

Parameters:
prefix - id prefix
suffix - id suffix
Returns:
ID

generateId

public static java.lang.String generateId(int length)
Returns randomly generated ID part with specified length.

Parameters:
length - part length in symbols
Returns:
ID part

parseDelay

public static long parseDelay(java.lang.String delay)
                       throws DelayFormatException
Either returns delay retrieved from string or throws an exception if it cannot be parsed. Full string format is "Xd Yh Zm s ms" but you can skip any part of it. Yet you must specify atleast one value. For example string "2h 5s" will be a valid delay declaration and will be converted into (2*60*60*1000+5*1000) long value.

Parameters:
delay - string delay
Returns:
delay retrieved from string
Throws:
DelayFormatException

toStringDelay

public static java.lang.String toStringDelay(long delay)
Returns delay string representation. Delay cannot be less or equal to zero.

Parameters:
delay - delay to process
Returns:
delay string representation