org.sandev.basics.util
Class StringUtil

java.lang.Object
  extended byorg.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 this on repeated calls.
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 boolean convertsToDate(java.lang.String val)
          Return true if the given value can be converted to a Date, false otherwise.
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 fixAMPMTimeValues(java.lang.String probtext)
          Assuming AM or PM needs to be specified, fix the problem text.
static java.lang.String fixUnparseableDateStr(java.lang.String probtext)
          Look for known parsing breakages on dates and fix them, returning the corrected text.
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 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.text.NumberFormat getNumberFormat()
          Initializing accessor for numberFormat.
static java.util.Date safeGetDateValue(java.lang.String val)
          Calls getDateValue but returns null rather than throwing if the value cannot be converted.
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 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.
 
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 this on repeated calls.

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.


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.


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

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.


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.


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

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.


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.


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. This method calls fixUnparseableDateStr and retries in the event DateFormat parsing fails.

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.


fixUnparseableDateStr

public static java.lang.String fixUnparseableDateStr(java.lang.String probtext)
Look for known parsing breakages on dates and fix them, returning the corrected text.


fixAMPMTimeValues

public static java.lang.String fixAMPMTimeValues(java.lang.String probtext)
Assuming AM or PM needs to be specified, fix the problem text. This runs after fixUnparseableDateStr was called and the date parsing was re-attempted, so 24 hour clock systems won't call this method unless the given date string was fundamentally broken (in which case trying to add AM/PM won't do anything).


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.