Skip to content

Instantly share code, notes, and snippets.

@electrum
Created December 18, 2013 20:22
Show Gist options
  • Save electrum/8029284 to your computer and use it in GitHub Desktop.
Save electrum/8029284 to your computer and use it in GitHub Desktop.
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
public final class Types
{
private Types() {}
public static <A, B extends A> B checkType(A value, Class<B> target, String name)
{
checkNotNull(value, "%s is null", name);
checkArgument(target.isInstance(value),
"%s must be of type %s, not %s",
target.getName(),
value.getClass().getName());
return target.cast(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment