public class TextUtils
extends java.lang.Object
Another useful feature is parsing String to time interval. It parses Strings with numerical value and optional time unit suffix (for example string "38s" will be parsed as 38 seconds, "24m" - 24 minutes "4h" - 4 hours, "3d" - 3 days and "45" as 45 milliseconds.) This method may be very useful for parsing time interval properties such as timeouts or waiting periods from configuration files.
Also in this class there is a method that converts String to preserve indentation formatting for html without use of escape characters. It converts a String in such a way that its spaces are not modified by HTML renderer i.e. it replaces regular space characters with non-breaking spaces known as ' ' but they look in your source as regular space ' ' and not as ' ' It also replaces new line character with '<br>'.
Note that this class has a loose dependency on slf4J library. If in the project some other compatible logging library is present (such as Log4J) this class will still work without any ill effects
| Modifier and Type | Field and Description |
|---|---|
protected static java.lang.String |
DAYS_SUFFIX |
protected static java.util.concurrent.TimeUnit |
DEFAULT_TIMEOUT_TIME_UNIT |
protected static java.lang.String |
HOURS_SUFFIX |
protected static java.lang.String |
MINUTES_SUFFIX |
protected static java.lang.String |
SECONDS_SUFFIX |
| Constructor and Description |
|---|
TextUtils() |
| Modifier and Type | Method and Description |
|---|---|
static int |
compareVersions(java.lang.String ver1,
java.lang.String ver2)
This method compares 2 Strings as versions
|
static int |
compareVersions(java.lang.String ver1,
Version ver2)
This method compares a String that represents a version to a Version
|
static int |
compareVersions(Version ver1,
java.lang.String ver2)
This method compares a Version to a String that represents a version
|
static int |
compareVersions(Version ver1,
Version ver2)
This method compares two Version instances
|
static java.lang.String |
formatStringToPreserveIndentationForHtml(java.lang.String rawText)
This method converts a String in such a way that its spaces are not modified by HTML renderer i.e.
|
static java.lang.String |
getRelevantPackage()
This a getter method for global value of relevant package prefix property
|
static java.lang.String |
getStacktrace(java.lang.Throwable e)
This method retrieves a stacktrace from
Throwable as a String in shortened format. |
static java.lang.String |
getStacktrace(java.lang.Throwable e,
boolean cutTBS)
This method retrieves a stacktrace from
Throwable as a String in full or shortened format. |
static java.lang.String |
getStacktrace(java.lang.Throwable e,
boolean cutTBS,
java.lang.String relevantPackage)
This method retrieves a stacktrace from
Throwable as a String in full or shortened format. |
static java.lang.String |
getStacktrace(java.lang.Throwable e,
java.lang.String relevantPackage)
This method retrieves a stacktrace from
Throwable as a String in shortened format. |
static java.lang.Byte |
parseStringToByte(java.lang.CharSequence num,
byte defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
This method parses a String to Numeric value.
|
static double |
parseStringToDouble(java.lang.CharSequence num,
double defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
This method parses a String to Numeric value.
|
static float |
parseStringToFloat(java.lang.CharSequence num,
float defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
This method parses a String to Numeric value.
|
static int |
parseStringToInt(java.lang.CharSequence num,
int defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
This method parses a String to its Numeric value.
|
static long |
parseStringToLong(java.lang.CharSequence num,
long defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
This method parses a String to Numeric value.
|
static java.lang.Short |
parseStringToShort(java.lang.CharSequence num,
short defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
This method parses a String to Numeric value.
|
static TimeInterval |
parsingStringToTimeInterval(java.lang.String valueStr)
This method parses String value into
TimeInterval. |
static void |
setRelevantPackage(java.lang.String relevantPackage)
This is a setter method for relevant package prefix property for method
getStacktrace(Throwable, boolean) Once the value has been set
the convenience method getStacktrace(Throwable, boolean) could be used instead of method
getStacktrace(Throwable, boolean, String) |
protected static final java.util.concurrent.TimeUnit DEFAULT_TIMEOUT_TIME_UNIT
protected static final java.lang.String SECONDS_SUFFIX
protected static final java.lang.String MINUTES_SUFFIX
protected static final java.lang.String HOURS_SUFFIX
protected static final java.lang.String DAYS_SUFFIX
public static int compareVersions(java.lang.String ver1,
java.lang.String ver2)
throws InvalidVersionFormatException
ver1 - String that contains version for comparisonver2 - String that contains version for comparisonInvalidVersionFormatException - if any of the String instances is not a valid Versionpublic static int compareVersions(Version ver1, java.lang.String ver2) throws InvalidVersionFormatException
ver1 - Version for comparisonver2 - String that contains version for comparisonInvalidVersionFormatException - if the String parameter is not a valid Versionpublic static int compareVersions(java.lang.String ver1,
Version ver2)
throws InvalidVersionFormatException
ver1 - String that contains version for comparisonver2 - Version for comparisonInvalidVersionFormatException - if the String parameter is not a valid Versionpublic static int compareVersions(Version ver1, Version ver2)
ver1 - Version for comparisonver2 - Version for comparisonInvalidVersionFormatException - if the String parameter is not a valid Versionpublic static int parseStringToInt(java.lang.CharSequence num,
int defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
nullOrEmptyStringErrorMessage or numberFormatErrorMessage parameters are null or empty
Strings than the correlating error will not be printed.num - CharSequence to be parseddefaultValue - value that will be returned by this method if parsing of the String failednullOrEmptyStringErrorMessage - String that holds an error message that will printed into log if parameter num is null or blanknumberFormatErrorMessage - String that holds an error message that will printed into log if parameter num is not in appropriate formatpublic static float parseStringToFloat(java.lang.CharSequence num,
float defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
nullOrEmptyStringErrorMessage or numberFormatErrorMessage parameters are null or empty Strings than the
correlating error will not be printed.num - CharSequence to be parseddefaultValue - value that will be returned by this method if parsing of the String failednullOrEmptyStringErrorMessage - String that holds an error message that will printed into log if parameter num is null or blanknumberFormatErrorMessage - String that holds an error message that will printed into log if parameter num is not in appropriate formatpublic static java.lang.Byte parseStringToByte(java.lang.CharSequence num,
byte defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
nullOrEmptyStringErrorMessage or numberFormatErrorMessage parameters are null or empty Strings than the
correlating error will not be printed.num - CharSequence to be parseddefaultValue - value that will be returned by this method if parsing of the String failednullOrEmptyStringErrorMessage - String that holds an error message that will printed into log if parameter num is null or blanknumberFormatErrorMessage - String that holds an error message that will printed into log if parameter num is not in appropriate formatpublic static double parseStringToDouble(java.lang.CharSequence num,
double defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
nullOrEmptyStringErrorMessage or numberFormatErrorMessage parameters are null or empty Strings than the
correlating error will not be printed.num - CharSequence to be parseddefaultValue - value that will be returned by this method if parsing of the String failednullOrEmptyStringErrorMessage - String that holds an error message that will printed into log if parameter num is null or blanknumberFormatErrorMessage - String that holds an error message that will printed into log if parameter num is not in appropriate formatpublic static long parseStringToLong(java.lang.CharSequence num,
long defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
nullOrEmptyStringErrorMessage or numberFormatErrorMessage parameters are null or empty Strings than the
correlating error will not be printed.num - CharSequence to be parseddefaultValue - value that will be returned by this method if parsing of the String failednullOrEmptyStringErrorMessage - String that holds an error message that will printed into log if parameter num is null or blanknumberFormatErrorMessage - String that holds an error message that will printed into log if parameter num is not in appropriate formatpublic static java.lang.Short parseStringToShort(java.lang.CharSequence num,
short defaultValue,
java.lang.String nullOrEmptyStringErrorMessage,
java.lang.String numberFormatErrorMessage)
nullOrEmptyStringErrorMessage or numberFormatErrorMessage parameters are null or empty Strings than the
correlating error will not be printed.num - CharSequence to be parseddefaultValue - value that will be returned by this method if parsing of the String failednullOrEmptyStringErrorMessage - String that holds an error message that will printed into log if parameter num is null or blanknumberFormatErrorMessage - String that holds an error message that will printed into log if parameter num is not in appropriate formatpublic static TimeInterval parsingStringToTimeInterval(java.lang.String valueStr) throws java.lang.IllegalArgumentException
TimeInterval. This method supports time interval suffixes "s"
for seconds, "m" for minutes, "h" for hours, and "d" for days. Suffix is case insensitive.
If String parameter contains no suffix the default is milliseconds. So for example string "38s" will be parsed
as 38 seconds, "24m" - 24 minutes "4h" - 4 hours, "3d" - 3 days and "45" as 45 milliseconds. If the string parses
to a negative numerical value or 0 or the string is not a valid numerical value then IllegalArgumentException
is thrown. Note that it is very convenient to extract time value from TimeInterval, See methods
TimeInterval.toMillis(), TimeInterval.toSeconds(), TimeInterval.toMinutes(),
TimeInterval.toHours(), TimeInterval.toDays().
methodInvokingInterval property that you need to set for 5 days. So in order to
set the miliseconds value you will need to calculate that 5 days is 432000000 milliseconds (obviously not an
impossible task but annoying and error prone) and then anyone else who sees the value 432000000 will have to
calculate it back to 5 days which is frustrating. But using this method you will have a property value set to
"5d" and invoking the code
long milliseconds = TextUtils.parsingStringToTimeInterval("5d").toMillis();
valueStr - String value to parse to TimeIntervalTimeInterval parsed from the Stringjava.lang.IllegalArgumentException - if parsed value has invalid suffix, invalid numeric value or negative numeric value or 0public static java.lang.String getStacktrace(java.lang.Throwable e,
boolean cutTBS,
java.lang.String relevantPackage)
Throwable as a String in full or shortened format. Shortened format skips the lines in the
stacktrace that do not start with a configurable package prefix and replaces them with "..." line. The stacktrace is viewed as consisting
possibly of several parts. If stacktrace contains "caused by" or "Suppressed" section, each such section for the purposes of
this utility is called "Singular stacktrace". For example the stacktrace bellow contains 2 singular stacktraces: First is 4 top lines and the
second starting from the line "Caused by: ..." and to the end."com.plain.analytics.v2.utils.test." which means that the lines starting with
that prefix are the important or "relevant" lines. (Also the parameter cutTBS set to true which means that stacktrace should be
shortened at all. In this case the result of this method should be as follows:setRelevantPackage(String) and getRelevantPackage()). Using method
setRelevantPackage(String) to set the value will preset the value of relevant package prefix for all calls for which parameter
relevantPackage is null. In fact there is a convinience method getStacktrace(Throwable, boolean) that invokes this method with
parameter relevantPackage set to null and relies on the globally set property through method setRelevantPackage(String).
However if the global property was not set and parameter relevantPackage was left null then the method will return stacktrace in full as
if the parameter cutTBS was set to falsee - Throwable from which stacktrace should be retrievedcutTBS - boolean that specifies if stacktrace should be shortened. The stacktrace should be shortened if this flag is set to true.
Note that if this parameter set to false the stacktrace will be printed in full and parameter relevantPackage becomes
irrelevant.relevantPackage - String that contains the prefix specifying which lines are relevant. It is recommended to be in the following format
"packag_name1.[package_name2.[...]]." In the example above it should be "com.plain.analytics.v2.utils.test.".public static java.lang.String getStacktrace(java.lang.Throwable e,
boolean cutTBS)
Throwable as a String in full or shortened format. This is convenience method that invokes
method getStacktrace(Throwable, boolean, String) with last parameter as null. It relies on relevant package prefix to have
been set by method setRelevantPackage(String). There are several ways to pre-invoke method setRelevantPackage(String):setRelevantPackage(String) which will appropriately initialize the package prefix and enable the use of this method
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="com.mgnt.utils.TextUtils"/>
<property name="targetMethod" value="setRelevantPackage"/>
<property name="arguments" value="com.plain."/>
</bean>
e - Throwable from which stacktrace should be retrievedcutTBS - boolean flag that specifies if stacktrace should be shortened or not. It is shortened if the flag value is truegetStacktrace(Throwable, boolean, String)public static java.lang.String getStacktrace(java.lang.Throwable e)
Throwable as a String in shortened format. This is convenience method that invokes method
getStacktrace(Throwable, boolean, String) with second parameter set to 'true' and last parameter as null. It relies on
relevant package prefix to have been set by method setRelevantPackage(String). There are several ways to pre-invoke method setRelevantPackage(String):setRelevantPackage(String) which will appropriately initialize the package prefix and enable the use of this method
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="com.mgnt.utils.TextUtils"/>
<property name="targetMethod" value="setRelevantPackage"/>
<property name="arguments" value="com.plain."/>
</bean>
e - Throwable from which stacktrace should be retrievedgetStacktrace(Throwable, boolean, String)public static java.lang.String getStacktrace(java.lang.Throwable e,
java.lang.String relevantPackage)
Throwable as a String in shortened format. This is convenience method that invokes method
getStacktrace(Throwable, boolean, String) with second parameter set to 'true'.e - Throwable from which stacktrace should be retrievedrelevantPackage - String that contains the prefix specifying which lines are relevant. It is recommended to be in the following format
"packag_name1.[package_name2.[...]]."getStacktrace(Throwable, boolean, String)public static java.lang.String getRelevantPackage()
public static void setRelevantPackage(java.lang.String relevantPackage)
getStacktrace(Throwable, boolean) Once the value has been set
the convenience method getStacktrace(Throwable, boolean) could be used instead of method
getStacktrace(Throwable, boolean, String)relevantPackage - String that contains the prefix specifying which lines are relevant. It is recommended to be in the following format
"packag_name1.[package_name2.[...]]."getStacktrace(Throwable, boolean, String)public static java.lang.String formatStringToPreserveIndentationForHtml(java.lang.String rawText)
This is non-indented line
This is 2 spaces indented line
This is 4 spaces indented line
This is non-indented line This is 2 spaces indented line This is 4 spaces indented line
This is non-indented line<br>
This is 2 spaces indented line<br>
This is 4 spaces indented line
This is non-indented line
This is 2 spaces indented line
This is 4 spaces indented line
This is non-indented line<br>
This is 2 spaces indented line<br>
This is 4 spaces indented line<br>
rawText - to be converted