Created
January 23, 2015 16:11
-
-
Save vorburger/2d03f1d31fff1e9c164b to your computer and use it in GitHub Desktop.
instanceof cast Optional (Guava or Java 8)
This file contains hidden or 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
| // TODO Such an Optional for instanceof cast should be available more generically, somewhere? | |
| @SuppressWarnings("unchecked") | |
| protected <S, T extends S> Optional<T> getOptionalSubclass(S project, Class<T> desiredSubclass) { | |
| if (desiredSubclass.isInstance(project)) | |
| return Optional.of((T) project); | |
| else | |
| return Optional.absent(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment