All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.bhowell2.apilib.checks.ArrayChecks Maven / Gradle / Ivy

The newest version!
package io.github.bhowell2.apilib.checks;

import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;

/**
 * Common checks for an array.
 * @author Blake Howell
 */
public final class ArrayChecks {

	public static  Check checkSize(Check sizeCheck) {
		return ary -> sizeCheck.check(ary.length);
	}

	/**
	 * Creates a check that ensures each value is unique within the array.
	 * @param failureMessage message to return when the index is not unique
	 * @param  the 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 array.
	 * @param uniqueValRetriever function that retrieves the value that should be unique in the array
	 *                           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 array -> {
			Set uniqueSet = new HashSet<>();
			for (T idx : array) {
				if (!uniqueSet.add(uniqueValRetriever.apply(idx))) {
					return Check.Result.failure(failureMessage);
				}
			}
			return Check.Result.success();
		};
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy