igpp.util
Class Text

java.lang.Object
  extended by igpp.util.Text

public class Text
extends Object

Methods to parse, interpret and manipulate strings.

Version:
1.00 2006
Author:
Todd King

Constructor Summary
Text()
           
 
Method Summary
static ArrayList<String> complement(ArrayList<String> listA, ArrayList<String> listB)
          Create an ArrayList of strings containing the complement of two lists.
static String concatPath(String path1, String path2)
          Concatenate two paths ensuring that a file separator exists between each part.
static String concatPath(String path1, String path2, String separator)
          Concatenate two paths ensuring that a file separator exists between each part.
static String getFile(String pathName)
          Get the file name portion of a pathname.
static String getFileBase(String fileName)
          Get the base portion of a file name.
static String getMaskedValue(String mask, String value)
          Returns the non-mask portion of a string.
static ArrayList<String> getMatchList(ArrayList<String> terms, ArrayList<String> list)
          Create an ArrayList of all words in a list that match words in a list.
static ArrayList<String> getMatchList(String[] terms, ArrayList<String> list)
          Create an ArrayList of all words in an array that match words in a list.
static String getPath(String pathName)
          Get the path name portion of a pathname.
static int getToken(String value, String[] list)
          Returns the index of the matching item in a list.
static String getURIPath(String pathName)
          Get the path name portion of a URI.
static String getURLPath(String pathName)
          Get the path name portion of a URL.
static boolean getValue(String value, boolean defaultValue)
          Returns a value if set (non-null) or the default if not set.
static String getValue(String value, String defaultValue)
          Returns a value if set (non-null) or the default if not set.
static String getYesNo(boolean state)
          Return a string representing the boolean state.
static ArrayList<String> intersection(ArrayList<String> listA, ArrayList<String> listB)
          Create an ArrayList of strings containing the intersection of two lists.
static String isChecked(String base, String value)
          Compares a value to a sample string.
static boolean isEmpty(String value)
          Determine if a string is empty or not defined.
static boolean isInList(String term, ArrayList<String> list)
          Check if an item is in a list
static boolean isInList(String term, String[] list)
          Check if an item is in a list
static boolean isInPrefixList(String term, ArrayList<String> list)
          Check if the term starts with any item in the list.
static boolean isInPrefixList(String term, String[] list)
          Check if the term starts with any item in the list.
static boolean isListEmpty(ArrayList<String> list)
          Determine if an ArrayList is empty or not defined.
static boolean isMatch(ArrayList<String> base, ArrayList<String> test)
          Compares a list of strings to another and returns true if they match.
static boolean isMatch(String base, String test)
          Compares one string to another and returns true if they match.
static boolean isSetMatch(ArrayList<String> base, ArrayList<String> test)
          Compares a list of strings to another and returns true if they match.
static boolean isSetMatch(String base, String test)
          Compares one string to another and returns true if they match.
static boolean isTrue(String value)
          Determine if a string is equivalent to a true/false state.
static boolean isTrue(String value, boolean defaultValue)
          Determine is a string is equivalent to a true/false state.
static String makeList(ArrayList<String> list)
          Construct a comma separated list of values from an ArrayList of Strings.
static String makeList(ArrayList<String> list, boolean quoted)
          Construct a comma separated list of values from an ArrayList of Strings.
static String makeList(String[] list)
          Construct a comma separated list of values from an array of Strings.
static String makeList(String[] list, boolean quoted)
          Construct a comma separated list of values from an array of Strings.
static String pad(String base, int max)
          Pad a string with spaces to a fixed width.
static String[] parseList(String text)
          Parse a comma separated list into an array of Strings, trimming extra white space.
static String replaceAll(String regex, String value, String opon)
          Replace patterns in a string and return the result.
static String[] splitMixedCase(String text)
          Divide a string on capital letters that follow lowercase letters
static ArrayList<String> toArrayList(String[] list)
          Convert an String array to an array list of strings.
static long toBytes(String value)
          Convert a string with a unitized to a byte count.
static long toBytes(String bytes, String units)
          Convert a string and units to a byte count.
static double toDouble(String value)
          Converts a string to a double.
static String toImproperCase(String value)
          Convert a string to "improper" case by converting the first letter to lower case.
static int toInt(String value)
          Converts a string to a int.
static String toLowerCase(String value)
          Convert a string to lower case.
static String toProperCase(String value)
          Convert a string to proper case by capitalizing the first letter.
static String toUnitizedBytes(long bytes)
          Converts a count of bytes into a concise count string with units like KB, MB, GB.
static String toUpperCase(String value)
          Convert a string to upper case.
static String trim(String opon)
          Trim the leading and trailing spaces from a string and return the result.
static ArrayList<String> uniqueList(ArrayList<String> list, boolean dropNull)
          Create an ArrayList of strings containing unique values.
static String wordWrap(String text, int width, String prefix)
          Wrap a string of text so that no line is longer than a given width.
static String zeroPad(String base, int max)
          Pad a number with leading zeros to the count given.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Text

public Text()
Method Detail

isEmpty

public static boolean isEmpty(String value)
Determine if a string is empty or not defined.

Parameters:
value - The string to test.
Returns:
true if the string has zero length or is null. false otherwise.

isListEmpty

public static boolean isListEmpty(ArrayList<String> list)
Determine if an ArrayList is empty or not defined.

Parameters:
list - a list of values.
Returns:
true if the list has zero length elements or is null. false otherwise.

getYesNo

public static String getYesNo(boolean state)
Return a string representing the boolean state.

Parameters:
state - A boolean set to true or false.
Returns:
"Yes" for true and "No" for false.

isChecked

public static String isChecked(String base,
                               String value)
Compares a value to a sample string. If they match the String "checked" is returned. Case is ignored. Useful for HTML web form generation.

Parameters:
base - The value to compare to the "value" argument.
value - The value to test.
Returns:
The string "checked" is the values matched. Otherwise a blank string.

getToken

public static int getToken(String value,
                           String[] list)
Returns the index of the matching item in a list.

Parameters:
value - the value to search for in the list.
list - An array of strings to search.
Returns:
-1 if no match is found.

getMaskedValue

public static String getMaskedValue(String mask,
                                    String value)
Returns the non-mask portion of a string. The mask is a simple pattern with a star (*) at the variable location. Only one star (*) is allowed in the pattern.

Parameters:
mask - the mask pattern to use. A star (*) identifies indicates the desired non-mask portion.
value - the value to mask and extract the value from.
Returns:
the non-mask portion of the value, null if no value is found.

getValue

public static String getValue(String value,
                              String defaultValue)
Returns a value if set (non-null) or the default if not set.

Parameters:
value - the preferred value if set.
defaultValue - the default value.
Returns:
value if value is not null, otherwise return the defaultValue.

getValue

public static boolean getValue(String value,
                               boolean defaultValue)
Returns a value if set (non-null) or the default if not set.

Parameters:
value - the text string for a boolean value.
defaultValue - the default value.
Returns:
value if value is not null, otherwise return the defaultValue.

isTrue

public static boolean isTrue(String value)
Determine if a string is equivalent to a true/false state. Supported tokens are y/n, yes/no, true/false, 0/1

Parameters:
value - the value to determine its boolean equivalent.
Returns:
the boolean equivalent or false if value is null.

isTrue

public static boolean isTrue(String value,
                             boolean defaultValue)
Determine is a string is equivalent to a true/false state. Supported tokens are y/n, yes/no, true/false, 0/1

Parameters:
value - the value to determine its boolean equivalent.
defaultValue - the default value.
Returns:
the defaultValue if value is null.

toProperCase

public static String toProperCase(String value)
Convert a string to proper case by capitalizing the first letter.

Parameters:
value - the string to convert to proper case.
Returns:
the string converted to proper case.

toLowerCase

public static String toLowerCase(String value)
Convert a string to lower case. This is a static interface to java.lang.String.toLowerCase()

Parameters:
value - the string to convert to proper case.
Returns:
the string converted to proper case.

toUpperCase

public static String toUpperCase(String value)
Convert a string to upper case. This is a static interface to java.lang.String.toUpperCase()

Parameters:
value - the string to convert to proper case.
Returns:
the string converted to proper case.

toImproperCase

public static String toImproperCase(String value)
Convert a string to "improper" case by converting the first letter to lower case.

Parameters:
value - the string to convert to improper case.
Returns:
the string converted to improper case.

zeroPad

public static String zeroPad(String base,
                             int max)
Pad a number with leading zeros to the count given.

Parameters:
base - the string containing the value.
max - the maximum length for the return string.
Returns:
the base string with zeros added to the beginning so that its length is equal to "max".

pad

public static String pad(String base,
                         int max)
Pad a string with spaces to a fixed width.

Parameters:
base - the string containing the value.
max - the maximum length for the return string.
Returns:
the base string with spaces added to the beginning so that its length is equal to "max".

isSetMatch

public static boolean isSetMatch(String base,
                                 String test)
Compares one string to another and returns true if they match. Case is ignored. Both strings must be defined (non-null).

Parameters:
base - the string to compare to the test value.
test - the value to compare to the base.
Returns:
true if the two strings match, otherwise false

isSetMatch

public static boolean isSetMatch(ArrayList<String> base,
                                 ArrayList<String> test)
Compares a list of strings to another and returns true if they match. Case is ignored. Both arrays must be defined (non-null).

Parameters:
base - the array of strings to compare to the test values.
test - the array of values to compare to the base.
Returns:
true if the two strings match, otherwise false

isMatch

public static boolean isMatch(String base,
                              String test)
Compares one string to another and returns true if they match. Case is ignored. null is considered a wild card.

Parameters:
base - the string to compare to the test value.
test - the value to compare to the base.
Returns:
true if the two strings match, otherwise false

isMatch

public static boolean isMatch(ArrayList<String> base,
                              ArrayList<String> test)
Compares a list of strings to another and returns true if they match. Case is ignored. null is considered a wild card.

Parameters:
base - the array of strings to compare to the test values.
test - the array of values to compare to the base.
Returns:
true if the two strings match, otherwise false

parseList

public static String[] parseList(String text)
Parse a comma separated list into an array of Strings, trimming extra white space.

Parameters:
text - the string to parse.
Returns:
An array of String values.

makeList

public static String makeList(String[] list)
Construct a comma separated list of values from an array of Strings. Values are not quoted.

Parameters:
list - An array of String values.
Returns:
a String containing a comma separated list of values.

makeList

public static String makeList(String[] list,
                              boolean quoted)
Construct a comma separated list of values from an array of Strings. Values can be quoted.

Parameters:
list - An array of String values.
quoted - determined if each value should be enclosed in double quotes (").
Returns:
a String containing a comma separated list of values.

makeList

public static String makeList(ArrayList<String> list)
Construct a comma separated list of values from an ArrayList of Strings. Values are not quoted.

Parameters:
list - An array of String values.
Returns:
A string containing a comma separated list of values.

makeList

public static String makeList(ArrayList<String> list,
                              boolean quoted)
Construct a comma separated list of values from an ArrayList of Strings.

Parameters:
list - An array of String values.
Returns:
A string containing a comma separated list of values.

wordWrap

public static String wordWrap(String text,
                              int width,
                              String prefix)
Wrap a string of text so that no line is longer than a given width. Also prefix each wrapped line with passed text.

Parameters:
text - the text string to word wrap.
width - the maximum width for a line of text.
prefix - text to add at the beginning of each line.
Returns:
a String containing the word wrapped text. Each line is terminated with a new line (\n) character.

uniqueList

public static ArrayList<String> uniqueList(ArrayList<String> list,
                                           boolean dropNull)
Create an ArrayList of strings containing unique values. Optional exclude null strings.

Parameters:
list - the list of values to scan.
dropNull - flag indicating whether null values should be dropped.
Returns:
an ArrayList containing unique values.

intersection

public static ArrayList<String> intersection(ArrayList<String> listA,
                                             ArrayList<String> listB)
Create an ArrayList of strings containing the intersection of two lists.

Parameters:
listA - a list of values to scan.
listB - a list of values to scan.
Returns:
an ArrayList containing those items in both listA and listB.

complement

public static ArrayList<String> complement(ArrayList<String> listA,
                                           ArrayList<String> listB)
Create an ArrayList of strings containing the complement of two lists.

Parameters:
listA - a list of values to scan.
listB - a list of values to scan.
Returns:
an ArrayList containing those items in listA, but not in listB.

isInList

public static boolean isInList(String term,
                               ArrayList<String> list)
Check if an item is in a list

Parameters:
term - the term (phrase) to search for in the list.
list - the ArrayList of Strings to search.
Returns:
true if the term is in the list, false otherwise.

isInList

public static boolean isInList(String term,
                               String[] list)
Check if an item is in a list

Parameters:
term - the term (phrase) to search for in the list.
list - the array of Strings to search.
Returns:
true if the term is in the list, false otherwise.

isInPrefixList

public static boolean isInPrefixList(String term,
                                     String[] list)
Check if the term starts with any item in the list.

Parameters:
term - the term (phrase) to search for in the list.
list - the array of Strings to search.
Returns:
true if the term starts with any item in the list, false otherwise.

isInPrefixList

public static boolean isInPrefixList(String term,
                                     ArrayList<String> list)
Check if the term starts with any item in the list.

Parameters:
term - the term (phrase) to search for in the list.
list - the array of Strings to search.
Returns:
true if the term starts with any item in the list, false otherwise.

getMatchList

public static ArrayList<String> getMatchList(String[] terms,
                                             ArrayList<String> list)
Create an ArrayList of all words in an array that match words in a list.

Parameters:
terms - the array of phrases to search for in the list.
list - the ArrayList of Strings to search.
Returns:
the ArrayList of matching items.

getMatchList

public static ArrayList<String> getMatchList(ArrayList<String> terms,
                                             ArrayList<String> list)
Create an ArrayList of all words in a list that match words in a list.

Parameters:
terms - the ArrayList of phrases to search for in the list.
list - the ArrayList of Strings to search.
Returns:
the ArrayList of matching items.

toArrayList

public static ArrayList<String> toArrayList(String[] list)
Convert an String array to an array list of strings.

Parameters:
list - an array of strings to convert to an ArrayList
Returns:
an ArrayList of Strings.

toBytes

public static long toBytes(String value)
Convert a string with a unitized to a byte count. The string format is "#[.#] units". Supported units are the same as for toBytes(String bytes, String units).

Parameters:
value - a string containing a unitized count.
Returns:
number of bytes.

toBytes

public static long toBytes(String bytes,
                           String units)
Convert a string and units to a byte count. Supported units The string format is "#[.#] units". Supported units are bytes, B, KB, MB, GB, TB, and PB. Case is ignored.

Parameters:
bytes - a string a count.
units - a string the units to apply to the count.
Returns:
number of bytes.

toUnitizedBytes

public static String toUnitizedBytes(long bytes)
Converts a count of bytes into a concise count string with units like KB, MB, GB. Rounds up to second decimal place.

Parameters:
bytes - a count.
Returns:
a formatted string containing the shortest possible value followed with the appropriate units.

getFileBase

public static String getFileBase(String fileName)
Get the base portion of a file name. The extension which is the text that follows the last dot (.) and the dot itself is removed from the name.

Parameters:
fileName - the name of the file to parse.
Returns:
the base name of the file.

getFile

public static String getFile(String pathName)
Get the file name portion of a pathname.

Parameters:
pathName - the path to a file. Full or partial path is permited.
Returns:
the filename portion of the pathname.

getPath

public static String getPath(String pathName)
Get the path name portion of a pathname.

Parameters:
pathName - the path to a file. Full or partial path is permited.
Returns:
the path portion of the pathname.

getURIPath

public static String getURIPath(String pathName)
Get the path name portion of a URI. Leaving the tailing delimiter in place.

Parameters:
pathName - the path to a file. Full or partial path is permited.
Returns:
the path portion of the pathname.

getURLPath

public static String getURLPath(String pathName)
Get the path name portion of a URL.

Parameters:
pathName - the path to a file. Full or partial path is permitted.
Returns:
the path portion of the pathname.

concatPath

public static String concatPath(String path1,
                                String path2,
                                String separator)
Concatenate two paths ensuring that a file separator exists between each part.

Parameters:
path1 - the first part of the path.
path2 - the second part of the path.
separator - the string to place between the first and second part of the path.
Returns:
the joining of the first and second part of the path.

concatPath

public static String concatPath(String path1,
                                String path2)
Concatenate two paths ensuring that a file separator exists between each part. The file separator (File.separator) for the current operating system is used.

Parameters:
path1 - the first part of the path.
path2 - the second part of the path.
Returns:
the joining of the first and second part of the path.

splitMixedCase

public static String[] splitMixedCase(String text)
Divide a string on capital letters that follow lowercase letters

Parameters:
text - the string to split on capital letters.
Returns:
an array of strings containing each word.
Since:
1.0

toInt

public static int toInt(String value)
Converts a string to a int.

Parameters:
value - a string representation of a value.
Returns:
the converted value.
Since:
1.0

toDouble

public static double toDouble(String value)
Converts a string to a double.

Parameters:
value - a string representation of a value.
Returns:
the converted value.
Since:
1.0

replaceAll

public static String replaceAll(String regex,
                                String value,
                                String opon)
Replace patterns in a string and return the result. This is a static interface to java.lang.String.replaceAll()

Parameters:
regex - regular expression for pattern to find.
value - The value to replace each found pattern.
opon - a string to operate on.
Returns:
the string with the pattern replaced with value.
Since:
1.0

trim

public static String trim(String opon)
Trim the leading and trailing spaces from a string and return the result. This is a static interface to java.lang.String.trim()

Parameters:
opon - a string to operate on.
Returns:
the string with the pattern replaced with value.
Since:
1.0


Copyright © 2008-2009 Regents University of California. All Rights Reserved.