Package org.dlese.dpc.util
Class Utils
java.lang.Object
org.dlese.dpc.util.Utils
This class holds a number of handy static methods for generating unique ids, random numbers within a given
range, parsing dates, executing command-line processes, and others.
- Author:
- John Weatherley
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final booleanReturns true if the object in the first parameter contains the Object in the second parameter according to the Objects equals method.static final StringconvertDateToString(Date date, String dateFormat) Converts a Java Date that into a formatted String.static final DateconvertLongToDate(long milliseconds) Converts a long representation of time to a Date.static StringconvertMillisecondsToTime(long milliseconds) Converts a long that represents time in milliseconds to a String that displays the time in minutes and seconds.static final DateconvertStringToDate(String dateString, String dateFormat) Converts a String that contains a recognizable date/time to a Java Date object.static final StringencodeToSearchTerm(String string) Encodes a String to a single term for searching over fields that have been indexed encoded.static final StringencodeToSearchTerms(String string) Encodes a String to a String for searching over fields that have been indexed encoded.static StringgetDayOfWeekString(int dayOfWeek) Gets a String representation of the Calendar.DAY_OF_WEEK field.static final StringgetLexicalDateString(String dateString) Converts a date String of the form YYYY-mm-dd, YYYY-mm, YYYY or yyyy-MM-ddTHH:mm:ssZ to a searchable Lucene (v2.x) lexical date String of the form 'yyyyMMddHHmmss', or null if unable to parse the date String.static MapgetPropertiesMap(String propertiesString) Gets the Map from a String in the Java properties format of the form property=value one per line.static final ResourceBundlegetPropertiesResourceBundle(String propsFileName) Gets a ResourceBundle from a properties file that is in a Jar file or class path within this application's runtime environment.static StringgetRandomAlphaString(int length) Generates a random alpha string of the given length.static StringgetRandomCharsString(int length) Generates a random string containing extended chars of the given lengthstatic intgetRandomIntBetween(int low, int high) Generates a random integer greater-than or equal to low and less-than high.static longGets a global system unique ID.static final DateluceneStringToDate(String dateString) Converts a Lucene String-encoded date to a Date Object.static MapPuts items in a Map, creating a new TreeMap if null is passed in for the myMap argument, otherwise updating the Map with the key/value pair.static MapmapSortByValue(Map myMap, String key, String value, String ascending) Puts items in a Map, creating a new Map if null is passed in for the myMap argument, otherwise updating the Map with the key/value pair.static final booleanTells whether or not the source string matches the given regular expression.static final StringInspects an objects type and methods to standard out.static voidPrints the char values of all chars in range 0 to 256.static voidprintElapsedTime(String msg, Date start, Date end) Print the elapsed time that occured beween two points of time as recorded in java Date objects.static final voidSends the string to System-err-println.static final void* Sends the string to System-out-println.static final StringRedirectEncoder(String text) Encodes a string used in the URL sent to the redirect server and ensures it does not contain problematic characters for the Apache 1 mod_redirect rules (the character sequence %2F is replaced with /).static final StringreplaceAll(String source, String regEx, String replacement) Replaces each substring of this string that matches the given regular expression with the given replacement.static final StringreplaceFirst(String source, String regEx, String replacement) Replaces the first substring of this string that matches the given regular expression with the given replacement.static intrunCommand(String command, StringBuffer standardOutput, StringBuffer errorOutput) Runs the given command-line command.static final String[]Splits this string around matches of the given regular expression.static final StringURLEncoder(String text) Encodes a String for use in a URL using UTF-8 character encoding.
-
Constructor Details
-
Utils
public Utils()
-
-
Method Details
-
convertStringToDate
public static final Date convertStringToDate(String dateString, String dateFormat) throws ParseException Converts a String that contains a recognizable date/time to a Java Date object. SeeSimpleDateFormatfor syntax information for the format String. For example a valid date String might look like '2003-04-27MST' with a date format specifier of 'yyyy-MM-ddz' where yyyy indicates the year, MM the month, dd the day and z the general time zone (GMT, MST, PST, etc).- Parameters:
dateString- A String that contains a recognizable date/time in it, for example '2003-04-27MST'.dateFormat- The format of the date String as specified inSimpleDateFormat, for example 'yyyy-MM-ddz'.- Returns:
- The Date object.
- Throws:
ParseException- If unable to interpret the date String using the given format specifier.
-
convertDateToString
Converts a Java Date that into a formatted String. SeeSimpleDateFormatfor syntax information for the format String. For example a valid date format specifier might be '"MMM' 'dd', 'yyyy' 'z"' where yyyy indicates the year, MMM the month, dd the day and z the time zone (GMT, MST, PST, etc).- Parameters:
date- A Java Date object.dateFormat- The format of the date String to be output as specified inSimpleDateFormat, for example 'yyyy-MM-ddz'.- Returns:
- The formatted String.
- Throws:
ParseException- If unable to interpret the date Date using the given format specifier.
-
convertLongToDate
Converts a long representation of time to a Date.- Parameters:
milliseconds- Time in milliseconds- Returns:
- A Date object
-
printElapsedTime
Print the elapsed time that occured beween two points of time as recorded in java Date objects.- Parameters:
msg- A message inserted in front of the elapsed time string.start- The start Date.end- The end Date.
-
convertMillisecondsToTime
Converts a long that represents time in milliseconds to a String that displays the time in minutes and seconds.- Parameters:
milliseconds- Time in millisoconds.- Returns:
- A String that displays the time in minutes and seconds.
-
getRandomAlphaString
Generates a random alpha string of the given length.- Parameters:
length- The length of the String to generate- Returns:
- The randomAlphaString value
-
getRandomIntBetween
public static int getRandomIntBetween(int low, int high) Generates a random integer greater-than or equal to low and less-than high.- Parameters:
low- Smallest possible valuehigh- Highest possible value- Returns:
- A random number greater-than or equal to low and less-than high
-
getRandomCharsString
Generates a random string containing extended chars of the given length- Parameters:
length- The length of the String- Returns:
- The randomCharsString value
-
contains
Returns true if the object in the first parameter contains the Object in the second parameter according to the Objects equals method. The first parameter can be an Array, Collection (List, Set, etc.), Map or String. If the Object in the first parameter is a String, returns true if the Object in the second parameter is a subString of the first. If the Object in the first parameter is a Map, returns true if one of it's keys is equal to the Object in the second parameter.Implements a more versitile version of the regular JSTL contains function.
- Parameters:
target- The target Objectsubject- The subject Object for comparison- Returns:
- True if the target Object contains the subject Object
-
map
Puts items in a Map, creating a new TreeMap if null is passed in for the myMap argument, otherwise updating the Map with the key/value pair. TreeMap keeps its keys sorted lexagraphically.- Parameters:
myMap- A Map or nullkey- A key for insertion in the Mapvalue- A value for insertion in the Map, or null to insert empty String- Returns:
- The existing Map or a new TreeMap with the key/value pair inserted
-
mapSortByValue
Puts items in a Map, creating a new Map if null is passed in for the myMap argument, otherwise updating the Map with the key/value pair. Keys remain sorted by the *values* in the Map.- Parameters:
myMap- A Map or nullkey- A key for insertion in the Mapvalue- A value for insertion in the Map, or null to insert empty Stringascending- 'ascending' or 'descending' (defaults to ascending if other value is passed)- Returns:
- The existing Map or a new TreeMap with the key/value pair inserted
-
getPropertiesMap
Gets the Map from a String in the Java properties format of the form property=value one per line.- Parameters:
propertiesString- A Java Properties String- Returns:
- A Map of properties, or null if unable to parse
-
runCommand
Runs the given command-line command.- Parameters:
command- The full command string including argumentsstandardOutput- A StringBuffer that will be pupulated with the error output after execution, if any, or null not to use iterrorOutput- A StringBuffer that will be pupulated with the normal output after execution, if any, or null not to use it- Returns:
- 0 if normal completion, non-zero if abnormal
-
print_char_values
public static void print_char_values()Prints the char values of all chars in range 0 to 256. -
URLEncoder
Encodes a String for use in a URL using UTF-8 character encoding.- Parameters:
text- Unencoded text- Returns:
- Text that is encoded for use in a URL
- Throws:
UnsupportedEncodingException- If unable to encode using UTF-8.
-
RedirectEncoder
Encodes a string used in the URL sent to the redirect server and ensures it does not contain problematic characters for the Apache 1 mod_redirect rules (the character sequence %2F is replaced with /).- Parameters:
text- Unencoded text- Returns:
- Text that is encoded for use in a URL
- Throws:
UnsupportedEncodingException- If unable to encode using UTF-8.
-
ObjectInspector
Inspects an objects type and methods to standard out.- Parameters:
o- The Object to inspect- Returns:
- The objects class name
-
matches
Tells whether or not the source string matches the given regular expression.- Parameters:
source- The source StringregEx- A regular expression- Returns:
- True if the source String matches the regular expression
- See Also:
-
replaceAll
Replaces each substring of this string that matches the given regular expression with the given replacement. If replacement is empty or null, all matches are removed from the source String.- Parameters:
source- The source StringregEx- A regular expressionreplacement- The replacement String- Returns:
- The resulting String
- See Also:
-
replaceFirst
Replaces the first substring of this string that matches the given regular expression with the given replacement. If replacement is empty or null, all matches are removed from the source String.- Parameters:
source- The source StringregEx- A regular expressionreplacement- The replacement String- Returns:
- The resulting String
- See Also:
-
luceneStringToDate
Converts a Lucene String-encoded date to a Date Object. If the String can not be converted, returns null.- Parameters:
dateString- A Lucene String-encoded date- Returns:
- A Date Object, or null
-
getLexicalDateString
Converts a date String of the form YYYY-mm-dd, YYYY-mm, YYYY or yyyy-MM-ddTHH:mm:ssZ to a searchable Lucene (v2.x) lexical date String of the form 'yyyyMMddHHmmss', or null if unable to parse the date String.- Parameters:
dateString- A date String- Returns:
- The corresponding lexicalDateString value
-
getPropertiesResourceBundle
Gets a ResourceBundle from a properties file that is in a Jar file or class path within this application's runtime environment.- Parameters:
propsFileName- The name of the properties file- Returns:
- The ResourceBundle, or null if not found
-
getDayOfWeekString
Gets a String representation of the Calendar.DAY_OF_WEEK field.- Parameters:
dayOfWeek- A Calenday.DAY_OF_WEEK field value- Returns:
- A String representation of the day
- Throws:
ArrayIndexOutOfBoundsException- If the specified field is out of range (field invalid input: '<' 0 || field >= 7)
-
split
Splits this string around matches of the given regular expression.- Parameters:
source- The source StringregEx- A regular expression- Returns:
- The resulting String tokens
- See Also:
-
encodeToSearchTerm
Encodes a String to a single term for searching over fields that have been indexed encoded. Encodes spaces but leaves the wild card '*' un-encoded for searching.- Parameters:
string- A String to encode- Returns:
- An encoded String that may be used for searches
- See Also:
-
encodeToSearchTerms
Encodes a String to a String for searching over fields that have been indexed encoded. Preserves the wild card '*' and spaces ' '.- Parameters:
string- The String to encode- Returns:
- An encoded String that may be used for searches
- See Also:
-
printToSystemErr
Sends the string to System-err-println.- Parameters:
s- String to ouptut.
-
printToSystemOut
* Sends the string to System-out-println.- Parameters:
s- String to ouptut.
-
getUniqueID
public static long getUniqueID()Gets a global system unique ID. This algorithm is reasonably guaranteed to be correct within a single running JVM.- Returns:
- A number guaranteed to be unique throughout this JVM.
-