Skip to content

Instantly share code, notes, and snippets.

@walmyrcarvalho
Forked from sdsantos/Preconditions.java
Created February 14, 2016 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walmyrcarvalho/cbdd666d2b0281b7ee69 to your computer and use it in GitHub Desktop.
Save walmyrcarvalho/cbdd666d2b0281b7ee69 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