io.github.bhowell2.apilib.checks.IntegerChecks Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api-lib Show documentation
Show all versions of api-lib Show documentation
API verification library.
The newest version!
package io.github.bhowell2.apilib.checks;
import java.util.Arrays;
import java.util.stream.Collectors;
/**
* @author Blake Howell
*/
public final class IntegerChecks {
private IntegerChecks() {} // no instantiation
/**
* Check to ensure the parameter is an Integer.
*/
public static final Check IS_INTEGER = Check.alwaysPass(Integer.class);
public static Check valueGreaterThan(int i) {
return input -> {
if (input > i) {
return Check.Result.success();
} else {
return Check.Result.failure("Must be greater than " + i + ".");
}
};
}
public static Check valueGreaterThanOrEqualTo(int i) {
return input -> {
if (input >= i) {
return Check.Result.success();
} else {
return Check.Result.failure("Must be greater than or equal to " + i + ".");
}
};
}
public static Check valueLessThan(int i) {
return input -> {
if (input < i) {
return Check.Result.success();
} else {
return Check.Result.failure("Must be less than " + i + ".");
}
};
}
public static Check valueLessThanOrEqualTo(int i) {
return input -> {
if (input <= i) {
return Check.Result.success();
} else {
return Check.Result.failure("Must be less than or equal to " + i + ".");
}
};
}
public static Check valueEqualTo(int i) {
return input -> {
if (input == i) {
return Check.Result.success();
} else {
return Check.Result.failure("Must be equal to " + i + ".");
}
};
}
/**
* Checks whether or not the value is equal to one of the values in the list.
* @param acceptableInts
* @return
*/
public static Check valueEqualTo(int[] acceptableInts) {
String acceptableListForFailureMsg = Arrays.stream(acceptableInts)
.mapToObj(Integer::toString)
.collect(Collectors.joining(", "));
return input -> {
for (int i : acceptableInts) {
if (input == i) {
return Check.Result.success();
}
}
return Check.Result.failure("Does not equal any of the acceptable integers: " + acceptableListForFailureMsg + ".");
};
}
public static Check valueIsEven() {
return input -> input % 2 == 0
?
Check.Result.success()
:
Check.Result.failure("Must be an even integer.");
}
public static Check valueIsOdd() {
return input -> input % 2 != 0
?
Check.Result.success()
:
Check.Result.failure("Must be an odd integer.");
}
}