001package org.clafer.ir;
002
003import org.clafer.common.Check;
004
005/**
006 *
007 * @author jimmy
008 */
009public class IrIfThenElse extends IrAbstractBool implements IrBoolExpr {
010
011    private final IrBoolExpr antecedent;
012    private final IrBoolExpr consequent;
013    private final IrBoolExpr alternative;
014
015    public IrIfThenElse(IrBoolExpr antecedent, IrBoolExpr consequent, IrBoolExpr alternative, IrBoolDomain domain) {
016        super(domain);
017        this.antecedent = Check.notNull(antecedent);
018        this.consequent = Check.notNull(consequent);
019        this.alternative = Check.notNull(alternative);
020    }
021
022    public IrBoolExpr getAntecedent() {
023        return antecedent;
024    }
025
026    public IrBoolExpr getConsequent() {
027        return consequent;
028    }
029
030    public IrBoolExpr getAlternative() {
031        return alternative;
032    }
033
034    @Override
035    public IrBoolExpr negate() {
036        return new IrIfThenElse(antecedent, consequent.negate(), alternative.negate(), getDomain().invert());
037    }
038
039    @Override
040    public boolean isNegative() {
041        return false;
042    }
043
044    @Override
045    public <A, B> B accept(IrBoolExprVisitor<A, B> visitor, A a) {
046        return visitor.visit(this, a);
047    }
048
049    @Override
050    public <A, B> B accept(IrIntExprVisitor<A, B> visitor, A a) {
051        return visitor.visit(this, a);
052    }
053
054    @Override
055    public boolean equals(Object obj) {
056        if (obj instanceof IrIfThenElse) {
057            IrIfThenElse other = (IrIfThenElse) obj;
058            return antecedent.equals(other.antecedent)
059                    && consequent.equals(other.consequent)
060                    && alternative.equals(other.alternative)
061                    && super.equals(other);
062        }
063        return false;
064    }
065
066    @Override
067    public int hashCode() {
068        return antecedent.hashCode() ^ consequent.hashCode() ^ alternative.hashCode();
069    }
070
071    @Override
072    public String toString() {
073        return "if (" + antecedent + ") then (" + consequent + ") else (" + alternative + ")";
074    }
075}