org.sandev.basics.util
Class StringUtil

java.lang.Object
  extended by org.sandev.basics.util.StringUtil

public class StringUtil
extends java.lang.Object

Utilities for String value manipulation. These aren't strictly necessary but they make coding easier.

Number conversion:

When converting from a string to a number, you really just want two methods: one to tell you if it can be converted (so you can do a nice error if it's not going to work), and another to do the conversion in an intelligent way. Writing the same code blocks repeatedly for this is annoying, so we have the methods factored here. That leaves all the ParseException, NumberFormatException, ClassCastException stuff from cluttering up the application code.


Field Summary
protected static java.text.DateFormat dateFormat
          Our DateFormat instance so we don't have to keep retrieving a new one for every date field translation call.
protected static java.text.NumberFormat numberFormat
          Our NumberFormat instance so we don't have to keep retrieving this on repeated calls.
 
Constructor Summary
StringUtil()
           
 
Method Summary
static java.lang.String[] atomize(java.lang.String text)
          Split up the given text into individual words.
static java.lang.String capitalize(java.lang.String str)
          Capitalize the given text and return the result.
static java.lang.String capwords(java.lang.String str)
          Capitalize all words in the given text.
static java.lang.String convertSlashFormat(java.lang.String probtext, java.text.DateFormat df)
          If the date pattern we are using is not slash separated, and we got a string with slashes in it, then reformat it.
static boolean convertsToDate(java.lang.String val)
          Return true if the given value can be converted to a Date, false otherwise.
static boolean convertsToDate(java.lang.String val, java.lang.String format, java.lang.String tzid)
          Like the standard convertsToDate but uses the given format string for parsing.
static boolean convertsToDouble(java.lang.String val)
          Return true if the given value can be converted to a long, false otherwise.
static boolean convertsToInt(java.lang.String val)
          Return true if the given value can be converted to an int, false otherwise.
static boolean convertsToLong(java.lang.String val)
          Return true if the given value can be converted to a long, false otherwise.
static java.lang.String dateToString(java.util.Date date)
          Return the String value of the given Date.
static java.lang.String dateToString(java.util.Date date, java.lang.String format, java.lang.String tzid)
          Like the single parameter dateToString but uses the given format to convert the date to a String.
static void debugDumpMonthNames(java.lang.String[] months)
          Write out the given months array for debug purposes.
protected static void debugout(java.lang.String text)
          Debug output informational method.
static java.lang.String fix24HourTimeEntry(java.lang.String probtext, java.text.DateFormat df)
          If we are on 12 hour time and the time was entered in 24 hour format, then convert it.
static java.lang.String fixAMPMSpecification(java.lang.String probtext, java.text.DateFormat df)
          Assuming AM or PM needs to be specified and was not, then append the appropriate value.
static java.lang.String fixCommaWithNoSpace(java.lang.String probtext, java.text.DateFormat df)
          If the date pattern we are using has a comma followed by a space, and what we are reading forgot the space, then add it back in.
static java.lang.String fixNoTimeSpecified(java.lang.String probtext)
          If no time was specified, then return a default time of 12:00.
static java.lang.String fixSecondsIgnored(java.lang.String probtext, java.text.DateFormat df)
          If the format specifies no seconds, and seconds are given then trim them from the string.
static java.lang.String fixSecondsRequired(java.lang.String probtext, java.text.DateFormat df)
          If seconds are required, but not specified, then add zero seconds to the time spec.
static java.lang.String fixSpuriousLeadingZeroForHour(java.lang.String probtext, java.text.DateFormat df)
          If the format specifies the base hour without a leading zero, and we have a leading zero, then get rid of it.
static java.text.DateFormat getDateFormat()
          Initializing accessor for dateFormat.
static java.util.Date getDateValue(java.lang.String val)
          Convert the given String to a Date value.
static java.util.Date getDateValue(java.lang.String val, java.lang.String format, java.lang.String timezoneID)
          Workhorse for single parameter getDateValue that can take a separate format specification.
static java.lang.String getDecimalValue(double val, int decimals, boolean sep)
          Convert the given double value to a text representation with the given number of decimals.
static java.lang.String getDecimalValue(int val, int decimals)
          Given a decimalized integer, and the decimal precision, return a string representation of the decimal value.
static double getDoubleValue(java.lang.String val)
          Convert the given String to a double value.
static int getIntValue(java.lang.String val)
          Convert the given String to an int value.
static long getLongValue(java.lang.String val)
          Convert the given String to a long value.
static java.lang.String[] getMonthNames(java.text.SimpleDateFormat dateformat, java.lang.String monthformat)
          Return an array of the month names as specified by the given DateFormat pattern.
static java.text.NumberFormat getNumberFormat()
          Initializing accessor for numberFormat.
static char getRandomChar(java.util.Random random)
          Return the next random character from the random sequence generator.
static java.lang.String getRandomText(int length)
          Return a random string of the specified length.
static java.lang.String guessMonth(java.lang.String probtext, java.text.DateFormat df)
          Guess the month specification from what was entered and convert it to the canonical format.
static java.lang.String localeToString(java.util.Locale loc)
          Transform the given Locale to a String.
static java.lang.String mailify(java.lang.String source)
          Attempt to convert the given source text escaping any characters that would cause problems if used literally within a mailto link.
static java.lang.String quoteString(java.lang.String source)
          Return the given string with double quotes around it.
static java.lang.String reconstitute(java.lang.String[] atoms)
          Reconstitute the given atom array back into a single string.
static java.lang.String replaceAll(java.lang.String source, java.lang.String tag, java.lang.String repl)
          Works like replaceFirst except it does all instances.
static java.lang.String replaceFirst(java.lang.String source, java.lang.String tag, java.lang.String repl)
          Replace the first instance of tag in source with repl.
static java.util.Date safeGetDateValue(java.lang.String val)
          Calls getDateValue but returns null rather than throwing if the value cannot be converted.
static java.util.Date safeGetDateValue(java.lang.String val, java.lang.String format, java.lang.String tzid)
          Same as the single parameter safeGetDateValue but using the specified format string.
static double safeGetDoubleValue(java.lang.String val)
          Calls getDoubleValue but returns 0 rather than throwing if the value can't be converted.
static int safeGetIntValue(java.lang.String val)
          Calls getIntValue but returns 0 rather than throwing if the value can't be converted.
static long safeGetLongValue(java.lang.String val)
          Calls getLongValue but returns 0 rather than throwing if the value can't be converted.
static java.util.Date sandGetDateValue(java.lang.String val)
          Wraps getDateValue and throws a SandException if anything goes wrong.
static java.util.Date sandGetDateValue(java.lang.String val, java.lang.String format)
          Same as the single parameter sandGetDateValue but using the specified format string.
static double sandGetDoubleValue(java.lang.String val)
          Wraps getDoubleValue and throws a SandException if anything goes wrong.
static int sandGetIntValue(java.lang.String val)
          Wraps getIntValue and throws a SandException if anything goes wrong.
static long sandGetLongValue(java.lang.String val)
          Wraps getLongValue and throws a SandException if anything goes wrong.
static java.util.Locale stringToLocale(java.lang.String locstr)
          Reconstitute the Locale previously converted to a String via the localeToString method.
static java.lang.String uncapitalize(java.lang.String str)
          Uncapitalize the given text and return the result.
static java.lang.String uncapwords(java.lang.String str)
          Uncapitalize all words in the given text.
static java.lang.String unquoteString(java.lang.String source)
          String the containing double quotes from the given string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

numberFormat

protected static java.text.NumberFormat numberFormat
Our NumberFormat instance so we don't have to keep retrieving this on repeated calls.


dateFormat

protected static java.text.DateFormat dateFormat
Our DateFormat instance so we don't have to keep retrieving a new one for every date field translation call.

Constructor Detail

StringUtil

public StringUtil()
Method Detail

getNumberFormat

public static java.text.NumberFormat getNumberFormat()
Initializing accessor for numberFormat.


getDateFormat

public static java.text.DateFormat getDateFormat()
Initializing accessor for dateFormat. Not setting the lenient flag in the DateFormat because it causes unpredictable results like rolling over 14:30 into 2:30 AM the next day rather than just reading the 24 hour time spec.


convertsToInt

public static boolean convertsToInt(java.lang.String val)
Return true if the given value can be converted to an int, false otherwise. If this returns true then you won't get an exception from calling getIntValue.


getIntValue

public static int getIntValue(java.lang.String val)
                       throws java.text.ParseException
Convert the given String to an int value.

Throws:
java.text.ParseException

safeGetIntValue

public static int safeGetIntValue(java.lang.String val)
Calls getIntValue but returns 0 rather than throwing if the value can't be converted. Exceptions thrown typically include ParseException or NumberFormatException.


sandGetIntValue

public static int sandGetIntValue(java.lang.String val)
                           throws SandException
Wraps getIntValue and throws a SandException if anything goes wrong. Saves an extra catch block for the caller.

Throws:
SandException

getDecimalValue

public static java.lang.String getDecimalValue(int val,
                                               int decimals)
Given a decimalized integer, and the decimal precision, return a string representation of the decimal value.


convertsToLong

public static boolean convertsToLong(java.lang.String val)
Return true if the given value can be converted to a long, false otherwise. If this returns true then you won't get an exception from calling getLongValue.


getLongValue

public static long getLongValue(java.lang.String val)
                         throws java.text.ParseException
Convert the given String to a long value.

Throws:
java.text.ParseException

safeGetLongValue

public static long safeGetLongValue(java.lang.String val)
Calls getLongValue but returns 0 rather than throwing if the value can't be converted. Exceptions thrown typically include ParseException or NumberFormatException.


sandGetLongValue

public static long sandGetLongValue(java.lang.String val)
                             throws SandException
Wraps getLongValue and throws a SandException if anything goes wrong. Saves an extra catch block for the caller.

Throws:
SandException

convertsToDouble

public static boolean convertsToDouble(java.lang.String val)
Return true if the given value can be converted to a long, false otherwise. If this returns true then you won't get an exception from calling getDoubleValue.


getDoubleValue

public static double getDoubleValue(java.lang.String val)
                             throws java.text.ParseException
Convert the given String to a double value.

Throws:
java.text.ParseException

safeGetDoubleValue

public static double safeGetDoubleValue(java.lang.String val)
Calls getDoubleValue but returns 0 rather than throwing if the value can't be converted. Exceptions thrown typically include ParseException or NumberFormatException.


sandGetDoubleValue

public static double sandGetDoubleValue(java.lang.String val)
                                 throws SandException
Wraps getDoubleValue and throws a SandException if anything goes wrong. Saves an extra catch block for the caller.

Throws:
SandException

getDecimalValue

public static java.lang.String getDecimalValue(double val,
                                               int decimals,
                                               boolean sep)
Convert the given double value to a text representation with the given number of decimals. If sep is true, then include thousands separator characters as applicable based on the default locale.


dateToString

public static java.lang.String dateToString(java.util.Date date)
Return the String value of the given Date. If you are reconstructing a Date value from the String using DateFormat, then you should also convert it to a String using the same DateFormat. This method allows for this.

A null Date value returns the empty string.


dateToString

public static java.lang.String dateToString(java.util.Date date,
                                            java.lang.String format,
                                            java.lang.String tzid)
Like the single parameter dateToString but uses the given format to convert the date to a String. If the format is null or empty then it is ignored. If a format is specified, then the timezone ID is used if provided.

A null Date value returns the empty string.


convertsToDate

public static boolean convertsToDate(java.lang.String val)
Return true if the given value can be converted to a Date, false otherwise. If this returns true, then you won't get an exception from calling getDateValue.


convertsToDate

public static boolean convertsToDate(java.lang.String val,
                                     java.lang.String format,
                                     java.lang.String tzid)
Like the standard convertsToDate but uses the given format string for parsing. The TimeZone ID may be null.


getDateValue

public static java.util.Date getDateValue(java.lang.String val)
                                   throws java.text.ParseException
Convert the given String to a Date value. If the value given was null or the empty string, then null is returned. If parsing the date fails, try whatever common string manipulations might help to make it work.

This method is synchronized because a DateFormat is not thread safe, so we prevent multiple threads trying to use it at the same time.

Throws:
java.text.ParseException

getDateValue

public static java.util.Date getDateValue(java.lang.String val,
                                          java.lang.String format,
                                          java.lang.String timezoneID)
                                   throws java.text.ParseException
Workhorse for single parameter getDateValue that can take a separate format specification. If a format is specified, then a timezone may also optionally be specified.

Throws:
java.text.ParseException

safeGetDateValue

public static java.util.Date safeGetDateValue(java.lang.String val)
Calls getDateValue but returns null rather than throwing if the value cannot be converted. Exceptions thrown typically include ParseException or NumberFormatException.


safeGetDateValue

public static java.util.Date safeGetDateValue(java.lang.String val,
                                              java.lang.String format,
                                              java.lang.String tzid)
Same as the single parameter safeGetDateValue but using the specified format string. The TimeZone ID may be null.


sandGetDateValue

public static java.util.Date sandGetDateValue(java.lang.String val)
                                       throws SandException
Wraps getDateValue and throws a SandException if anything goes wrong. Saves an extra catch block for the caller.

Throws:
SandException

sandGetDateValue

public static java.util.Date sandGetDateValue(java.lang.String val,
                                              java.lang.String format)
                                       throws SandException
Same as the single parameter sandGetDateValue but using the specified format string.

Throws:
SandException

convertSlashFormat

public static java.lang.String convertSlashFormat(java.lang.String probtext,
                                                  java.text.DateFormat df)
If the date pattern we are using is not slash separated, and we got a string with slashes in it, then reformat it. We assume that what we are fixing starts with the slash expression, so something like "10/20/2006 2:30 PM" or somesuch.


fixCommaWithNoSpace

public static java.lang.String fixCommaWithNoSpace(java.lang.String probtext,
                                                   java.text.DateFormat df)
If the date pattern we are using has a comma followed by a space, and what we are reading forgot the space, then add it back in.


fixNoTimeSpecified

public static java.lang.String fixNoTimeSpecified(java.lang.String probtext)
If no time was specified, then return a default time of 12:00.


fix24HourTimeEntry

public static java.lang.String fix24HourTimeEntry(java.lang.String probtext,
                                                  java.text.DateFormat df)
If we are on 12 hour time and the time was entered in 24 hour format, then convert it.


fixSecondsRequired

public static java.lang.String fixSecondsRequired(java.lang.String probtext,
                                                  java.text.DateFormat df)
If seconds are required, but not specified, then add zero seconds to the time spec.


fixSecondsIgnored

public static java.lang.String fixSecondsIgnored(java.lang.String probtext,
                                                 java.text.DateFormat df)
If the format specifies no seconds, and seconds are given then trim them from the string.


fixSpuriousLeadingZeroForHour

public static java.lang.String fixSpuriousLeadingZeroForHour(java.lang.String probtext,
                                                             java.text.DateFormat df)
If the format specifies the base hour without a leading zero, and we have a leading zero, then get rid of it.


fixAMPMSpecification

public static java.lang.String fixAMPMSpecification(java.lang.String probtext,
                                                    java.text.DateFormat df)
Assuming AM or PM needs to be specified and was not, then append the appropriate value.


guessMonth

public static java.lang.String guessMonth(java.lang.String probtext,
                                          java.text.DateFormat df)
Guess the month specification from what was entered and convert it to the canonical format. So for example if the user entered something like "sept" and we were expecting "sep" then convert appropriately.


getMonthNames

public static java.lang.String[] getMonthNames(java.text.SimpleDateFormat dateformat,
                                               java.lang.String monthformat)
Return an array of the month names as specified by the given DateFormat pattern.


debugDumpMonthNames

public static void debugDumpMonthNames(java.lang.String[] months)
Write out the given months array for debug purposes.


debugout

protected static void debugout(java.lang.String text)
Debug output informational method. For now this just calls println, should probably find a logger instead.


localeToString

public static java.lang.String localeToString(java.util.Locale loc)
Transform the given Locale to a String.


stringToLocale

public static java.util.Locale stringToLocale(java.lang.String locstr)
Reconstitute the Locale previously converted to a String via the localeToString method.


atomize

public static java.lang.String[] atomize(java.lang.String text)
Split up the given text into individual words. Double quotes around words are removed, and the quoted elements returned as a single element.


reconstitute

public static java.lang.String reconstitute(java.lang.String[] atoms)
Reconstitute the given atom array back into a single string.


getRandomText

public static java.lang.String getRandomText(int length)
Return a random string of the specified length. Basically this returns something that qualifies as a java identifier, since that's most generally useful. The first letter will always be lower case.


getRandomChar

public static char getRandomChar(java.util.Random random)
Return the next random character from the random sequence generator.


replaceFirst

public static java.lang.String replaceFirst(java.lang.String source,
                                            java.lang.String tag,
                                            java.lang.String repl)
Replace the first instance of tag in source with repl. This is effectively the same thing as java.lang.String.replaceFirst except it does not make use of java.util.regex in any way. So the tag is interpreted literally, and the repl text can include characters like backslashes or dollar signs that regex would attempt to interpret as lookup tokens. Sometimes that interpretation can be very dangerous if you don't have full control over what is in the replacement text, hence this method.


replaceAll

public static java.lang.String replaceAll(java.lang.String source,
                                          java.lang.String tag,
                                          java.lang.String repl)
Works like replaceFirst except it does all instances. See the comments on that method for details. This method does not call replaceFirst because repl may actually contain tag, which would lead to an infinite loop.


quoteString

public static java.lang.String quoteString(java.lang.String source)
Return the given string with double quotes around it.


unquoteString

public static java.lang.String unquoteString(java.lang.String source)
String the containing double quotes from the given string.


mailify

public static java.lang.String mailify(java.lang.String source)
Attempt to convert the given source text escaping any characters that would cause problems if used literally within a mailto link. Trying to do a minimal replace for readability, uncomment additional translations as needed.


capitalize

public static java.lang.String capitalize(java.lang.String str)
Capitalize the given text and return the result.


uncapitalize

public static java.lang.String uncapitalize(java.lang.String str)
Uncapitalize the given text and return the result.


capwords

public static java.lang.String capwords(java.lang.String str)
Capitalize all words in the given text.


uncapwords

public static java.lang.String uncapwords(java.lang.String str)
Uncapitalize all words in the given text.