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

net.sf.xolite.expressions.jaxb_choice.CompositeExpression Maven / Gradle / Ivy

package net.sf.xolite.expressions.jaxb_choice;


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

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;


/**
 * An expression that combines other expression by doing a logical operation. 
* This abstract class does not define the logical operation (it will be done by sub-classes). It is only there to handle the * 'plumbing' code of managing sub-expressions. * * @author BEROL */ public abstract class CompositeExpression extends BooleanExpression { private int minOperandCount; private int maxOperandCount; private List operands = new ArrayList(); protected CompositeExpression() { throw new UnsupportedOperationException("You sould not call this constructor"); } protected CompositeExpression(int minOp, int maxOp) { minOperandCount = minOp; maxOperandCount = maxOp; } @XmlElements( { @XmlElement(name = "false", type = FalseExpression.class), @XmlElement(name = "variable", type = VariableExpression.class), @XmlElement(name = "and", type = AndExpression.class), @XmlElement(name = "true", type = TrueExpression.class), @XmlElement(name = "not", type = NotExpression.class), @XmlElement(name = "or", type = OrExpression.class) }) public List getExpressionGroup() { return operands; } @Override public String toString() { StringBuilder sb = new StringBuilder("("); int len = getOperandCount(); for (int i = 0; i < len; i++) { if (i > 0) { sb.append(" "); sb.append(getOperationName()); sb.append(" "); } sb.append(getOperandAt(i)); } sb.append(")"); return sb.toString(); } protected int getOperandCount() { return operands.size(); } protected BooleanExpression getOperandAt(int index) { return operands.get(index); } protected void addOperands(BooleanExpression... ops) { for (BooleanExpression op : ops) { addOperand(op); } } protected void addOperand(BooleanExpression op) { if (op == null) throw new IllegalArgumentException("Added operand cannot be null"); if (getOperandCount() >= maxOperandCount) { throw new IllegalStateException("Cannot add new operand in operator <" + getOperationName() + "> because it must have at most " + maxOperandCount + " operands"); } operands.add(op); } protected abstract String getOperationName(); @Override public void validate(ExpressionContext context) { validateThis(); for (BooleanExpression expr : getExpressionGroup()) { expr.validate(context); } } protected void validateThis() { if (getOperandCount() < minOperandCount) { throw new IllegalStateException("Operator <" + getOperationName() + "> must have at least " + minOperandCount + " operands"); } if (getOperandCount() > maxOperandCount) { throw new IllegalStateException("Operator <" + getOperationName() + "> must have at most " + maxOperandCount + " operands"); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy