Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 058c37a272bed3464d47f0b01038a16a/549494706c7c1e119d3d to your computer and use it in GitHub Desktop.
Save 058c37a272bed3464d47f0b01038a16a/549494706c7c1e119d3d to your computer and use it in GitHub Desktop.
public static <T> T cast(Object object, Class<T> type) {
return cast(object, type, null);
}
public static <T> T cast(Object object, Class<T> type, T def) {
Check.notNull(object, "object cannot be null");
Check.notNull(type, "type cannot be null");
return isInstance(object, type) ? type.cast(object) : def;
}
public static boolean isInstance(Object object, Class<?>... types) {
Check.notNull(object, "object cannot be null");
Check.notNull(types, "types cannot be null");
for(Class<?> type : types)
if(type.isInstance(object))
return true;
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment