### Question:
StandardTypeConverter implements TypeConverter, ConverterRegistry { @Override @SuppressWarnings("unchecked") public <T> T convert(final Object source, Type type) { if (source == null) { return (T) nullValue(type); } if (source.getClass() == type) { return (T) source; } if (Generics.isSuperType(type, source.getClass())) { return (T) source; } if ("".equals(source) && type != String.class && isEmptyStringNull()) { return null; } Type sourceType = source.getClass(); Class<?> sourceClass = Generics.erase(sourceType); if (sourceClass.isArray() && Generics.isSuperType(Collection.class, type)) { return (T) Arrays.asList(source); } Class<?> targetClass = Generics.erase(type); if (Collection.class.isAssignableFrom(sourceClass) && targetClass.isArray()) { throw new UnsupportedOperationException("Not implemented yet"); } if (type instanceof Class<?> && ((Class<?>) type).isPrimitive()) { type = Primitives.wrap((Class<?>) type); } Object result = null; do { SourceAndTarget key = new SourceAndTarget(sourceType, type); Collection<Converter<?, ?>> forwards = convertersBySourceAndTarget.get(key); for (Converter<?, ?> forward : forwards) if ((result = typeSafeTo(forward, source)) != null) break; if (result == null) { Collection<Converter<?,?>> reverses = convertersBySourceAndTarget.get(key.reverse()); for (Converter<?, ?> reverse : reverses) if ((result = typeSafeFrom(reverse, source)) != null) break; } if (sourceType == Object.class) break; Class<?> superClass = erase(sourceType).getSuperclass(); sourceType = getExactSuperType(sourceType, superClass); } while (result == null); if (result == null) throw new IllegalStateException("Cannot convert " + source.getClass() + " to " + type); return (T) result; } @Inject  StandardTypeConverter(@SuppressWarnings("rawtypes") Set<Converter> converters); @Override void register(Converter<?, ?> converter); static Type targetType(Converter<?, ?> converter); static Type sourceType(Converter<?, ?> converter); @Override @SuppressWarnings("unchecked") T convert(final Object source, Type type); @Override Collection<Converter<?, ?>> converter(Type source, Type target); @SuppressWarnings("unchecked") static T typeSafeTo(Converter<?, ?> converter, S source); @SuppressWarnings("unchecked") static S typeSafeFrom(Converter<?, ?> converter, T source); @Override Multimap<Type, Converter<?, ?>> getConvertersBySource(); @Override Multimap<Type, Converter<?, ?>> getConvertersByTarget();  }### Answer:
@Test public void stringToPrimitive() { Integer answer = converter.convert("42", Integer.class); assert answer == 42; }
@Test public void numbers() { BigDecimal answer = converter.convert(42, BigDecimal.class); assert answer.intValue() == 42; }
@Test public void dateToString() { SimpleDateFormat sdf = new SimpleDateFormat (format); Date date = new Date(); String answer = converter.convert(date, String.class); assert answer.equals(sdf.format(date)); }
@Test public void stringToDate() { SimpleDateFormat sdf = new SimpleDateFormat(format); Date original = new Date(); String expected = sdf.format(original); Date converted = converter.convert(expected, Date.class); String actual = sdf.format(converted); assert actual.equals(expected); }
@Test public void calendarToString() { SimpleDateFormat sdf = new SimpleDateFormat(format); Calendar calendar = Calendar.getInstance(); String answer = converter.convert(calendar, String.class); String expected = sdf.format(calendar.getTime()); assert answer.equals(expected) : answer + " but wanted " + expected; }
@Test public void stringToCalendar() { SimpleDateFormat sdf = new SimpleDateFormat(format); Calendar calendar = Calendar.getInstance(); Calendar answer = converter.convert(sdf.format(calendar.getTime()), Calendar.class); assert sdf.format(answer.getTime()).equals(sdf.format(calendar.getTime())); }