edu.nrao.sss.util
Class StringUtil

java.lang.Object
  extended by edu.nrao.sss.util.StringUtil

public class StringUtil
extends Object

A utility class for manipulating text strings.

Version Info:

$Revision: 1351 $
$Date: 2008-06-13 14:11:55 -0600 (Fri, 13 Jun 2008) $
$Author: dharland $

Since:
2006-02-23
Author:
David M. Harland

Field Summary
static String EOL
          Returns the end-of-line string.
 
Method Summary
 String capitalizeFirstLetterOfEachWord(String str)
          Returns a string based on str where the first letters of each word are converted to upper case.
 String format(double number, double stdBeg, double stdEnd)
          Returns a text representation of number.
 String formatNoScientificNotation(BigDecimal number)
          Returns a text representation of number.
 String formatNoScientificNotation(BigDecimal number, int minFracDigits, int maxFracDigits)
          Returns a text representation of number.
 String formatNoScientificNotation(double number)
          Returns a text representation of number.
 String formatNoScientificNotation(double number, int minFracDigits, int maxFracDigits)
          Returns a text representation of number.
 String fromCollection(Collection<?> source, String separator)
          Returns a string that is the result of calling toString() on every member of source and inserting separator between each member.
 String fromCollection(Collection<?> source, String separator, boolean appendSeparatorToEnd)
          Returns a string that is the result of calling toString() on every member of source and inserting separator between each member.
static StringUtil getInstance()
          Returns an instance of this string utility.
 String getSpacer(int multiplier)
           
 Pattern globToPattern(String glob)
          Calls globToRegex(java.lang.String) to create a regex string, then compiles that regex and returns the resultant pattern.
 String globToRegex(String glob)
          Converts a glob expression to a regex.
 String globToSqlLike(String glob, char escape)
          Converts a glob expression to an SQL LIKE expression.
 String lowerAndCapitalizeFirstLetterOfEachWord(String str)
          Returns a string based on str that is in lower case, except for the first letters of each word, which are in upper case.
 String normalizeString(String str)
          Returns a normalized version of str.
 Collection<String> toCollection(String source, String separator, Collection<String> destination)
          Returns a collection of strings that was built by parsing source, using separator as a delimiter.
 Collection<String> toCollection(String source, String separator, Collection<String> destination, boolean addMissingFinalSeparator)
          Returns a collection of strings that was built by parsing source, using separator as a delimiter.
 String toXmlName(String name, char replacementForIllegalChars)
          Returns a valid XML Name by replacing illegal characters in name with replacementForIllegalChars.
 String toXmlNCName(String name, char replacementForIllegalChars)
          Returns a valid XML non-colonized name by replacing illegal characters in name with replacementForIllegalChars.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EOL

public static final String EOL
Returns the end-of-line string.

Method Detail

getInstance

public static StringUtil getInstance()
Returns an instance of this string utility.

Returns:
a string utility.

normalizeString

public String normalizeString(String str)
Returns a normalized version of str.

Normalization means the following:

  1. If str is null, the empty string ("") is returned.
  2. Otherwise, all leading and trailing whitespace is removed.

Parameters:
str - the string to be normalized.
Returns:
a normalized copy of str.
See Also:
String.trim()

lowerAndCapitalizeFirstLetterOfEachWord

public String lowerAndCapitalizeFirstLetterOfEachWord(String str)
Returns a string based on str that is in lower case, except for the first letters of each word, which are in upper case.

Example:

INPUT:  hANd OvER THe moNeY and NO OnE geTS HURT.
OUTPUT: Hand Over The Money And No One Gets Hurt.

Parameters:
str - the input string.
Returns:
a string where each word is in lower case but begins with a capital letter.
See Also:
capitalizeFirstLetterOfEachWord(String)

capitalizeFirstLetterOfEachWord

public String capitalizeFirstLetterOfEachWord(String str)
Returns a string based on str where the first letters of each word are converted to upper case.

Example:

INPUT:  hANd OvER THe moNeY and NO OnE geTS HURT.
OUTPUT: HANd OvER THe MoNeY And NO OnE GeTS HURT.

Parameters:
str - the input string.
Returns:
a string where each word begins with a capital letter.
See Also:
lowerAndCapitalizeFirstLetterOfEachWord(String)

getSpacer

public String getSpacer(int multiplier)
Returns:
a String containing multiplier consequitive tab characters.

format

public String format(double number,
                     double stdBeg,
                     double stdEnd)
Returns a text representation of number. The returned text is similar to that provided by Double.toString(double), except that the range of numbers where scientific notation is not used is given by the other parameters.

If number is such that

   stdBeg <= number <= stdEnd
 
scientific notation will not be used.

Parameters:
number - the number for which a text representation is needed.
stdBeg - the minimum absolute value where regular notation should be preferred to scientific notation. This should be a non-negative number.
stdEnd - the maximum absolute value where regular notation should be preferred to scientific notation. This should be a non-negative number.
Returns:
a text representation of number.

formatNoScientificNotation

public String formatNoScientificNotation(double number)
Returns a text representation of number. The returned text is similar to that provided by Double.toString(double), except that scientific notation is never used.

Parameters:
number - the number for which a text representation is needed.
Returns:
a text representation of number.

formatNoScientificNotation

public String formatNoScientificNotation(double number,
                                         int minFracDigits,
                                         int maxFracDigits)
Returns a text representation of number. The returned text is similar to that provided by Double.toString(double), except that scientific notation is never used.

Parameters:
number - the number for which a text representation is needed.
minFracDigits - the minimum number of digits after the decimal point.
maxFracDigits - the maximum number of digits after the decimal point.
Returns:
a text representation of number.

formatNoScientificNotation

public String formatNoScientificNotation(BigDecimal number)
Returns a text representation of number. There will always be at least one digit after the decimal point.

If number is determined by MathUtil.doubleValueIsInfinite(BigDecimal) to be infinite, the returned text will be either "+infinity" or "-infinity".

Parameters:
number - the number for which a text representation is needed.
Returns:
a text representation of number.

formatNoScientificNotation

public String formatNoScientificNotation(BigDecimal number,
                                         int minFracDigits,
                                         int maxFracDigits)
Returns a text representation of number.

Parameters:
number - the number for which a text representation is needed.
minFracDigits - the minimum number of digits after the decimal point.
maxFracDigits - the maximum number of digits after the decimal point.
Returns:
a text representation of number.

fromCollection

public String fromCollection(Collection<?> source,
                             String separator)
Returns a string that is the result of calling toString() on every member of source and inserting separator between each member. A null member will be represented by the empty string.

The returned string does not contain a trailing separator (unless the final member of source was the empty string or ended with the separator). This is a convenience method that is equivalent to calling fromCollection(source, separator, false).

It is the client's duty to select a separator that will not be present in the string representation of any of the members of the source collection.

Parameters:
source - a collection of objects that will be turned into a single string.
separator - the text used to separate one member of the collection from another.
Returns:
a string representation of source. If the collection is empty, the empty string ("") is returned.

fromCollection

public String fromCollection(Collection<?> source,
                             String separator,
                             boolean appendSeparatorToEnd)
Returns a string that is the result of calling toString() on every member of source and inserting separator between each member. A null member will be represented by the empty string.

It is the client's duty to select a separator that will not be present in the string representation of any of the members of the source collection.

Parameters:
source - a collection of objects that will be turned into a single string.
separator - the text used to separate one member of the collection from another.
appendSeparatorToEnd - if true, this method adds a final separator at the end of the returned string.
Returns:
a string representation of source. If the collection is empty, the empty string ("") is returned.

toCollection

public Collection<String> toCollection(String source,
                                       String separator,
                                       Collection<String> destination)
Returns a collection of strings that was built by parsing source, using separator as a delimiter.

Note: the separator is not interpreted as a regular expression, but just as a simple string. This allows a client to call toString and fromString with the same delimiter and obtain sensible results. Also, a final delimiter is assumed, if not already present. That is, using ";" as a delimiter, the following text would result in the set of integers from one through five: 1;2;3;4;5. Consecutive delimiters will result in empty-string ("") elements. This is a convenience method that is equivalent to calling toCollection(source, separator, desination, true).

Parameters:
source - a string representation of a collection.
separator - text that separates one member of a collection from another in the source text.
destination - the collection into which the members created from source are added. If this parameter is null, a new collection will be created and used.
Returns:
a collection of strings built by parsing source, using separator as a delimiter.

toCollection

public Collection<String> toCollection(String source,
                                       String separator,
                                       Collection<String> destination,
                                       boolean addMissingFinalSeparator)
Returns a collection of strings that was built by parsing source, using separator as a delimiter.

Note: the separator is not interpreted as a regular expression, but just as a simple string. This allows a client to call toString and fromString with the same delimiter and obtain sensible results. Consecutive delimiters will result in empty-string ("") elements.

Parameters:
source - a string representation of a collection.
separator - text that separates one member of a collection from another in the source text.
destination - the collection into which the members created from source are added. If this parameter is null, a new collection will be created and used.
addMissingFinalSeparator - if true, any characters in source between the final separator and the end of the string will be treated as the final string added to destination. If false, any characters after the final separator are discarded.
Returns:
a collection of strings built by parsing source, using separator as a delimiter.

toXmlName

public String toXmlName(String name,
                        char replacementForIllegalChars)
Returns a valid XML Name by replacing illegal characters in name with replacementForIllegalChars. If there are no illegal characters, name is returned unaltered. If the first character of name is normally a legal value, but is not legal as the first character in an XML Name, the replacement character is inserted prior to the first character in name.

Parameters:
replacementForIllegalChars - a character to use as a replacement for illegal characters in name.
Returns:
a valid XML name based on name.

toXmlNCName

public String toXmlNCName(String name,
                          char replacementForIllegalChars)
Returns a valid XML non-colonized name by replacing illegal characters in name with replacementForIllegalChars. If there are no illegal characters, name is returned unaltered. If the first character of name is normally a legal value, but is not legal as the first character in an XML NCName, the replacement character is inserted prior to the first character in name.

XML Schema's ID and IDREF types are subtypes of NCName. That is, any string used as an ID must be a legal NCName.

Parameters:
replacementForIllegalChars - a character to use as a replacement for illegal characters in name.
Returns:
a valid XML name based on name.

globToSqlLike

public String globToSqlLike(String glob,
                            char escape)
Converts a glob expression to an SQL LIKE expression. For the purposes of this method, globs have 2 special operators:


globToRegex

public String globToRegex(String glob)
Converts a glob expression to a regex. For the purposes of this method, globs have 2 special operators: Note also that the returned regex will only match a whole string. (i.e. it starts with '^' and ends with '$')

Returns:
an regular expression equivalent to the glob glob.

globToPattern

public Pattern globToPattern(String glob)
Calls globToRegex(java.lang.String) to create a regex string, then compiles that regex and returns the resultant pattern. The Pattern has the CASE_INSENSITIVE and UNICODE_CASE flags set.

Throws:
RuntimeException - if the value returned by globToRegex is not a valid regex. Note: this should never happen but may if there's a programmer error in globToRegex.


Copyright © 2009. All Rights Reserved.