
io.polyglotted.common.util.Assertions Maven / Gradle / Ivy
package io.polyglotted.common.util;
import java.util.Collection;
import java.util.Map;
import static com.google.common.base.Strings.isNullOrEmpty;
@SuppressWarnings({"unused", "WeakerAccess"})
public abstract class Assertions {
public static void checkBool(boolean expression, String message) { if (!expression) { throw new IllegalArgumentException(message); } }
public static int checkEq(int actual, int required, String item) {
checkBool(actual == required, "expected " + item + " equal to " + required + " but got " + actual);
return actual;
}
public static int checkGt(int actual, int required, String item) {
checkBool(actual > required, "expected " + item + " greater than " + required + " but got " + actual);
return actual;
}
public static int checkGte(int actual, int required, String item) {
checkBool(actual >= required, "expected " + item + " greater than equal to " + required + " but got " + actual);
return actual;
}
public static int checkLte(int actual, int required, String item) {
checkBool(actual <= required, "expected " + item + " less than equal to " + required + " but got " + actual);
return actual;
}
public static boolean checkBetween(long actual, long lower, long upper, boolean includeLower, boolean includeUpper) {
return includeLower ? (includeUpper ? actual >= lower && actual <= upper : actual >= lower && actual < upper)
: (includeUpper ? actual > lower && actual <= upper : actual > lower && actual < upper);
}
public static String checkNotNullOrEmpty(String actual, String item) {
checkBool(!isNullOrEmpty(actual), "expected " + item + " not to be empty or null");
return actual;
}
public static K checkContains(Map map, K key) { checkBool(map.containsKey(key), key + " is missing in the map"); return key; }
public static E checkContains(Collection coll, E item, String message) { checkBool(coll.contains(item), message); return item; }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy