Class Utils

java.lang.Object
org.dlese.dpc.util.Utils

public class Utils extends Object
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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static final boolean
    contains(Object target, Object subject)
    Returns true if the object in the first parameter contains the Object in the second parameter according to the Objects equals method.
    static final String
    convertDateToString(Date date, String dateFormat)
    Converts a Java Date that into a formatted String.
    static final Date
    convertLongToDate(long milliseconds)
    Converts a long representation of time to a Date.
    static String
    convertMillisecondsToTime(long milliseconds)
    Converts a long that represents time in milliseconds to a String that displays the time in minutes and seconds.
    static final Date
    convertStringToDate(String dateString, String dateFormat)
    Converts a String that contains a recognizable date/time to a Java Date object.
    static final String
    Encodes a String to a single term for searching over fields that have been indexed encoded.
    static final String
    Encodes a String to a String for searching over fields that have been indexed encoded.
    static String
    getDayOfWeekString(int dayOfWeek)
    Gets a String representation of the Calendar.DAY_OF_WEEK field.
    static final String
    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 Map
    getPropertiesMap(String propertiesString)
    Gets the Map from a String in the Java properties format of the form property=value one per line.
    static final ResourceBundle
    Gets a ResourceBundle from a properties file that is in a Jar file or class path within this application's runtime environment.
    static String
    Generates a random alpha string of the given length.
    static String
    Generates a random string containing extended chars of the given length
    static int
    getRandomIntBetween(int low, int high)
    Generates a random integer greater-than or equal to low and less-than high.
    static long
    Gets a global system unique ID.
    static final Date
    Converts a Lucene String-encoded date to a Date Object.
    static Map
    map(Map myMap, String key, String value)
    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.
    static Map
    mapSortByValue(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 boolean
    matches(String source, String regEx)
    Tells whether or not the source string matches the given regular expression.
    static final String
    Inspects an objects type and methods to standard out.
    static void
    Prints the char values of all chars in range 0 to 256.
    static void
    printElapsedTime(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 void
    Sends the string to System-err-println.
    static final void
    * Sends the string to System-out-println.
    static final String
    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 String
    replaceAll(String source, String regEx, String replacement)
    Replaces each substring of this string that matches the given regular expression with the given replacement.
    static final String
    replaceFirst(String source, String regEx, String replacement)
    Replaces the first substring of this string that matches the given regular expression with the given replacement.
    static int
    runCommand(String command, StringBuffer standardOutput, StringBuffer errorOutput)
    Runs the given command-line command.
    static final String[]
    split(String source, String regEx)
    Splits this string around matches of the given regular expression.
    static final String
    Encodes a String for use in a URL using UTF-8 character encoding.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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. See SimpleDateFormat for 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 in SimpleDateFormat, for example 'yyyy-MM-ddz'.
      Returns:
      The Date object.
      Throws:
      ParseException - If unable to interpret the date String using the given format specifier.
    • convertDateToString

      public static final String convertDateToString(Date date, String dateFormat) throws ParseException
      Converts a Java Date that into a formatted String. See SimpleDateFormat for 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 in SimpleDateFormat, for example 'yyyy-MM-ddz'.
      Returns:
      The formatted String.
      Throws:
      ParseException - If unable to interpret the date Date using the given format specifier.
    • convertLongToDate

      public static final Date convertLongToDate(long milliseconds)
      Converts a long representation of time to a Date.
      Parameters:
      milliseconds - Time in milliseconds
      Returns:
      A Date object
    • printElapsedTime

      public static void printElapsedTime(String msg, Date start, Date end)
      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

      public static String convertMillisecondsToTime(long milliseconds)
      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

      public static String getRandomAlphaString(int length)
      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 value
      high - Highest possible value
      Returns:
      A random number greater-than or equal to low and less-than high
    • getRandomCharsString

      public static String getRandomCharsString(int length)
      Generates a random string containing extended chars of the given length
      Parameters:
      length - The length of the String
      Returns:
      The randomCharsString value
    • contains

      public static final boolean contains(Object target, Object subject)
      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 Object
      subject - The subject Object for comparison
      Returns:
      True if the target Object contains the subject Object
    • map

      public static Map map(Map myMap, String key, String value)
      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 null
      key - A key for insertion in the Map
      value - 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

      public static Map mapSortByValue(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. Keys remain sorted by the *values* in the Map.
      Parameters:
      myMap - A Map or null
      key - A key for insertion in the Map
      value - A value for insertion in the Map, or null to insert empty String
      ascending - '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

      public static Map getPropertiesMap(String propertiesString)
      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

      public static int runCommand(String command, StringBuffer standardOutput, StringBuffer errorOutput)
      Runs the given command-line command.
      Parameters:
      command - The full command string including arguments
      standardOutput - A StringBuffer that will be pupulated with the error output after execution, if any, or null not to use it
      errorOutput - 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
    • URLEncoder

      public static final String URLEncoder(String text) throws UnsupportedEncodingException
      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

      public static final String RedirectEncoder(String text) throws UnsupportedEncodingException
      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

      public static final String ObjectInspector(Object o)
      Inspects an objects type and methods to standard out.
      Parameters:
      o - The Object to inspect
      Returns:
      The objects class name
    • matches

      public static final boolean matches(String source, String regEx)
      Tells whether or not the source string matches the given regular expression.
      Parameters:
      source - The source String
      regEx - A regular expression
      Returns:
      True if the source String matches the regular expression
      See Also:
    • replaceAll

      public static final String replaceAll(String source, String regEx, String replacement)
      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 String
      regEx - A regular expression
      replacement - The replacement String
      Returns:
      The resulting String
      See Also:
    • replaceFirst

      public static final String replaceFirst(String source, String regEx, String replacement)
      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 String
      regEx - A regular expression
      replacement - The replacement String
      Returns:
      The resulting String
      See Also:
    • luceneStringToDate

      public static final Date luceneStringToDate(String dateString)
      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

      public static final String getLexicalDateString(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.
      Parameters:
      dateString - A date String
      Returns:
      The corresponding lexicalDateString value
    • getPropertiesResourceBundle

      public static final ResourceBundle getPropertiesResourceBundle(String propsFileName)
      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

      public static String getDayOfWeekString(int dayOfWeek) throws ArrayIndexOutOfBoundsException
      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

      public static final String[] split(String source, String regEx)
      Splits this string around matches of the given regular expression.
      Parameters:
      source - The source String
      regEx - A regular expression
      Returns:
      The resulting String tokens
      See Also:
    • encodeToSearchTerm

      public static final String encodeToSearchTerm(String string)
      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

      public static final String encodeToSearchTerms(String string)
      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

      public static final void printToSystemErr(String s)
      Sends the string to System-err-println.
      Parameters:
      s - String to ouptut.
    • printToSystemOut

      public static final void printToSystemOut(String s)
      * 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.