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

org.joo.libra.logic.OrPredicate Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
package org.joo.libra.logic;

import java.util.Arrays;

import org.joo.libra.Predicate;
import org.joo.libra.PredicateContext;
import org.joo.libra.common.CompositionPredicate;
import org.joo.libra.support.exceptions.PredicateExecutionException;

/**
 * Represents a predicate with or rule. It will be satisfied by if
 * and only if any of the child predicates is satisfied. If the child is empty,
 * it will always return true.
 * 
 * @author griever
 *
 */
public class OrPredicate implements CompositionPredicate {

	private Predicate[] predicates;

	public OrPredicate(final Predicate... predicates) {
		this.predicates = predicates;
	}

	@Override
	public boolean satisfiedBy(final PredicateContext context) throws PredicateExecutionException {
		if (predicates.length == 0)
			return true;
		for (Predicate predicate : predicates) {
			if (predicate.satisfiedBy(context))
				return true;
		}
		return false;
	}

	public String toString() {
		String[] predicatesAsString = Arrays.stream(predicates).map(p -> p.toString())
				.toArray(size -> new String[size]);
		return "OR(" + String.join(", ", predicatesAsString) + ")";
	}

	public Predicate[] getPredicates() {
		return predicates;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy