001package org.clafer.assertion;
002
003import java.util.concurrent.atomic.AtomicInteger;
004import org.clafer.ast.AstBoolExpr;
005import org.clafer.frontend.SourceSpan;
006import org.clafer.common.Check;
007
008/**
009 *
010 * @author jimmy
011 */
012public class Assertion {
013
014    private static final AtomicInteger idFactory = new AtomicInteger();
015    private final int id = idFactory.getAndIncrement();
016    private final AstBoolExpr expr;
017    private final SourceSpan span;
018
019    public Assertion(AstBoolExpr expr) {
020        this(expr, null);
021    }
022
023    /**
024     * @param expr the expression to assert
025     * @param span where the assertion is in the source model, or {@code null}
026     * if unknown
027     */
028    public Assertion(AstBoolExpr expr, SourceSpan span) {
029        this.expr = Check.notNull(expr);
030        this.span = span;
031    }
032
033    /**
034     * Returns the expression to assert.
035     *
036     * @return the expression to assert
037     */
038    public AstBoolExpr getExpr() {
039        return expr;
040    }
041
042    public boolean hasSpan() {
043        return span != null;
044    }
045
046    /**
047     * Returns where the assertion is in the source model.
048     *
049     * @return the source span, or {@code null} if unknown
050     */
051    public SourceSpan getSpan() {
052        return span;
053    }
054
055    @Override
056    public boolean equals(Object obj) {
057        return this == obj;
058    }
059
060    @Override
061    public int hashCode() {
062        return id;
063    }
064
065    @Override
066    public String toString() {
067        return "assert " + expr + (span == null ? "" : " at " + span);
068    }
069}