001package org.clafer.ir;
002
003import java.util.Arrays;
004import org.clafer.common.Check;
005import org.clafer.common.Util;
006
007/**
008 *
009 * @author jimmy
010 */
011public class IrAnd extends IrAbstractBool implements IrBoolExpr {
012
013    private final IrBoolExpr[] operands;
014
015    IrAnd(IrBoolExpr[] operands, IrBoolDomain domain) {
016        super(domain);
017        this.operands = Check.noNullsNotEmpty(operands);
018    }
019
020    public IrBoolExpr[] getOperands() {
021        return operands;
022    }
023
024    @Override
025    public IrBoolExpr negate() {
026        IrBoolExpr[] negativeOperands = new IrBoolExpr[operands.length];
027        for (int i = 0; i < negativeOperands.length; i++) {
028            negativeOperands[i] = operands[i].negate();
029        }
030        return new IrOr(negativeOperands, getDomain().invert());
031    }
032
033    @Override
034    public boolean isNegative() {
035        return false;
036    }
037
038    @Override
039    public <A, B> B accept(IrBoolExprVisitor<A, B> visitor, A a) {
040        return visitor.visit(this, a);
041    }
042
043    @Override
044    public <A, B> B accept(IrIntExprVisitor<A, B> visitor, A a) {
045        return visitor.visit(this, a);
046    }
047
048    @Override
049    public boolean equals(Object obj) {
050        if (obj instanceof IrAnd) {
051            IrAnd other = (IrAnd) obj;
052            return Arrays.equals(operands, other.operands) && super.equals(other);
053        }
054        return false;
055    }
056
057    @Override
058    public int hashCode() {
059        return 7 * Arrays.hashCode(operands);
060    }
061
062    @Override
063    public String toString() {
064        return '(' + Util.intercalate(") & (", operands) + ')';
065    }
066}