package org.boris.expr.function.excel;
import org.boris.expr.Expr;
import org.boris.expr.ExprArray;
import org.boris.expr.ExprBoolean;
import org.boris.expr.ExprEvaluatable;
import org.boris.expr.ExprException;
import org.boris.expr.ExprMissing;
import org.boris.expr.ExprNumber;
import org.boris.expr.function.AbstractFunction;
public class AND extends AbstractFunction
{
public Expr evaluate(Expr[] args) throws ExprException {
if (args.length == 0)
throw new ExprException(
"AND function requires at least one argument");
for (Expr a : args) {
if (!eval(a, true))
return ExprBoolean.FALSE;
}
return ExprBoolean.TRUE;
}
protected boolean eval(Expr a, boolean strict) throws ExprException {
if (a == null)
return true;
if (a instanceof ExprEvaluatable) {
a = ((ExprEvaluatable) a).evaluate();
}
if (a instanceof ExprNumber) {
return ((ExprNumber) a).booleanValue();
}
if (a instanceof ExprMissing)
return true;
if (a instanceof ExprArray) {
ExprArray arr = (ExprArray) a;
int rows = arr.rows();
int cols = arr.columns();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (!eval(arr.get(i, j), false))
return false;
}
}
}
if (strict)
throw new ExprException("Unexpected argument to " +
getClass().getSimpleName() + ": " + a);
return false;
}
}