patterntesting.runtime.util.Converter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of patterntesting-rt Show documentation
Show all versions of patterntesting-rt Show documentation
PatternTesting Runtime (patterntesting-rt) is the runtime component for
the PatternTesting framework. It provides the annotations and base classes
for the PatternTesting testing framework (e.g. patterntesting-check,
patterntesting-concurrent or patterntesting-exception) but can be also
used standalone for classpath monitoring or profiling.
It uses AOP and AspectJ to perform this feat.
/*
* $Id: Converter.java,v 1.28 2014/04/21 08:44:43 oboehm Exp $
*
* Copyright (c) 2008 by Oliver Boehm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 19.01.2009 by oliver ([email protected])
*/
package patterntesting.runtime.util;
import static patterntesting.runtime.NullConstants.NULL_DATE;
import java.io.*;
import java.lang.reflect.Array;
import java.net.*;
import java.text.*;
import java.util.*;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.*;
import org.slf4j.*;
/**
* The Class Converter to convert objects from one type to another type.
*
* @author oliver
* @since 19.01.2009
* @version $Revision: 1.28 $
*/
public final class Converter {
private static final Logger log = LoggerFactory.getLogger(Converter.class);
/** The different date patterns (date only) which are used by toDate(). */
private static final String[] datePatterns = { "dd-MMM-yyyy", "dd-MM-yyyy", "yyyy-MMM-dd",
"yyyy-MM-dd", "MMM-dd-yyyy", "dd MMM yyyy", "dd MM yyyy", "yyyy MMM dd", "yyyy MM dd",
"MMM dd yyyy", "dd.MMM.yyyy", "dd.MM.yyyy", "yyyy.MMM.dd", "MMM.dd.yyyy" };
/** The differnt date patterns including the time. */
private static final String[] dateTimePatterns = getDateTimePatterns();
/**
* To avoid that this class is instantiated.
*/
private Converter() {}
/**
* Gets the memory as string.
*
* @param mem the mem
*
* @return the memory as string
*/
public static String getMemoryAsString(final long mem) {
if (mem < 1000) {
return mem + " bytes";
} else if (mem < 0x100000) {
return (mem + 512) / 1024 + " KB";
} else {
return (mem + 0x80000) / 0x100000 + " MB";
}
}
/**
* Gets the time as string with the corresponding unit. Unit can be
* "ms" (for milliseconds) or "seconds".
*
* @param timeInMillis the time in millis
* @return the time as string
* @since 1.2.20
*/
public static String getTimeAsString(final long timeInMillis) {
if (timeInMillis > 300000L) {
return ((timeInMillis + 30000L) / 60000L) + " minutes";
} else if (timeInMillis > 5000L) {
return ((timeInMillis + 500L) / 1000L) + " seconds";
}
return timeInMillis + " ms";
}
/**
* Gets the time as string with the corresponding unit. Unit can be
* "ms" (for milliseconds) or "seconds".
*
* @param timeInMillis the time in millis
* @return the time as string
* @since 1.4.2
*/
public static String getTimeAsString(final double timeInMillis) {
return getTimeAsString(timeInMillis, Locale.getDefault());
}
/**
* Gets the time as string with the corresponding unit. Unit can be
* "ms" (for milliseconds) or "seconds".
*
* @param timeInMillis the time in millis
* @param locale the locale
* @return the time as string
* @since 1.4.2
*/
public static String getTimeAsString(final double timeInMillis, final Locale locale) {
if (timeInMillis > 1.0) {
return getTimeAsString((long) timeInMillis);
}
Format nf = new DecimalFormat("#.###", new DecimalFormatSymbols(locale));
return nf.format(timeInMillis) + " ms";
}
/**
* Converts a classname (e.g. "java.lang.String") into a resource
* ("/java/lang/String.class").).
*
* @param name e.g. "java.lang.String"
*
* @return e.g. "/java/lang/String.class"
*/
public static String classToResource(final String name) {
if (name == null) {
return null;
}
return name.replaceAll("\\.", "/") + ".class";
}
/**
* To resource.
*
* @param clazz the clazz
*
* @return the string
*/
public static String toResource(final Class> clazz) {
return classToResource(clazz.getName());
}
/**
* Converts a package name (e.g. "java.lang") into a resource
* ("/java/lang").).
*
* @param name e.g. "java.lang"
*
* @return e.g. "/java/lang"
*/
public static String packageToResource(final String name) {
if (name == null) {
return null;
}
return name.replaceAll("\\.", "/");
}
/**
* To resource.
*
* @param p the p
*
* @return the string
*/
public static String toResource(final Package p) {
return packageToResource(p.getName());
}
/**
* Converts a resource (e.g. "/java/lang/String.class") into its classname
* ("java.lang.String").
*
* @param name e.g. "/java/lang/String.class"
*
* @return e.g. "java.lang.String"
*/
public static String resourceToClass(final String name) {
if (name == null) {
return null;
}
if (name.endsWith(".class")) {
int lastdot = name.lastIndexOf(".");
return name.substring(0, lastdot).replaceAll("[/\\\\]", "\\.");
} else {
return name;
}
}
/**
* If a URL contains illegal characters for an URI (like blanks) this
* should be automatically converted (e.g. to "%20")
*
* @param url the URL to be converted
* @return the given URL as URI
*/
public static URI toURI(final URL url) {
try {
return url.toURI();
} catch (URISyntaxException e) {
return toURI(url.toExternalForm());
}
}
/**
* If a URL contains illegal characters for an URI (like blanks) this
* should be automatically converted (e.g. to "%20")
*
* @param uri the URL to be converted
* @return the given URL as URI
*/
public static URI toURI(final String uri) {
try {
return new URI(uri);
} catch (URISyntaxException e) {
String converted = uri.replaceAll(" ", "%20");
try {
return new URI(converted);
} catch (URISyntaxException e2) {
throw new IllegalArgumentException(uri + " is not a valid URI",
e2);
}
}
}
/**
* Converts an URI into a file.
*
* @param uri e.g. URI("file:/tmp")
*
* @return e.g. File("/tmp")
*/
public static File toFile(final URI uri) {
try {
File file = new File(uri);
try {
return file.getCanonicalFile();
} catch (IOException ioe) {
return file;
}
} catch (IllegalArgumentException iae) {
if (uri.getScheme().equalsIgnoreCase("jar")) {
try {
String fileScheme = uri.toString().substring(4);
return toFile(new URI(fileScheme));
} catch (URISyntaxException e) {
throw new IllegalArgumentException("not a File: " + uri, e);
}
}
}
String path = uri.getPath();
if (StringUtils.isEmpty(path)) {
path = StringUtils.substringAfterLast(uri.toString(), ":");
}
return new File(path);
}
/**
* Converts an URI into a file.
*
* @param uri the uri as string
* @return the file
*/
public static File toFile(final String uri) {
try {
return toFile(new URI(uri));
} catch (URISyntaxException e) {
return new File(uri);
}
}
/**
* Converts an URI into a file and returns it as absolute pathname.
*
* @param uri e.g. URI("file:/tmp")
*
* @return e.g. "/tmp"
*/
public static String toAbsolutePath(final URI uri) {
File file = toFile(uri);
return file.getAbsolutePath();
}
/**
* Converts an object into its string representation.
* A null object is mapped to "" (empty string).
*
* @param obj the obj
*
* @return "" if the given object is null
*/
public static String toString(final Object obj) {
if (obj == null) {
return "";
}
try {
if (obj instanceof Object[]) {
return toString((Object[]) obj);
}
if (obj instanceof Enumeration>) {
return toString((Enumeration>) obj);
}
return obj.toString();
} catch (RuntimeException e) {
log.debug("Returning empty string because of " + e);
return "";
}
}
/**
* To string.
*
* @param enumeration the enumeration
* @return the string
*/
public static String toString(final Enumeration> enumeration) {
if (!enumeration.hasMoreElements()) {
return "";
}
StringBuffer sbuf = new StringBuffer();
while(enumeration.hasMoreElements()) {
sbuf.append(" ," + enumeration.nextElement());
}
return sbuf.substring(2);
}
/**
* 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 {@link Object#toString()} the package name will be
* removed from the result. For other toString results
* {@link StringUtils#abbreviate(String, int)} will be used to cut it if
* necessary.
*
*
* @param obj the obj
*
* @return the string
*/
public static String toShortString(final Object obj) {
if (obj == null) {
return "";
}
if (obj instanceof Object[]) {
return toShortString((Object[]) obj);
}
if (obj instanceof Class>) {
return ((Class>)obj).getSimpleName();
}
if (obj instanceof Number) {
return toShortString((Number) obj);
}
if (obj.getClass().isArray()) {
StringBuffer sbuf = new StringBuffer();
int n = Array.getLength(obj);
if (n == 0) {
return "";
}
sbuf.append(Array.get(obj, 0));
for (int i = 1; i < n; i++) {
sbuf.append(", ");
if (i > 2) {
sbuf.append("...");
break;
}
sbuf.append(Array.get(obj, i));
}
return sbuf.toString();
}
String s = toString(obj);
if (s.startsWith(obj.getClass().getName())) {
int lengthPackagename = obj.getClass().getPackage().getName().length();
return s.substring(lengthPackagename + 1);
}
return StringUtils.abbreviate(s, 20);
}
/**
* To short string.
*
* @param number the number
* @return the string
*/
public static String toShortString(final Number number) {
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setMaximumFractionDigits(5);
nf.setGroupingUsed(false);
return nf.format(number);
}
/**
* Converts an object into a long representation as the normal toString
* method. E.g. maps are splitted into several lines.
*
* @param obj the obj
* @return the string
* @since 1.4
*/
public static String toLongString(final Object obj) {
if (obj instanceof Map, ?>) {
return toLongString((Map, ?>) obj);
}
return toString(obj);
}
/**
* An empty array or null is mapped to "" (empty string).
*
* @param array e.g. an int array {1, 2, 3}
*
* @return e.g. "[ 1, 2, 3 ]"
*/
public static String toString(final Object[] array) {
if (ArrayUtils.isEmpty(array)) {
return "[]";
}
StringBuffer sbuf = new StringBuffer("[ ");
sbuf.append(toString(array[0]));
for (int i = 1; i < array.length; i++) {
sbuf.append(", ");
sbuf.append(toString(array[i]));
}
sbuf.append(" ]");
return sbuf.toString();
}
/**
* To short string.
*
* @param array the array
*
* @return the string
*/
public static String toShortString(final Object[] array) {
try {
StringBuffer sbuf = new StringBuffer(toShortString(array[0]));
for (int i = 1; i < array.length; i++) {
if (i > 2) {
sbuf.append(", ...");
break;
}
sbuf.append(", ");
sbuf.append(toShortString(array[i]));
}
return sbuf.toString();
} catch (RuntimeException e) {
return "";
}
}
/**
* 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.
*
* @since 1.4
* @param pairs e.g. the system properties
* @return "key=value" lines (separated by newlines)
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static String toLongString(final Map, ?> pairs) {
StringBuilder buf = new StringBuilder();
Set> keys = new TreeSet(pairs.keySet());
for (Iterator> iterator = keys.iterator(); iterator.hasNext();) {
Object key = iterator.next();
buf.append(key);
buf.append('=');
buf.append(pairs.get(key));
buf.append('\n');
}
return buf.toString();
}
/**
* Each object inside the array is converted into its toString()
* representation.
*
* @param array e.g. an int array {1, 2, 3}
*
* @return e.g. {"1", "2", "3"}
*
* @since 27-Jul-2009
*/
public static String[] toStringArray(final Object[] array) {
String[] strings = new String[array.length];
for (int i = 0; i < strings.length; i++) {
try {
strings[i] = array[i].toString();
} catch (NullPointerException npe) {
strings[i] = null;
}
}
return strings;
}
/**
* Each object inside the Set is converted into its toString()
* representation.
*
* @param set the set
*
* @return the given Set as array
*
* @since 27-Jul-2009
*/
public static String[] toStringArray(final Set extends Object> set) {
Object[] objects = new Object[set.size()];
objects = set.toArray(objects);
return toStringArray(objects);
}
/**
* Converts a date to string using the default locale.
*
* @param date a valid date
* @param pattern e.g. "dd-MMM-yyyy"
* @return e.g. "26-Nov-2009"
*/
public static String toString(final Date date, final String pattern) {
DateFormat df = new SimpleDateFormat(pattern);
return df.format(date);
}
/**
* Converts a date to string using the default locale.
*
* @param date a valid date
* @param pattern e.g. "dd-MMM-yyyy"
* @param locale e.g. new Locale("de")
* @return e.g. "30-Mai-2010" (with German locale)
*/
public static String toString(final Date date, final String pattern,
final Locale locale) {
DateFormat df = new SimpleDateFormat(pattern, locale);
return df.format(date);
}
/**
* Converts a string to a date by trying different date patterns.
* If the string can't be converted an IllegalArgumentException will
* be thrown.
*
* @param s e.g. "28-Nov-2009" or "Thu Nov 26 14:30:25 CET 2009"
* @return a valid date or NULL_DATE (if an empty string is given)
*/
public static Date toDate(final String s) {
try {
return toDate(s, false);
} catch (IllegalArgumentException iae) {
return toDate(s, true);
}
}
/**
* Converts a string to a date by trying different date patterns.
* To speed up this method we look first if the string is long enough
* to hold a date and time value. If not only the datePatterns
* are probed. This speeds up this method about a factor of 5 (from about
* 10 ms to 2 ms on a MacBook Pro with 2 GHz).
*
* @param s e.g. "28-Nov-2009"
* @param lenient the lenient
* @return the date
*/
private static Date toDate(final String s, final boolean lenient) {
if (StringUtils.isEmpty(s)) {
return NULL_DATE;
}
String[] patterns = dateTimePatterns;
if (s.length() < 12) {
patterns = datePatterns;
}
for (int i = 0; i < patterns.length; i++) {
try {
return toDate(s, patterns[i], lenient);
} catch (IllegalArgumentException iae) {
log.trace(iae.getMessage());
}
}
throw new IllegalArgumentException("can't convert \"" + s
+ "\" to date");
}
/**
* Here we combine date and time patterns.
* If it takes too long, cache the result!
*
* @since 1.1
* @return combined date and time patterns
*/
private static String[] getDateTimePatterns() {
String[] timePatterns = { "H:m", "h:m", "K:m", "k:m" };
List patterns = new ArrayList(datePatterns.length
* (timePatterns.length + 1));
for (int i = 0; i < datePatterns.length; i++) {
for (int j = 0; j < timePatterns.length; j++) {
patterns.add(datePatterns[i] + " " + timePatterns[j]);
}
}
patterns.addAll(Arrays.asList(datePatterns));
return patterns.toArray(new String[patterns.size()]);
}
/**
* 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.
*
* @param s e.g. "28-Nov-2009"
* @param pattern e.g. "dd-MMM-yyyy"
* @return a valid date or NULL_DATE (if an empty string is given)
*/
public static Date toDate(final String s, final String pattern) {
return toDate(s, pattern, true);
}
private static Date toDate(final String s, final String pattern,
final boolean lenient) {
DateFormat df = new SimpleDateFormat(pattern);
df.setLenient(lenient);
try {
return df.parse(s);
} catch (ParseException e1) {
if (log.isTraceEnabled()) {
log.trace("trying to match " + s + " with locale \"en\"...");
}
df = new SimpleDateFormat(pattern, new Locale("en"));
df.setLenient(lenient);
try {
return df.parse(s);
} catch (ParseException e2) {
throw new IllegalArgumentException("\"" + s
+ "\" does not match pattern " + pattern, e2);
}
}
}
/**
* Converts an Enumeration into a SortedSet.
*
* @since 1.0
* @param enumeration the Enumeration
* @return the SortedSet
*/
public static SortedSet> toSortedSet(final Enumeration> enumeration) {
SortedSet