// 
// Decompiled by Procyon v0.6.0
// 

package com.google.common.flogger.parser;

import com.google.common.flogger.parameter.Parameter;
import com.google.common.flogger.util.Checks;
import com.google.common.flogger.backend.TemplateContext;

public abstract class MessageBuilder<T>
{
    private final TemplateContext context;
    private int pmask;
    private int maxIndex;
    
    public MessageBuilder(final TemplateContext context) {
        this.pmask = 0;
        this.maxIndex = -1;
        this.context = Checks.checkNotNull(context, "context");
    }
    
    public final MessageParser getParser() {
        return this.context.getParser();
    }
    
    public final String getMessage() {
        return this.context.getMessage();
    }
    
    public final int getExpectedArgumentCount() {
        return this.maxIndex + 1;
    }
    
    public final void addParameter(final int termStart, final int termEnd, final Parameter param) {
        if (param.getIndex() < 32) {
            this.pmask |= 1 << param.getIndex();
        }
        this.maxIndex = Math.max(this.maxIndex, param.getIndex());
        this.addParameterImpl(termStart, termEnd, param);
    }
    
    protected abstract void addParameterImpl(final int p0, final int p1, final Parameter p2);
    
    protected abstract T buildImpl();
    
    public final T build() {
        this.getParser().parseImpl((MessageBuilder<Object>)this);
        if ((this.pmask & this.pmask + 1) != 0x0 || (this.maxIndex > 31 && this.pmask != -1)) {
            final int firstMissing = Integer.numberOfTrailingZeros(~this.pmask);
            throw ParseException.generic(String.format("unreferenced arguments [first missing index=%d]", firstMissing), this.getMessage());
        }
        return this.buildImpl();
    }
}
