Class GeneralUtil


  • public class GeneralUtil
    extends java.lang.Object
    Description of the Class
    Author:
    Patrick Wright
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.text.DecimalFormat PADDED_HASH_FORMAT
      Used to format an Object's hashcode into a 0-padded 10 char String, e.g.
    • Constructor Summary

      Constructors 
      Constructor Description
      GeneralUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String classNameOnly​(java.lang.Object o)
      Given an Object instance, returns just the classname with no package
      static java.lang.String classNameOnly​(java.lang.String cname)
      Given a String classname, returns just the classname with no package
      static void dumpShortException​(java.lang.Exception ex)
      Dumps an exception to the console, only the last 5 lines of the stack trace.
      static java.lang.String escapeHTML​(java.lang.String s)
      Converts any special characters into their corresponding HTML entities , for example < to <.
      static java.net.URL getURLFromClasspath​(java.lang.Object obj, java.lang.String resource)  
      static java.lang.StringBuffer htmlEscapeSpace​(java.lang.String uri)  
      static java.lang.String inputStreamToString​(java.io.InputStream is)  
      static boolean isMacOSX()  
      static void main​(java.lang.String[] args)  
      static java.io.InputStream openStreamFromClasspath​(java.lang.Object obj, java.lang.String resource)
      Description of the Method
      static java.lang.String paddedHashCode​(java.lang.Object o)
      Description of the Method
      static int parseIntRelaxed​(java.lang.String s)
      Parses an integer from a string using less restrictive rules about which characters we won't accept.
      static java.lang.String trackBack​(int cnt)
      Returns a String tracking the last n method calls, from oldest to most recent.
      static void writeStringToFile​(java.lang.String content, java.lang.String encoding, java.lang.String fileName)  
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PADDED_HASH_FORMAT

        public static final java.text.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

      • openStreamFromClasspath

        public static java.io.InputStream openStreamFromClasspath​(java.lang.Object obj,
                                                                  java.lang.String resource)
        Description of the Method
        Parameters:
        obj - PARAM
        resource - PARAM
        Returns:
        Returns
      • getURLFromClasspath

        public static java.net.URL getURLFromClasspath​(java.lang.Object obj,
                                                       java.lang.String resource)
      • dumpShortException

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

        public static java.lang.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 java.lang.String classNameOnly​(java.lang.Object o)
        Given an Object instance, returns just the classname with no package
        Parameters:
        o - PARAM
        Returns:
        Returns
      • classNameOnly

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

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

        public static boolean isMacOSX()
      • htmlEscapeSpace

        public static java.lang.StringBuffer htmlEscapeSpace​(java.lang.String uri)
      • inputStreamToString

        public static java.lang.String inputStreamToString​(java.io.InputStream is)
                                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • main

        public static void main​(java.lang.String[] args)
      • writeStringToFile

        public static void writeStringToFile​(java.lang.String content,
                                             java.lang.String encoding,
                                             java.lang.String fileName)
                                      throws java.io.IOException,
                                             java.io.UnsupportedEncodingException
        Throws:
        java.io.IOException
        java.io.UnsupportedEncodingException
      • parseIntRelaxed

        public static int parseIntRelaxed​(java.lang.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 final java.lang.String escapeHTML​(java.lang.String s)
        Converts any special characters into their corresponding HTML entities , for example < to <. 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.