Skip to content

Instantly share code, notes, and snippets.

@sdsantos
Created May 13, 2015 09:05
Show Gist options
  • Save sdsantos/f80f3aaa918109d9692c to your computer and use it in GitHub Desktop.
Save sdsantos/f80f3aaa918109d9692c to your computer and use it in GitHub Desktop.
Small Preconditions helper class for your Offensive Programming
import android.support.annotation.Nullable;
// Based on http://google-collections.googlecode.com/svn-history/r78/trunk/javadoc/com/google/common/base/Preconditions.html
public class Preconditions {
public static void checkArgument(boolean expression, @Nullable Object errorMessage) {
if (!expression) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
}
}
public static void checkState(boolean expression, @Nullable Object errorMessage) {
if (!expression) {
throw new IllegalStateException(String.valueOf(errorMessage));
}
}
public static <T> T checkNotNull(T reference, @Nullable Object errorMessage) {
if (reference == null) {
throw new NullPointerException(String.valueOf(errorMessage));
}
return reference;
}
public static <T> T checkNull(T reference, @Nullable Object errorMessage) {
checkState(reference == null, errorMessage);
return reference;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment