Class GeneralUtil


  • public class GeneralUtil
    extends Object
    Description of the Class
    Author:
    Patrick Wright
    • Field Detail

      • PADDED_HASH_FORMAT

        public static final DecimalFormat PADDED_HASH_FORMAT
        Used to format an Object's hashcode into a 0-padded 10 char String, e.g. for 24993066 returns "0024993066"
    • Constructor Detail

      • GeneralUtil

        public GeneralUtil()
    • Method Detail

      • dumpShortException

        public static void dumpShortException​(Exception ex)
        Dumps an exception to the console, only the last 5 lines of the stack trace.
        Parameters:
        ex - PARAM
      • trackBack

        public static String trackBack​(int cnt)
        Returns a String tracking the last n method calls, from oldest to most recent. You can use this as a simple tracing mechanism to find out the calls that got to where you execute the trackBack() call from. Example:
         // called from Box.calcBorders(), line 639
         String tback = GeneralUtil.trackBack(6);
         System.out.println(tback);
         
        produces
         Boxing.layoutChildren(ln 204)
         BlockBoxing.layoutContent(ln 81)
         Boxing.layout(ln 72)
         Boxing.layout(ln 133)
         Box.totalLeftPadding(ln 306)
         Box.calcBorders(ln 639)
         
        The trackBack() method itself is always excluded from the dump. Note the output may not be useful if HotSpot has been optimizing the code.
        Parameters:
        cnt - How far back in the call tree to go; if call tree is smaller, will be limited to call tree.
        Returns:
        see desc
      • classNameOnly

        public static String classNameOnly​(Object o)
        Given an Object instance, returns just the classname with no package
        Parameters:
        o - PARAM
        Returns:
        Returns
      • classNameOnly

        public static String classNameOnly​(String cname)
        Given a String classname, returns just the classname with no package
        Parameters:
        cname - PARAM
        Returns:
        Returns
      • paddedHashCode

        public static String paddedHashCode​(Object o)
        Description of the Method
        Parameters:
        o - PARAM
        Returns:
        Returns
      • isMacOSX

        public static boolean isMacOSX()
      • inputStreamToString

        public static String inputStreamToString​(InputStream is)
                                          throws IOException
        Reads all content from a given InputStream into a String using the default platform encoding.
        Parameters:
        is - the InputStream to read from. Must already be open, and will NOT be closed by this function. Failing to close this stream after the call will result in a resource leak.
        Returns:
        String containing contents read from the stream
        Throws:
        IOException - if the stream could not be read
      • parseIntRelaxed

        public static int parseIntRelaxed​(String s)
        Parses an integer from a string using less restrictive rules about which characters we won't accept. This scavenges the supplied string for any numeric character, while dropping all others.
        Parameters:
        s - The string to parse
        Returns:
        The number represented by the passed string, or 0 if the string is null, empty, white-space only, contains only non-numeric characters, or simply evaluates to 0 after parsing (e.g. "0")
      • escapeHTML

        public static String escapeHTML​(String s)
        Converts any special characters into their corresponding HTML entities, for example < to &lt;. This is done using a character by character test, so you may consider other approaches for large documents. Make sure you declare the entities that might appear in this replacement, e.g. the latin-1 entities This method was taken from a code-samples website, written and hosted by Real Gagnon, at http://www.rgagnon.com/javadetails/java-0306.html.
        Parameters:
        s - The String which may contain characters to escape.
        Returns:
        The string with the characters as HTML entities.