patterntesting.runtime.util
Class Converter

Package class diagram package Converter
java.lang.Object
  extended by patterntesting.runtime.util.Converter

public final class Converter
extends Object

The Class Converter to convert objects from one type to another type.

Since:
19.01.2009
Version:
$Revision: 1.30 $
Author:
oliver

Method Summary
static String classToResource(String name)
          Converts a classname (e.g.
static Serializable deserialize(byte[] bytes)
          Deserializes the given byte array and returns it as object.
static String getMemoryAsString(long mem)
          Gets the memory as string.
static String getTimeAsString(double timeInMillis)
          Gets the time as string with the corresponding unit.
static String getTimeAsString(double timeInMillis, Locale locale)
          Gets the time as string with the corresponding unit.
static String getTimeAsString(long timeInMillis)
          Gets the time as string with the corresponding unit.
static String packageToResource(String name)
          Converts a package name (e.g.
static String resourceToClass(String name)
          Converts a resource (e.g.
static byte[] serialize(Serializable object)
          Serializes the given object and returns it as byte array.
static String toAbsolutePath(URI uri)
          Converts an URI into a file and returns it as absolute pathname.
static Date toDate(String s)
          Converts a string to a date by trying different date patterns.
static Date toDate(String s, String pattern)
          Converts a string to a date with the help of the given pattern.
static File toFile(String uri)
          Converts an URI into a file.
static File toFile(URI uri)
          Converts an URI into a file.
static String toLongString(Map<?,?> pairs)
          If you want to print the system properties as key-value pairs (e.g.
static String toLongString(Object obj)
          Converts an object into a long representation as the normal toString method.
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.
static String toLongString(StackTraceElement[] stacktrace)
          This implementation prints the stacktrace in a similar way as the printStacktrace method of Throwable does it.
static String toResource(Class<?> clazz)
          To resource.
static String toResource(Package p)
          To resource.
static String toShortString(Number number)
          To short string.
static String toShortString(Object obj)
          Converts an object to its toString representation.
static String toShortString(Object[] array)
          To short string.
static SortedSet<?> toSortedSet(Enumeration<?> enumeration)
          Converts an Enumeration into a SortedSet.
static String toString(Date date, String pattern)
          Converts a date to string using the default locale.
static String toString(Date date, String pattern, Locale locale)
          Converts a date to string using the default locale.
static String toString(Enumeration<?> enumeration)
          To string.
static String toString(Object obj)
          Converts an object into its string representation.
static String toString(Object[] array)
          An empty array or null is mapped to "" (empty string).
static String[] toStringArray(Object[] array)
          Each object inside the array is converted into its toString() representation.
static String[] toStringArray(Set<? extends Object> set)
          Each object inside the Set is converted into its toString() representation.
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")
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")
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

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")

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

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 string).

Parameters:
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.

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<? extends 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(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)

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)

toSortedSet

public static SortedSet<?> 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


Copyright © 2002–2014 PatternTesting Team. All rights reserved.