Class Converter


  • public final class Converter
    extends java.lang.Object
    The Class Converter to convert objects from one type to another type.
    Since:
    19.01.2009
    Version:
    $Revision: 1.50 $
    Author:
    oliver
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String classToResource​(java.lang.String name)
      Converts a classname (e.g.
      static java.io.Serializable deserialize​(byte[] bytes)
      Deserializes the given byte array and returns it as object.
      static java.lang.String getMemoryAsString​(long mem)
      Gets the memory as string.
      static java.lang.String getTimeAsString​(double timeInMillis)
      Gets the time as string with the corresponding unit.
      static java.lang.String getTimeAsString​(double timeInMillis, java.util.Locale locale)
      Gets the time as string with the corresponding unit.
      static java.lang.String getTimeAsString​(long timeInMillis)
      Gets the time as string with the corresponding unit.
      static java.lang.String packageToResource​(java.lang.String name)
      Converts a package name (e.g.
      static java.lang.String resourceToClass​(java.lang.String name)
      Converts a resource (e.g.
      static byte[] serialize​(java.io.Serializable object)
      Serializes the given object and returns it as byte array.
      static java.lang.String toAbsolutePath​(java.net.URI uri)
      Converts an URI into a file and returns it as absolute pathname.
      static java.lang.Object[] toArray​(java.lang.Object... objects)
      Converts variable number of arguments into an object array.
      static java.util.Date toDate​(java.lang.String s)
      Converts a string to a date by trying different date patterns.
      static java.util.Date toDate​(java.lang.String s, java.lang.String pattern)
      Converts a string to a date with the help of the given pattern.
      static java.io.File toFile​(java.lang.String uri)
      Converts an URI into a file.
      static java.io.File toFile​(java.net.URI uri)
      Converts an URI into a file.
      static java.lang.String toLongString​(java.lang.Object obj)
      Converts an object into a long representation as the normal toString method.
      static java.lang.String toLongString​(java.lang.Object[] array)
      If you want to print each element of an array into a single line you can use this method here.
      static java.lang.String toLongString​(java.lang.StackTraceElement[] stacktrace)
      This implementation prints the stacktrace in a similar way as the printStacktrace method of Throwable does it.
      static java.lang.String toLongString​(java.util.Map<?,​?> pairs)
      If you want to print the system properties as key-value pairs (e.g.
      static java.lang.String toResource​(java.lang.Class<?> clazz)
      To resource.
      static java.lang.String toResource​(java.lang.Package p)
      To resource.
      static java.lang.String toShortString​(java.lang.Number number)
      To short string.
      static java.lang.String toShortString​(java.lang.Object obj)
      Converts an object to its toString representation.
      static java.lang.String toShortString​(java.lang.Object[] array)
      To short string.
      static java.lang.String toShortString​(java.sql.Time time)
      If the given time string has 0 seconds it is returned as "hh:mm".
      static java.util.SortedSet<?> toSortedSet​(java.util.Enumeration<?> enumeration)
      Converts an Enumeration into a SortedSet.
      static java.lang.String toString​(java.lang.Object obj)
      Converts an object into its string representation.
      static java.lang.String toString​(java.lang.Object[] array)
      An empty array or null is mapped to "[]" (empty array).
      static java.lang.String toString​(java.lang.String prefix, java.lang.String postfix, java.lang.Object[] array)
      Maps an array to its string representation.
      static java.lang.String toString​(java.util.Date date, java.lang.String pattern)
      Converts a date to string using the default locale.
      static java.lang.String toString​(java.util.Date date, java.lang.String pattern, java.util.Locale locale)
      Converts a date to string using the default locale.
      static java.lang.String toString​(java.util.Enumeration<?> enumeration)
      To string.
      static java.lang.String[] toStringArray​(java.lang.Object[] array)
      Each object inside the array is converted into its toString() representation.
      static java.lang.String[] toStringArray​(java.util.Set<? extends java.lang.Object> set)
      Each object inside the Set is converted into its toString() representation.
      static java.sql.Time toTime​(java.lang.String s)
      Converts a time string to a Time value.
      static java.net.URI toURI​(java.lang.String uri)
      If a URL contains illegal characters for an URI (like blanks) this should be automatically converted (e.g. to "%20").
      static java.net.URI toURI​(java.net.URL url)
      If a URL contains illegal characters for an URI (like blanks) this should be automatically converted (e.g. to "%20")
      • Methods inherited from class java.lang.Object

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

      • getMemoryAsString

        public static java.lang.String getMemoryAsString​(long mem)
        Gets the memory as string.
        Parameters:
        mem - the mem
        Returns:
        the memory as string
      • getTimeAsString

        public static java.lang.String getTimeAsString​(long timeInMillis)
        Gets the time as string with the corresponding unit. Unit can be "ms" (for milliseconds) or "seconds".
        Parameters:
        timeInMillis - the time in millis
        Returns:
        the time as string
        Since:
        1.2.20
      • getTimeAsString

        public static java.lang.String getTimeAsString​(double timeInMillis)
        Gets the time as string with the corresponding unit. Unit can be "ms" (for milliseconds) or "seconds".
        Parameters:
        timeInMillis - the time in millis
        Returns:
        the time as string
        Since:
        1.4.2
      • getTimeAsString

        public static java.lang.String getTimeAsString​(double timeInMillis,
                                                       java.util.Locale locale)
        Gets the time as string with the corresponding unit. Unit can be "ms" (for milliseconds) or "seconds".
        Parameters:
        timeInMillis - the time in millis
        locale - the locale
        Returns:
        the time as string
        Since:
        1.4.2
      • classToResource

        public static java.lang.String classToResource​(java.lang.String name)
        Converts a classname (e.g. "java.lang.String") into a resource ("/java/lang/String.class").).
        Parameters:
        name - e.g. "java.lang.String"
        Returns:
        e.g. "/java/lang/String.class"
      • toResource

        public static java.lang.String toResource​(java.lang.Class<?> clazz)
        To resource.
        Parameters:
        clazz - the clazz
        Returns:
        the string
      • packageToResource

        public static java.lang.String packageToResource​(java.lang.String name)
        Converts a package name (e.g. "java.lang") into a resource ("/java/lang").).
        Parameters:
        name - e.g. "java.lang"
        Returns:
        e.g. "/java/lang"
      • toResource

        public static java.lang.String toResource​(java.lang.Package p)
        To resource.
        Parameters:
        p - the p
        Returns:
        the string
      • resourceToClass

        public static java.lang.String resourceToClass​(java.lang.String name)
        Converts a resource (e.g. "/java/lang/String.class") into its classname ("java.lang.String").
        Parameters:
        name - e.g. "/java/lang/String.class"
        Returns:
        e.g. "java.lang.String"
      • toURI

        public static java.net.URI toURI​(java.net.URL url)
        If a URL contains illegal characters for an URI (like blanks) this should be automatically converted (e.g. to "%20")
        Parameters:
        url - the URL to be converted
        Returns:
        the given URL as URI
      • toURI

        public static java.net.URI toURI​(java.lang.String uri)
        If a URL contains illegal characters for an URI (like blanks) this should be automatically converted (e.g. to "%20"). Also URL starting without an authority element (like "file:/") cannot have two slash characters ("//"). This is also corrected
        Parameters:
        uri - the URL to be converted
        Returns:
        the given URL as URI
      • toFile

        public static java.io.File toFile​(java.net.URI uri)
        Converts an URI into a file.
        Parameters:
        uri - e.g. URI("file:/tmp")
        Returns:
        e.g. File("/tmp")
      • toFile

        public static java.io.File toFile​(java.lang.String uri)
        Converts an URI into a file.
        Parameters:
        uri - the uri as string
        Returns:
        the file
      • toAbsolutePath

        public static java.lang.String toAbsolutePath​(java.net.URI uri)
        Converts an URI into a file and returns it as absolute pathname.
        Parameters:
        uri - e.g. URI("file:/tmp")
        Returns:
        e.g. "/tmp"
      • toString

        public static java.lang.String toString​(java.lang.Object obj)
        Converts an object into its string representation. A null object is mapped to "" (empty string).
        Parameters:
        obj - the obj
        Returns:
        "" if the given object is null
      • toString

        public static java.lang.String toString​(java.util.Enumeration<?> enumeration)
        To string.
        Parameters:
        enumeration - the enumeration
        Returns:
        the string
      • toShortString

        public static java.lang.String toShortString​(java.lang.Object obj)
        Converts an object to its toString representation. If the resuulting string would be too long it will be abbreviated.

        If the resulting toString representation looks like the default implementation of Object.toString() the package name will be removed from the result. For other toString results StringUtils.abbreviate(String, int) will be used to cut it if necessary.

        Parameters:
        obj - the obj
        Returns:
        the string
      • toShortString

        public static java.lang.String toShortString​(java.lang.Number number)
        To short string.
        Parameters:
        number - the number
        Returns:
        the string
      • toShortString

        public static java.lang.String toShortString​(java.sql.Time time)
        If the given time string has 0 seconds it is returned as "hh:mm". If not it is returned as "hh:mm:ss".
        Parameters:
        time - the time
        Returns:
        e.g. 12:00 for high noon
      • toLongString

        public static java.lang.String toLongString​(java.lang.Object obj)
        Converts an object into a long representation as the normal toString method. E.g. maps are splitted into several lines.
        Parameters:
        obj - the obj
        Returns:
        the string
        Since:
        1.4
      • toString

        public static java.lang.String toString​(java.lang.Object[] array)
        An empty array or null is mapped to "[]" (empty array).
        Parameters:
        array - e.g. an int array {1, 2, 3}
        Returns:
        e.g. "[ 1, 2, 3 ]"
      • toString

        public static java.lang.String toString​(java.lang.String prefix,
                                                java.lang.String postfix,
                                                java.lang.Object[] array)
        Maps an array to its string representation.
        Parameters:
        prefix - the prefix, e.g. "[ "
        postfix - the postfix, e.g. " ]"
        array - e.g. an int array {1, 2, 3}
        Returns:
        e.g. "[ 1, 2, 3 ]"
      • toShortString

        public static java.lang.String toShortString​(java.lang.Object[] array)
        To short string.
        Parameters:
        array - the array
        Returns:
        the string
      • toLongString

        public static java.lang.String toLongString​(java.util.Map<?,​?> pairs)
        If you want to print the system properties as key-value pairs (e.g. "java.version=1.6.0_17...") you can use this method here.
        Parameters:
        pairs - e.g. the system properties
        Returns:
        "key=value" lines (separated by newlines)
        Since:
        1.4
      • toLongString

        public static java.lang.String toLongString​(java.lang.Object[] array)
        If you want to print each element of an array into a single line you can use this method here.
        Parameters:
        array - the array
        Returns:
        the string
        Since:
        1.4.2
      • toLongString

        public static java.lang.String toLongString​(java.lang.StackTraceElement[] stacktrace)
        This implementation prints the stacktrace in a similar way as the printStacktrace method of Throwable does it.
        Parameters:
        stacktrace - the array
        Returns:
        the string
        Since:
        1.4.2
      • toStringArray

        public static java.lang.String[] toStringArray​(java.lang.Object[] array)
        Each object inside the array is converted into its toString() representation.

        Since 1.5 a null object is converted into an empty string ("").

        Parameters:
        array - e.g. an int array {1, 2, 3}
        Returns:
        e.g. {"1", "2", "3"}
        Since:
        27-Jul-2009
      • toStringArray

        public static java.lang.String[] toStringArray​(java.util.Set<? extends java.lang.Object> set)
        Each object inside the Set is converted into its toString() representation.
        Parameters:
        set - the set
        Returns:
        the given Set as array
        Since:
        27-Jul-2009
      • toString

        public static java.lang.String toString​(java.util.Date date,
                                                java.lang.String pattern)
        Converts a date to string using the default locale.
        Parameters:
        date - a valid date
        pattern - e.g. "dd-MMM-yyyy"
        Returns:
        e.g. "26-Nov-2009"
      • toString

        public static java.lang.String toString​(java.util.Date date,
                                                java.lang.String pattern,
                                                java.util.Locale locale)
        Converts a date to string using the default locale.
        Parameters:
        date - a valid date
        pattern - e.g. "dd-MMM-yyyy"
        locale - e.g. new Locale("de")
        Returns:
        e.g. "30-Mai-2010" (with German locale)
      • toArray

        public static java.lang.Object[] toArray​(java.lang.Object... objects)
        Converts variable number of arguments into an object array.
        Parameters:
        objects - the objects
        Returns:
        the object[]
      • toDate

        public static java.util.Date toDate​(java.lang.String s)
        Converts a string to a date by trying different date patterns. If the string can't be converted an IllegalArgumentException will be thrown.
        Parameters:
        s - e.g. "28-Nov-2009" or "Thu Nov 26 14:30:25 CET 2009"
        Returns:
        a valid date or NULL_DATE (if an empty string is given)
      • toDate

        public static java.util.Date toDate​(java.lang.String s,
                                            java.lang.String pattern)
        Converts a string to a date with the help of the given pattern. If the string can't be converted an IllegalArgumentException will be thrown.
        Parameters:
        s - e.g. "28-Nov-2009"
        pattern - e.g. "dd-MMM-yyyy"
        Returns:
        a valid date or NULL_DATE (if an empty string is given)
      • toTime

        public static java.sql.Time toTime​(java.lang.String s)
        Converts a time string to a Time value. In contradiction to Time.valueOf(String) it works also if the time string is given in the format "hh:mm".
        Parameters:
        s - e.g. "12:00" for high noon
        Returns:
        the time
      • toSortedSet

        public static java.util.SortedSet<?> toSortedSet​(java.util.Enumeration<?> enumeration)
        Converts an Enumeration into a SortedSet.
        Parameters:
        enumeration - the Enumeration
        Returns:
        the SortedSet
        Since:
        1.0
      • serialize

        public static byte[] serialize​(java.io.Serializable object)
                                throws java.io.NotSerializableException
        Serializes the given object and returns it as byte array.
        Parameters:
        object - the object to be serialized
        Returns:
        the object as byte array
        Throws:
        java.io.NotSerializableException - the not serializable exception
      • deserialize

        public static java.io.Serializable deserialize​(byte[] bytes)
                                                throws java.lang.ClassNotFoundException
        Deserializes the given byte array and returns it as object.
        Parameters:
        bytes - the byte array
        Returns:
        the deserialized object
        Throws:
        java.lang.ClassNotFoundException - if byte array can't be deserialized