@Override public String toString() { return format("%s[format=%s, arguments=%s]", getClass().getSimpleName(), quote(format), format(arguments)); }
private Object[] msgArgs(String description) { return array(description, toStringOf(expected), toStringOf(actual)); }
@SuppressWarnings("unchecked") private static <T> List<T> asList(Object array) { if (array == null) { return null; } if (!isArray(array)) { throw new IllegalArgumentException("The object should be an array"); } int length = getLength(array); List<T> list = new ArrayList<T>(length); for (int i = 0; i < length; i++) { list.add((T) Array.get(array, i)); } return list; }
/** * Returns an array containing the names of the given types. * * @param types the given types. * @return the names of the given types stored in an array. */ public static String[] namesOf(Class<?>... types) { if (isNullOrEmpty(types)) { return new String[0]; } String[] names = new String[types.length]; for (int i = 0; i < types.length; i++) { names[i] = types[i].getName(); } return names; }
/** * Indicates whether the given array is {@code null} or empty. * * @param <T> the type of elements of the array. * @param array the array to check. * @return {@code true} if the given array is {@code null} or empty, otherwise {@code false}. */ public static <T> boolean isNullOrEmpty(T[] array) { return array == null || !hasElements(array); }
/** * Concatenates the given objects into a single {@code String}. This method is more efficient than concatenating using * "+", since only one <code>{@link StringBuilder}</code> is created. * * @param objects the objects to concatenate. * @return a {@code String} containing the given objects. */ public static String concat(Object... objects) { if (Arrays.isNullOrEmpty(objects)) { return null; } StringBuilder b = new StringBuilder(); for (Object o : objects) { b.append(o); } return b.toString(); }
/** * Returns {@code true} if the given array has only {@code null} elements, {@code false} otherwise. If given array is * empty, this method returns {@code true}. * * @param <T> the type of elements of the array. * @param array the given array. <b>It must not be null</b>. * @return {@code true} if the given array has only {@code null} elements or is empty, {@code false} otherwise. * @throws NullPointerException if the given array is {@code null}. * @since 1.1.3 */ public static <T> boolean hasOnlyNullElements(T[] array) { checkNotNull(array); if (!hasElements(array)) { return false; } for (T o : array) { if (o != null) { return false; } } return true; }
private ReflectionError cannotInvokeMethod(Throwable cause, Object... args) { String message = concat("Unable to invoke method ", quote(method.getName()), " with arguments ", format(args)); throw new ReflectionError(message, cause); }
private boolean isObjectArray(Object o) { return isArray(o) && !isArrayTypePrimitive(o); }
DecoratorInvocationHandler<T> handler = new PostDecorator<T>(target, decorator); @SuppressWarnings("unchecked") T field = (T) Proxy.newProxyInstance(decorator.getClass().getClassLoader(), array(expectedType), handler); set(field); return DecoratedInvoker.newInvoker(target, decorator, expectedType, this, handler);
/** * Specifies the delimeter to use to join {@code String}s. * * @param delimeter the delimeter to use. * @return the {@code String}s joined using the given delimeter. */ public String with(String delimeter) { if (delimeter == null) { throw new IllegalArgumentException("Delimiter should not be null"); } if (Arrays.isNullOrEmpty(strings)) { return ""; } StringBuilder b = new StringBuilder(); int stringCount = strings.length; for (int i = 0; i < stringCount; i++) { String s = strings[i]; b.append(s != null ? s : ""); if (i < stringCount - 1) { b.append(delimeter); } } return b.toString(); } }
private static Method lookupInClassHierarchy(String methodName, Class<?> targetType, Class<?>[] parameterTypes) { Method method = null; Class<?> type = targetType; while (type != null) { method = method(methodName, type, parameterTypes); if (method != null) break; type = type.getSuperclass(); } if (method == null) throw new ReflectionError(concat("Unable to find method ", quote(methodName), " in ", targetType.getName(), " with parameter type(s) ", format(parameterTypes))); return method; }
private static boolean areEqualArrays(Object o1, Object o2) { if (!isArray(o1) || !isArray(o2)) { return false; } if (o1 == o2) { return true; } int size = Array.getLength(o1); if (Array.getLength(o2) != size) { return false; } for (int i = 0; i < size; i++) { Object e1 = Array.get(o1, i); Object e2 = Array.get(o2, i); if (!areEqual(e1, e2)) { return false; } } return true; }
DecoratorInvocationHandler<T> handler = new PreDecorator<T>(target, decorator); @SuppressWarnings("unchecked") T field = (T) Proxy.newProxyInstance(decorator.getClass().getClassLoader(), array(expectedType), handler); set(field); return DecoratedInvoker.newInvoker(target, decorator, expectedType, this, handler);
/** * Returns the names of the files inside the specified directory. * * @param dir the name of the directory to start the search from. * @param recurse if {@code true}, we will look in subdirectories. * @return the names of the files inside the specified directory. */ private static List<String> fileNamesIn(File dir, boolean recurse) { List<String> scriptNames = new ArrayList<String>(); File[] existingFiles = dir.listFiles(); if (isNullOrEmpty(existingFiles)) { return scriptNames; } for (File existingFile : existingFiles) { if (existingFile.isDirectory()) { if (recurse) { scriptNames.addAll(fileNamesIn(existingFile, recurse)); } continue; } String filename = existingFile.getAbsolutePath(); if (!scriptNames.contains(filename)) { scriptNames.add(filename); } } return scriptNames; }
private String formatPrimitiveArray(Object o) { if (!isArray(o)) { return null; } if (!isArrayTypePrimitive(o)) { throw notAnArrayOfPrimitives(o); } int size = getLength(o); if (size == 0) { return "[]"; } StringBuilder buffer = new StringBuilder(); buffer.append('['); buffer.append(Array.get(o, 0)); for (int i = 1; i < size; i++) { buffer.append(", "); buffer.append(Array.get(o, i)); } buffer.append("]"); return buffer.toString(); }
/** * Creates a new directory using the given path. * * @param path the path of the new directory. * @return the new created directory. * @throws FilesException if the path belongs to an existing non-empty directory. * @throws FilesException if the path belongs to an existing file. * @throws FilesException if any I/O error is thrown when creating the new directory. */ public static File newFolder(String path) { File file = new File(path); if (file.isDirectory() && !isNullOrEmpty(file.list())) { throw cannotCreateNewFile(path, "a non-empty directory was found with the same path"); } try { if (!file.mkdir()) { throw cannotCreateNewFile(path, "a file was found with the same path"); } } catch (Exception e) { throw cannotCreateNewFile(path, e); } return file; }
private void deepToString(Object[] array, StringBuilder buffer, Set<Object[]> alreadyFormatted) { if (array == null) { buffer.append(NULL); return; } alreadyFormatted.add(array); buffer.append('['); int size = array.length; for (int i = 0; i < size; i++) { if (i != 0) { buffer.append(", "); } Object element = array[i]; if (!isArray(element)) { buffer.append(element == null ? NULL : toStringOf(element)); continue; } if (!isObjectArray(element)) { buffer.append(formatPrimitiveArray(element)); continue; } if (alreadyFormatted.contains(element)) { buffer.append("[...]"); continue; } deepToString((Object[]) element, buffer, alreadyFormatted); } buffer.append(']'); alreadyFormatted.remove(array); }
/** * Creates a new file using the given path. * * @param path the path of the new file. * @return the new created file. * @throws FilesException if the path belongs to an existing non-empty directory. * @throws FilesException if the path belongs to an existing file. * @throws FilesException if any I/O error is thrown when creating the new file. */ public static File newFile(String path) { File file = new File(path); if (file.isDirectory() && !isNullOrEmpty(file.list())) { throw cannotCreateNewFile(path, "a non-empty directory was found with the same path"); } try { if (!file.createNewFile()) { throw cannotCreateNewFile(path, "a file was found with the same path"); } } catch (IOException e) { throw cannotCreateNewFile(path, e); } return file; }