Skip to content

Commit

Permalink
add more flexible ways to check arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
quiram committed Apr 26, 2018
1 parent f269905 commit 89b149f
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/main/java/com/github/quiram/utils/ArgumentChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.function.Function;

import static com.github.quiram.utils.Exceptions.unchecked;

public class ArgumentChecks {

static public void ensureNotNegative(int param, String paramName) throws IllegalArgumentException {
ensure(param, paramName, p -> p < 0, "not be negative");
}

static public void ensureGreaterThanZero(int param, String paramName) throws IllegalArgumentException {
ensure(param, paramName, p -> p <= 0, "be greater than zero");
ensureGreaterThan(0, param, paramName);
}

static public void ensureGreaterThan(int threshold, int param, String paramName) throws IllegalArgumentException {
ensure(param, paramName, p -> p <= threshold, "be greater than " + threshold);
}

static public void ensureNotBlank(String param, final String paramName) throws IllegalArgumentException {
Expand All @@ -24,8 +31,12 @@ static public <T> void ensureNotNull(T param, String paramName) throws IllegalAr
}

static public <T> void ensure(T param, String paramName, Function<T, Boolean> failCondition, String message) throws IllegalArgumentException {
if (failCondition.apply(param)) {
throw new IllegalArgumentException(paramName + " must " + message);
ensure(() -> failCondition.apply(param), paramName + " must " + message);
}

static public void ensure(Callable<Boolean> failCondition, String message) {
if (unchecked(failCondition)) {
throw new IllegalArgumentException(message);
}
}
}

0 comments on commit 89b149f

Please sign in to comment.