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

org.joo.libra.sql.functional.AbstractFunctionalMatchPredicate Maven / Gradle / Ivy

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

import java.util.Collection;

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

public abstract class AbstractFunctionalMatchPredicate implements Predicate {

	protected HasValue list;
	protected String indexName;
	protected Predicate conditionPredicate;

	public AbstractFunctionalMatchPredicate(HasValue list, String indexName, Predicate conditionPredicate) {
		this.list = list;
		this.indexName = indexName;
		this.conditionPredicate = conditionPredicate;
	}

	@Override
	public boolean satisfiedBy(PredicateContext context) throws PredicateExecutionException {
		Object listValue = list.getValue(context);
		if (listValue == null)
			return satisfiesAsArray(new Object[0], context);
		if (listValue instanceof Object[])
			return satisfiesAsArray((Object[]) listValue, context);
		if (listValue instanceof Collection)
			return satisfiesAsCollection((Collection) listValue, context);
		return false;
	}

	protected abstract boolean satisfiesAsCollection(Collection listValue, PredicateContext context);

	protected abstract boolean satisfiesAsArray(Object[] listValue, PredicateContext context);

	protected boolean satisfiedBy(Object value, PredicateContext context) {
		PredicateContext cloneContext = context != null ? context.clone() : null;
		cloneContext.setTempVariable(indexName, value);
		return conditionPredicate.satisfiedBy(cloneContext);
	}
}