/** * An empty array or null is mapped to "[]" (empty array). * * @param array e.g. an int array {1, 2, 3} * @return e.g. "[ 1, 2, 3 ]" */ public static String toString(final Object[] array) { return toString("[ ", " ]", array); }
/** * Class to resource. * * @param clazz the clazz * * @return the string * * @deprecated use {@link #toResource(Class)} */ public static String classToResource(final Class<?> clazz) { return classToResource(clazz.getName()); }
/** * Gets the free memory as string. * * @return the free memory as string */ public static String getFreeMemoryAsString() { return Converter.getMemoryAsString(getFreeMemory()); }
private static String[] getAsClasspath(final URL[] repositoryURLs) { String[] cp = new String[repositoryURLs.length]; for (int i = 0; i < cp.length; i++) { cp[i] = Converter.toAbsolutePath(Converter.toURI(repositoryURLs[i])); } return cp; }
/** * 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); } else if (obj instanceof Object[]) { return toLongString((Object[]) obj); } return toString(obj); }
private static void assertEquals(final byte[] a1, final byte[] a2, final int i) { if (a1[i] != a2[i]) { if (LOG.isTraceEnabled()) { LOG.trace("{}. element differs ({} != {}).", i + 1, Converter.toLongString(a1[i]), Converter.toLongString(a2[i])); } else if (LOG.isDebugEnabled()) { LOG.debug("{}. element differs ({} != {}).", i + 1, Converter.toString(a1[i]), Converter.toString(a2[i])); } throw new AssertionError((i + 1) + ". element differs (" + Converter.toShortString(a1[i]) + " != " + Converter.toShortString(a2[i]) + ")"); } }
private static String toParamString(final Object[] args) { Class<?> paramTypes[] = new Class<?>[args.length]; for (int i = 0; i < args.length; i++) { paramTypes[i] = args[i].getClass(); } return Converter.toShortString(paramTypes); }
private static void assertEqualSizedMaps(final Map<?, ?> m2, Map<Object, Object> m1) throws AssertionError { for (Entry<Object, Object> entry : m1.entrySet()) { Object value2 = m2.get(entry.getKey()); if (entry.getValue() == null) { if (value2 != null) { LOG.debug("Key \"{}\" has different values: null <--> {}.", entry.getKey(), Converter.toString(value2)); throw new AssertionError("key \"" + entry.getKey() + "\" has different values: null <--> " + Converter.toShortString(value2)); } } else if (value2 == null) { LOG.debug("{{}}\" is missing in {}.", entry, Converter.toString(m2)); throw new AssertionError("{" + entry + "} is missing in " + Converter.toShortString(m2)); } else if (!(value2.equals(entry.getValue()))) { LOG.debug("Key \"{}\" has different values: {} <--> {}.", entry.getKey(), Converter.toString(entry.getValue()), Converter.toString(value2)); throw new AssertionError("key \"" + entry.getKey() + "\" has different values: " + Converter.toShortString(entry.getValue()) + " <--> " + Converter.toShortString(value2)); } } }
/** * 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(); }
/** * Gets the classpath set. * * @param classes the classes * * @return the classpath set */ private SortedSet<URI> getClasspathSet(final List<Class<?>> classes) { SortedSet<URI> classpath = new TreeSet<URI>(); for (Class<?> cl : classes) { String resource = Converter.toResource(cl); Enumeration<URL> resources = this.getResources(resource); while (resources.hasMoreElements()) { URL url = resources.nextElement(); URI path = Converter.toURI(url); classpath.add(ClasspathHelper.getParent(path, resource)); } } return classpath; }
private SortedSet<URI> getClasspathSet(final List<Class<?>> classes) { Collection<String> classResources = new ArrayList<>(); for (Class<?> cl : classes) { classResources.add(Converter.toResource(cl)); } return this.classpathDigger.getResourcepathSet(classResources); }
/** * 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); }
/** * 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 { return toCanonicalFile(uri); } catch (IllegalArgumentException iae) { if ("jar".equalsIgnoreCase(uri.getScheme())) { try { String fileScheme = uri.toString().substring(4); return toFile(new URI(fileScheme)); } catch (URISyntaxException ex) { LOG.trace("Cannot convert '{}' to a file:", uri, ex); throw new IllegalArgumentException("not a File: " + uri, iae); } } } String path = uri.getPath(); if (StringUtils.isEmpty(path)) { path = StringUtils.substringAfterLast(uri.toString(), ":"); } return new File(path); }
private static void addElementsFromArchive(Collection<String> elements, File archive, String suffix) throws IOException { Collection<String> allElements = readElementsFromNestedArchive(archive); for(String resource : allElements) { if (resource.endsWith(suffix)) { elements.add(Converter.resourceToClass(resource)); } } }
private String[] getClasspathFromPackages(final Set<URI> packages) { String[] classpath = new String[packages.size()]; Iterator<URI> iterator = packages.iterator(); for (int i = 0; i < classpath.length; i++) { URI uri = iterator.next(); classpath[i] = Converter.toAbsolutePath(uri); } return classpath; }
/** * To resource. * * @param p the p * * @return the string */ public static String toResource(final Package p) { return packageToResource(p.getName()); }
private static void logMissingElement(Object element, Collection<?> collection) { if (LOG.isTraceEnabled()) { LOG.trace("Element \"{}\" is missing in {}.", Converter.toLongString(element), Converter.toLongString(collection)); } else if (LOG.isDebugEnabled()) { LOG.debug("Element \"{}\" is missing in {}.", Converter.toString(element), Converter.toString(collection)); } }
/** * Checks if each key-value pair in map m1 is equals to that of map m2. If * not this method will tell you which pair is different. If the maps have * different size this method will tell you which pair is missing. * * @param m1 * the m1 * @param m2 * the m2 */ @SuppressWarnings("unchecked") public static void assertEquals(final Map<?, ?> m1, final Map<?, ?> m2) { Map<Object, Object> map1 = (Map<Object, Object>) m1; if (m1.size() < m2.size()) { assertEquals(m2, m1); } else if (m1.size() > m2.size()) { for (Entry<Object, Object> entry : map1.entrySet()) { if (!(m2.containsKey(entry.getKey()))) { if (LOG.isTraceEnabled()) { LOG.trace("<{}> is missing in {}.", Converter.toLongString(entry), Converter.toLongString(m2)); } else if (LOG.isDebugEnabled()) { LOG.debug("<{}> is missing in {}.", Converter.toString(entry), Converter.toString(m2)); } throw new AssertionError("<" + entry + "> is missing in " + Converter.toShortString(m2)); } } } else { assertEqualSizedMaps(m2, map1); } }
/** * To short string. * * @param array the array * * @return the string */ public static String toShortString(Object[] array) { try { StringBuffer sbuf = new StringBuffer(toShortString(array[0])); for (int i = 1; i < array.length; i++) { sbuf.append(", "); sbuf.append(toShortString(array[i])); } return sbuf.toString(); } catch (RuntimeException e) { return ""; } }
/** * 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(); }