### Question:
SafeCast { public static <T, U extends T> T toGeneric(Object o, U def, Class<T> clazz) { if (o == null) { return def; } try { return clazz.cast(o); } catch (ClassCastException e) { return def; } }  static long toLong(Object o, long def); static int toInt(Object o, int def); static byte toByte(Object o, byte def); static float toFloat(Object o, float def); static byte[] toByteArray(Object o, byte[] def); static short[] toShortArray(Object o, short[] def); static int[] toIntArray(Object o, int[] def); static String toString(Object o, String def); static T toGeneric(Object o, U def, Class<T> clazz);  }### Answer:
@Test public void testGeneric() { String string = "Valid string"; String backup = "Backup"; assertTrue("Unable to process valid cast", (SafeCast.toGeneric(string, backup, String.class) == null ? string == null : SafeCast.toGeneric(string, backup, String.class).equals(string))); assertTrue("Default did not work (wrong type)", (SafeCast.toGeneric(new Integer(22), backup, String.class) == null ? backup == null : SafeCast.toGeneric(new Integer(22), backup, String.class).equals(backup))); assertTrue("Default did not work (null)", (SafeCast.toGeneric(null, backup, String.class) == null ? backup == null : SafeCast.toGeneric(null, backup, String.class).equals(backup))); }