001package org.clafer.ast.analysis;
002
003import java.util.Map;
004import org.clafer.ast.AstAbstractClafer;
005import org.clafer.ast.AstClafer;
006import org.clafer.common.Check;
007
008/**
009 *
010 * @author jimmy
011 */
012public class Offsets {
013
014    private final AstAbstractClafer sup;
015    private final Map<AstClafer, Integer> offsets;
016    private final AstClafer[] reverseOffsets;
017
018    Offsets(AstAbstractClafer sup, Map<AstClafer, Integer> offsets, AstClafer[] reverseOffsets) {
019        this.sup = Check.notNull(sup);
020        this.offsets = Check.notNull(offsets);
021        this.reverseOffsets = Check.noNulls(reverseOffsets);
022    }
023
024    public int getOffset(AstClafer sub) {
025        Integer offset = offsets.get(sub);
026        if (offset == null) {
027            throw new AnalysisException(sub + " is not a direct sub clafer of " + sup);
028        }
029        return offset.intValue();
030    }
031
032    public AstClafer getClafer(int offset) {
033        return reverseOffsets[offset];
034    }
035
036    @Override
037    public String toString() {
038        return sup + "=>" + offsets;
039    }
040}