Class Util


  • public final class Util
    extends java.lang.Object
    Generic utility functions.
    Author:
    Matthew Wakeling
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String KEY_DEFINITIONS
      Name of the key under which to store the serialized version of the key definitions
      static java.lang.String MODEL
      Name of the key under which to store the serialized version of the model
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <K,​V>
      void
      addToListMap​(java.util.Map<K,​java.util.List<V>> map, K key, V value)
      Add a value to a Map from keys to Set of values, creating the value list as needed.
      static <K,​V>
      void
      addToSetMap​(java.util.Map<K,​java.util.Set<V>> map, K key, java.util.Collection<V> newValues)
      Add values to a Map from keys to Set of values, creating the value list as needed.
      static <K,​V>
      void
      addToSetMap​(java.util.Map<K,​java.util.Set<V>> map, K key, V value)
      Add a value to a Map from keys to Set of values, creating the value list as needed.
      static java.util.Set<java.lang.Class<?>> decomposeClass​(java.lang.Class<?> clazz)
      Convert a dynamic Class into a Set of Class objects that comprise it.
      static boolean equals​(java.lang.Object a, java.lang.Object b)
      Compare two objects, using their .equals method, but comparing null to null as equal.
      static int gcd​(int a, int b)
      Takes two integers, and returns the greatest common divisor, using euclid's algorithm.
      static java.lang.Class<?> getClassFromString​(java.lang.String type)
      Returns the class (not primitive) associated with the given String type.
      protected static java.lang.String getFilename​(java.lang.String key, java.lang.String modelName)
      Given a key and model name, return filename for reading/writing.
      static java.lang.String getFriendlyName​(java.lang.Class<?> clazz)
      Creates a friendly name for a given class.
      static java.lang.String getMd5checksum​(java.lang.String sequence)  
      protected static java.util.Properties getPropertiesStartingWith​(java.lang.String str, java.util.Properties props)
      Returns all Properties in props that begin with str
      static int hashCode​(java.lang.Object obj)
      Return a zero hashCode if the object is null, otherwise return the real hashCode
      protected static java.lang.Class<?> instantiate​(java.lang.String type)
      Returns the Class for a given name (promoting primitives to their container class)
      protected static java.lang.String join​(java.util.Collection<?> c, java.lang.String delimiter)
      Returns a String formed by the delimited results of calling toString over a collection.
      static int lcm​(int a, int b)
      Takes two integers, and returns the lowest common multiple.
      static java.util.Properties loadKeyDefinitions​(java.lang.String modelName)
      Load the key definitions file for the named model from the classpath
      protected static java.util.Properties loadProperties​(java.lang.String filename)
      Load a specified properties file
      protected static java.util.Properties stripStart​(java.lang.String prefix, java.util.Properties props)
      Strips the given string off the keys of the given Properties, and returns a new set of properties.
      protected static java.util.List<java.lang.String> tokenize​(java.lang.String str)
      Returns a list of tokens delimited by whitespace in String str (useful when handling XML)
      protected static java.lang.String unqualifiedName​(java.lang.String className)
      Returns the unqualified class name from a fully qualified class name
      static java.lang.Exception verboseException​(java.lang.Exception e)
      Takes an Exception, and returns an Exception of similar type with all available information in the message.
      static java.lang.String wildcardSqlToUser​(java.lang.String exp)
      Deprecated.
      I don't think this is used anymore?
      static java.lang.String wildcardUserToSql​(java.lang.String exp)
      Turn a user supplied wildcard expression with * into an SQL LIKE/NOT LIKE expression with %'s and other special characters.
      • Methods inherited from class java.lang.Object

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

      • KEY_DEFINITIONS

        public static final java.lang.String KEY_DEFINITIONS
        Name of the key under which to store the serialized version of the key definitions
        See Also:
        Constant Field Values
      • MODEL

        public static final java.lang.String MODEL
        Name of the key under which to store the serialized version of the model
        See Also:
        Constant Field Values
    • Method Detail

      • equals

        public static boolean equals​(java.lang.Object a,
                                     java.lang.Object b)
        Compare two objects, using their .equals method, but comparing null to null as equal.
        Parameters:
        a - one Object
        b - another Object
        Returns:
        true if they are equal or both null
      • hashCode

        public static int hashCode​(java.lang.Object obj)
        Return a zero hashCode if the object is null, otherwise return the real hashCode
        Parameters:
        obj - an object
        Returns:
        the hashCode, or zero if the object is null
      • verboseException

        public static java.lang.Exception verboseException​(java.lang.Exception e)
        Takes an Exception, and returns an Exception of similar type with all available information in the message.
        Parameters:
        e - an Exception
        Returns:
        a String
      • gcd

        public static int gcd​(int a,
                              int b)
        Takes two integers, and returns the greatest common divisor, using euclid's algorithm.
        Parameters:
        a - an integer
        b - an integer
        Returns:
        the gcd of a and b
      • lcm

        public static int lcm​(int a,
                              int b)
        Takes two integers, and returns the lowest common multiple.
        Parameters:
        a - an integer
        b - an integer
        Returns:
        the lcm of a and b
      • wildcardSqlToUser

        @Deprecated
        public static java.lang.String wildcardSqlToUser​(java.lang.String exp)
        Deprecated.
        I don't think this is used anymore?
        Convert an SQL LIKE/NOT LIKE expression to a * wildcard expression. See wildcardUserToSql method for more information.
        Parameters:
        exp - the wildcard expression
        Returns:
        the SQL LIKE parameter
      • wildcardUserToSql

        public static java.lang.String wildcardUserToSql​(java.lang.String exp)
        Turn a user supplied wildcard expression with * into an SQL LIKE/NOT LIKE expression with %'s and other special characters. Please note that constraint value is saved in created java object (constraint) in form with '%' and in this form is saved in xml as well.
        Parameters:
        exp - the SQL LIKE parameter
        Returns:
        the equivalent wildcard expression
      • getMd5checksum

        public static java.lang.String getMd5checksum​(java.lang.String sequence)
        Parameters:
        sequence - sequence to be encoded
        Returns:
        encoded sequence, set to lowercase
      • getClassFromString

        public static java.lang.Class<?> getClassFromString​(java.lang.String type)
        Returns the class (not primitive) associated with the given String type.
        Parameters:
        type - the String type name
        Returns:
        a Class
        Throws:
        java.lang.IllegalArgumentException - if the String is an invalid name
      • addToSetMap

        public static <K,​V> void addToSetMap​(java.util.Map<K,​java.util.Set<V>> map,
                                                   K key,
                                                   java.util.Collection<V> newValues)
        Add values to a Map from keys to Set of values, creating the value list as needed.
        Type Parameters:
        K - The type of the key of the map.
        V - The type of the values in the sets of the map.
        Parameters:
        map - the Map
        key - the key
        newValues - the set of values
      • addToSetMap

        public static <K,​V> void addToSetMap​(java.util.Map<K,​java.util.Set<V>> map,
                                                   K key,
                                                   V value)
        Add a value to a Map from keys to Set of values, creating the value list as needed.
        Type Parameters:
        K - The type of the key of the map.
        V - The type of the values in the sets of the map.
        Parameters:
        map - the Map
        key - the key
        value - the value
      • addToListMap

        public static <K,​V> void addToListMap​(java.util.Map<K,​java.util.List<V>> map,
                                                    K key,
                                                    V value)
        Add a value to a Map from keys to Set of values, creating the value list as needed.
        Type Parameters:
        K - The type of the key of the map.
        V - The type of the values in the lists of the map.
        Parameters:
        map - the Map
        key - the key
        value - the value
      • tokenize

        protected static java.util.List<java.lang.String> tokenize​(java.lang.String str)
        Returns a list of tokens delimited by whitespace in String str (useful when handling XML)
        Parameters:
        str - the String to tokenize
        Returns:
        the String tokens
        Throws:
        java.lang.NullPointerException - if str is null
      • unqualifiedName

        protected static java.lang.String unqualifiedName​(java.lang.String className)
        Returns the unqualified class name from a fully qualified class name
        Parameters:
        className - the fully qualified class name
        Returns:
        the unqualified name
      • join

        protected static java.lang.String join​(java.util.Collection<?> c,
                                               java.lang.String delimiter)
        Returns a String formed by the delimited results of calling toString over a collection.
        Parameters:
        c - the collection to stringify
        delimiter - the character to join on
        Returns:
        the string representation
      • instantiate

        protected static java.lang.Class<?> instantiate​(java.lang.String type)
        Returns the Class for a given name (promoting primitives to their container class)
        Parameters:
        type - a classname
        Returns:
        the corresponding Class
      • getPropertiesStartingWith

        protected static java.util.Properties getPropertiesStartingWith​(java.lang.String str,
                                                                        java.util.Properties props)
        Returns all Properties in props that begin with str
        Parameters:
        str - the String that the returned properties should start with
        props - the Properties to search through
        Returns:
        a Properties object containing the subset of props
      • stripStart

        protected static java.util.Properties stripStart​(java.lang.String prefix,
                                                         java.util.Properties props)
        Strips the given string off the keys of the given Properties, and returns a new set of properties. The original properties are not altered.
        For example, given the property:
        • database.name=production
        a call to stripStart("database", props) will produce:
        • name=production
        Note that a dot will be added to the prefix.
        Parameters:
        prefix - the String to strip off - a "." will be appended to this string.
        props - the Properties object to change
        Returns:
        a Properties object containing the same properties with the initial string + "." stripped off the keys
      • loadProperties

        protected static java.util.Properties loadProperties​(java.lang.String filename)
        Load a specified properties file
        Parameters:
        filename - the filename of the properties file
        Returns:
        the corresponding Properties object
      • getFilename

        protected static java.lang.String getFilename​(java.lang.String key,
                                                      java.lang.String modelName)
        Given a key and model name, return filename for reading/writing.
        Parameters:
        key - key name
        modelName - the name of the model
        Returns:
        name of file
      • loadKeyDefinitions

        public static java.util.Properties loadKeyDefinitions​(java.lang.String modelName)
        Load the key definitions file for the named model from the classpath
        Parameters:
        modelName - the model name
        Returns:
        the key definitions
      • decomposeClass

        public static java.util.Set<java.lang.Class<?>> decomposeClass​(java.lang.Class<?> clazz)
        Convert a dynamic Class into a Set of Class objects that comprise it.
        Parameters:
        clazz - the Class to decompose
        Returns:
        a Set of Class objects
      • getFriendlyName

        public static java.lang.String getFriendlyName​(java.lang.Class<?> clazz)
        Creates a friendly name for a given class.
        Parameters:
        clazz - the class
        Returns:
        a String describing the class, without package names