Created
May 9, 2014 21:35
-
-
Save 058c37a272bed3464d47f0b01038a16a/549494706c7c1e119d3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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