Class Converter

java.lang.Object
clazzfish.monitor.util.Converter

public final class Converter extends Object
The Class Converter to convert objects from one type to another type.

This class was copied from the PatternTesting project.

Author:
oliver
  • Method Details

    • getMemoryAsString

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

      public static 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 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 String getTimeAsString(double timeInMillis, 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 String classToResource(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 String toResource(Class<?> clazz)
      To resource.
      Parameters:
      clazz - the clazz
      Returns:
      the string
    • packageToResource

      public static String packageToResource(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 String toResource(Package p)
      To resource.
      Parameters:
      p - the p
      Returns:
      the string
    • resourceToClass

      public static String resourceToClass(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 URI toURI(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 URI toURI(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 File toFile(URI uri)
      Converts an URI into a file.
      Parameters:
      uri - e.g. URI("file:/tmp")
      Returns:
      e.g. File("/tmp")
    • toFile

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

      public static String toAbsolutePath(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 String toString(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 String toString(Enumeration<?> enumeration)
      To string.
      Parameters:
      enumeration - the enumeration
      Returns:
      the string
    • toShortString

      public static String toShortString(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 String toShortString(Number number)
      To short string.
      Parameters:
      number - the number
      Returns:
      the string
    • toShortString

      public static String toShortString(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 String toLongString(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 String toString(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 String toString(String prefix, String postfix, 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 String toShortString(Object[] array)
      To short string.
      Parameters:
      array - the array
      Returns:
      the string
    • toLongString

      public static String toLongString(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 String toLongString(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 String toLongString(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 String[] toStringArray(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 String[] toStringArray(Set<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 String toString(Date date, 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 String toString(LocalDate date, String pattern)
      Converts a date to string using English as default locale.
      Parameters:
      date - a valid date
      pattern - e.g. "dd-MMM-yyyy"
      Returns:
      e.g. "26-Nov-2009"
      Since:
      1.1
    • toString

      public static String toString(LocalDateTime time, String pattern)
      Converts a date to string using English as default locale.
      Parameters:
      time - a valid time
      pattern - e.g. "dd-MMM-yyyy HH:mm"
      Returns:
      e.g. "07-Jan-2022 17:40"
      Since:
      1.1
    • toString

      public static String toString(Date date, String pattern, 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 Object[] toArray(Object... objects)
      Converts variable number of arguments into an object array.
      Parameters:
      objects - the objects
      Returns:
      the object[]
    • toDate

      public static Date toDate(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 Date toDate(String s, 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 Time toTime(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 SortedSet<Object> toSortedSet(Enumeration<?> enumeration)
      Converts an Enumeration into a SortedSet.
      Parameters:
      enumeration - the Enumeration
      Returns:
      the SortedSet
      Since:
      1.0
    • serialize

      public static byte[] serialize(Serializable object) throws 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:
      NotSerializableException - the not serializable exception
    • deserialize

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