io.github.bhowell2.apilib.checks.ListChecks 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.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
/**
* @author Blake Howell
*/
public class ListChecks {
public static Check> checkSize(Check sizeCheck) {
return list -> sizeCheck.check(list.size());
}
/**
* Creates a check that ensures some value is unique within the list.
* @param failureMessage message to return when the index is not unique
* @param the list's index type
* @return the created check to ensure the index is unique
*/
public static Check> checkUnique(String failureMessage) {
return checkUnique(v -> v, failureMessage);
}
/**
* Creates a check that ensures some value is unique within the list.
* @param uniqueValRetriever function that retrieves the value that should be unique in the list
* from each index
* @param failureMessage message to return when the index is not unique
* @param the list's index type type
* @param the retrieved type (from each index of type T)
* @return the created check to ensure the retrieved value at each index is unique
*/
public static Check> checkUnique(Function uniqueValRetriever, String failureMessage) {
return list -> {
Set uniqueSet = new HashSet<>();
for (T idx : list) {
if (!uniqueSet.add(uniqueValRetriever.apply(idx))) {
return Check.Result.failure(failureMessage);
}
}
return Check.Result.success();
};
}
}