001package org.clafer.ir;
002
003import org.clafer.common.Check;
004import org.clafer.domain.BoolDomain;
005
006/**
007 *
008 * @author jimmy
009 */
010public class IrSetEquality extends IrAbstractBool {
011
012    private final IrSetExpr left;
013    private final Op op;
014    private final IrSetExpr right;
015
016    IrSetEquality(IrSetExpr left, Op op, IrSetExpr right, BoolDomain domain) {
017        super(domain);
018        this.left = Check.notNull(left);
019        this.op = Check.notNull(op);
020        this.right = Check.notNull(right);
021    }
022
023    public IrSetExpr getLeft() {
024        return left;
025    }
026
027    public Op getOp() {
028        return op;
029    }
030
031    public IrSetExpr getRight() {
032        return right;
033    }
034
035    @Override
036    public IrBoolExpr negate() {
037        return new IrSetEquality(left, op.negate(), right, getDomain().invert());
038    }
039
040    @Override
041    public boolean isNegative() {
042        return false;
043    }
044
045    @Override
046    public <A, B> B accept(IrBoolExprVisitor<A, B> visitor, A a) {
047        return visitor.visit(this, a);
048    }
049
050    @Override
051    public <A, B> B accept(IrIntExprVisitor<A, B> visitor, A a) {
052        return visitor.visit(this, a);
053    }
054
055    @Override
056    public boolean equals(Object obj) {
057        if (obj instanceof IrSetEquality) {
058            IrSetEquality other = (IrSetEquality) obj;
059            return left.equals(other.left) && op.equals(other.op) && right.equals(other.right) && super.equals(other);
060        }
061        return false;
062    }
063
064    @Override
065    public int hashCode() {
066        // op.hashCode() can change between runs which makes the output change
067        // every time.
068        return left.hashCode() ^ op.ordinal() ^ right.hashCode();
069    }
070
071    @Override
072    public String toString() {
073        return left + " " + op.getSyntax() + " " + right;
074    }
075
076    public static enum Op {
077
078        Equal("="),
079        NotEqual("!=");
080        private final String syntax;
081
082        Op(String syntax) {
083            this.syntax = syntax;
084        }
085
086        public String getSyntax() {
087            return syntax;
088        }
089
090        public Op negate() {
091            switch (this) {
092                case Equal:
093                    return NotEqual;
094                case NotEqual:
095                    return Equal;
096                default:
097                    throw new IllegalStateException();
098            }
099        }
100    }
101}