Skip to content

Instantly share code, notes, and snippets.

@vorburger
Created January 23, 2015 16:11
Show Gist options
  • Select an option

  • Save vorburger/2d03f1d31fff1e9c164b to your computer and use it in GitHub Desktop.

Select an option

Save vorburger/2d03f1d31fff1e9c164b to your computer and use it in GitHub Desktop.
instanceof cast Optional (Guava or Java 8)
// 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