public void apply() throws IllegalAccessException, InvocationTargetException, InstantiationException { if (type.isEnum()) { for (T instance : type.getEnumConstants()) { assertThat( instance.toString(), is( type.getCanonicalName().substring(type.getPackage().getName().length() + 1) + "." + ((Enum<?>) instance).name())); } return; } for (Constructor<?> constructor : type.getDeclaredConstructors()) { if (constructor.isSynthetic() && skipSynthetic) { continue; } constructor.setAccessible(true); Class<?>[] parameterTypes = constructor.getParameterTypes(); Object[] actualArguments = new Object[parameterTypes.length]; Object[] otherArguments = new Object[parameterTypes.length]; int index = 0; for (Class<?> parameterType : parameterTypes) { putInstance(parameterType, actualArguments, otherArguments, index++); } int testIndex = 0; @SuppressWarnings("unchecked") T instance = (T) constructor.newInstance(actualArguments); assertThat(instance, is(instance)); assertThat(instance, not(is((Object) null))); assertThat(instance, not(is(new Object()))); Object similarInstance = constructor.newInstance(actualArguments); assertThat(instance.hashCode(), is(similarInstance.hashCode())); assertThat(instance, is(similarInstance)); if (skipToString) { assertThat(instance.toString(), notNullValue()); } else if (optionalToStringRegex == null) { checkString(instance); } else { assertThat(instance.toString(), new RegexMatcher(optionalToStringRegex)); } for (Object otherArgument : otherArguments) { Object[] compareArguments = new Object[actualArguments.length]; int argumentIndex = 0; for (Object actualArgument : actualArguments) { if (argumentIndex == testIndex) { compareArguments[argumentIndex] = otherArgument; } else { compareArguments[argumentIndex] = actualArgument; } argumentIndex++; } Object unlikeInstance = constructor.newInstance(compareArguments); assertThat(instance.hashCode(), not(is(unlikeInstance))); assertThat(instance, not(is(unlikeInstance))); testIndex++; } } }
@Override public String describe() { List<String> returns = Lists.newArrayList(); for (T r : values) returns.add(r.toString()); return "{" + Joiner.on(',').join(returns) + "}"; }
public static <T> List<String> toStringList(T[] array) { List<String> list = new ArrayList(); for (T element : array) { list.add(element.toString()); } return list; }
private void checkString(T instance) { assertThat( instance.toString(), CoreMatchers.startsWith( type.getCanonicalName().substring(type.getPackage().getName().length() + 1) + "{")); assertThat(instance.toString(), endsWith("}")); Class<?> currentType = type; do { for (Field field : type.getDeclaredFields()) { if (!field.isSynthetic() && !Modifier.isStatic(field.getModifiers()) && !ignoredFields.contains(field.getName())) { assertThat(instance.toString(), containsString(field.getName())); } } } while ((currentType = currentType.getSuperclass()) != Object.class); }
public static <T> String fmtList(final List<T> list) { String result = ""; for (T item : list) { result += item.toString() + "\n"; } return result; }
<T> String toString(Iterable<T> items, String sep) { String currSep = ""; StringBuilder sb = new StringBuilder(); for (T item : items) { sb.append(currSep); sb.append(item.toString()); currSep = sep; } return sb.toString(); }
/** * Computes the display string for any value. * * @param t the value to print * @return the formatted string */ public static <T> String print(T t) { if (t == null) { return ""; } if (conversion.canConvert(t.getClass(), String.class)) { return conversion.convert(t, String.class); } else { return t.toString(); } }
/** * Returns a string of the form elt1.toString() [sep elt2.toString() ... sep elt.toString()] for a * collection of elti objects (note there's no actual space between sep and the elti elements). * Returns "" if collection is empty. If collection contains just elt, then returns elt.toString() * * @param separator the string to use to separate objects * @param objects a collection of objects. the element order is defined by the iterator over * objects * @param <T> the type of the objects * @return a non-null string */ public static <T> String join(final String separator, final Collection<T> objects) { if (objects.isEmpty()) { // fast path for empty collection return ""; } else { final Iterator<T> iter = objects.iterator(); final T first = iter.next(); if (!iter.hasNext()) // fast path for singleton collections return first.toString(); else { // full path for 2+ collection that actually need a join final StringBuilder ret = new StringBuilder(first.toString()); while (iter.hasNext()) { ret.append(separator); ret.append(iter.next().toString()); } return ret.toString(); } } }
/** * Computes the display string for any value, for a specific type. * * @param desc the field descriptor - custom formatters are extracted from this descriptor. * @param t the value to print * @return the formatted string */ public static <T> String print(TypeDescriptor desc, T t) { if (t == null) { return ""; } if (desc != null && conversion.canConvert(desc, TypeDescriptor.valueOf(String.class))) { return (String) conversion.convert(t, desc, TypeDescriptor.valueOf(String.class)); } else if (conversion.canConvert(t.getClass(), String.class)) { return conversion.convert(t, String.class); } else { return t.toString(); } }
public static <T> String collectionToCommaString(Collection<T> list) { if (CollectionUtils.isEmpty(list)) return ""; StringBuffer result = new StringBuffer(); for (T item : list) { if (item != null) { result.append(item.toString() + ","); } } if (result.length() >= 1) { result.setLength(result.length() - 1); } return result.toString(); }
public <T> void logMany(final PrintStream stream, final Collection<T> list) { final String[] a = new String[list.size()]; int i = 0; for (T e : list) { a[i++] = e.toString(); } Arrays.sort(a); for (String o : a) { stream.println(o); } }
@Override public String toString() { List<T> ls = new ArrayList<T>(lefts); Collections.reverse(ls); StringBuffer sb = new StringBuffer(); sb.append("(("); for (int i = 0; i < lefts.size(); i++) { if (i != 0) sb.append(" "); sb.append(lefts.get(i).toString()); } sb.append(") " + current.toString() + " ("); for (int i = 0; i < rights.size(); i++) { if (i != 0) sb.append(" "); sb.append(rights.get(i).toString()); } sb.append("))\n"); return sb.toString(); }
public static <T> void out(LinkedList<T> list) { for (T t : list) { System.out.println(t.toString()); } }
protected String getItemText(@NotNull T value) { return value.toString(); }