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

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

package io.github.bhowell2.apilib.checks;

import io.github.bhowell2.apilib.checks.utils.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Provides conditional functionality for checks. Note, there is no AND conditional check, because
 * an AND conditional check would be what occurs when using required parameters.
 * @author Blake Howell
 */
public final class ConditionalChecks {

	/**
	 * Creates check which ensures at least one of the checks provided passes. Combines failure
	 * messages from each check if they all fail, joining them with the string " OR ".
	 *
	 * @param checks one of these checks must pass
	 * @param  the parameter type
	 * @return a check that passes if one of the supplied checks passes. otherwise it will fail.
	 */
	@SafeVarargs
	static  Check orConditionalCheck(Check... checks) {
		return orConditionalCheck(null, checks);
	}

	/**
	 * Creates check which ensures at least one of the checks provided passes.
	 *
	 * @param failureMessage overrides failure messages returned from checks. if this is null then
	 *                       the error messages from each check will be collected and combined with
	 *                       the string " OR ".
	 * @param checks one of these checks must pass
	 * @param  the parameter type
	 * @return a check that passes if one of the supplied checks passes. otherwise it will fail.
	 */
	@SafeVarargs
	static  Check orConditionalCheck(String failureMessage, Check... checks) {
		CollectionUtils.requireNonNullEntries(checks);
		return param -> {
			/*
			 * Used to combine error messages if an explicit failure message is not provided.
			 * If a failure
			 * */
			List combinedErrorMessages = null;
			for (int i = 0; i < checks.length; i++) {
				Check.Result result = checks[i].check(param);
				if (result.failed()) {
					if (failureMessage == null) {
						if (combinedErrorMessages == null) {
							combinedErrorMessages = new ArrayList<>();
						}
						combinedErrorMessages.add(result.failureMessage);
					}
				} else {
					// if any single check passes this is successful
					return Check.Result.success();
				}
			}
			if (failureMessage != null) {
				return Check.Result.failure(failureMessage);
			}
			return Check.Result.failure(String.join(" OR ", combinedErrorMessages));
		};
	}

//	/**
//	 * Behaves like an XOR gate, which ensures that only one of the checks passes, but not both.
//	 *
//	 * Result:
//	 * Both fail: check fails
//	 * One passes: check passes
//	 * Both pass: check fails
//	 *
//	 * @param failureMessage message to return
//	 * @param check1
//	 * @param check2
//	 * @param 
//	 * @return
//	 */
//	static  Check xorConditionalCheck(String failureMessage, Check check1, Check check2) {
//		return param -> {
//			Check.Result result1 = check1.check(param);
//			Check.Result result2 = check2.check(param);
//			if (result1.failed() && result2.failed() || (result1.successful() && result2.successful())) {
//				return Check.Result.failure(failureMessage);
//			} else {
//				return Check.Result.success();
//			}
//		};
//	}

	/**
	 * Only one of the checks can pass, otherwise the failure message is returned.
	 * @param failureMessage message returned on failure
	 * @param checks which checks should exclusively pass one another.
	 * @param  the parameter type
	 * @return a check that passes if only one of the provided checks passes
	 */
	@SafeVarargs
	static  Check exclusiveConditionalCheck(String failureMessage, Check... checks) {
		CollectionUtils.requireNonNullEntries(checks);
		return param -> {
			boolean successfulResult = false;
			for (Check c : checks) {
				Check.Result result = c.check(param);
				if (result.successful()) {
					if (successfulResult) {
						return Check.Result.failure(failureMessage);
					}
					successfulResult = true;
				}
			}
			return successfulResult ? Check.Result.success() : Check.Result.failure(failureMessage);
		};
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy