/* This file was generated by SableCC's ObjectMacro. */

package org.sablecc.objectmacro.codegeneration.java.macro;

import java.util.*;

public class MFactoryComparison
        extends Macro {

    private Map<Context, StringValue> list_VarName = new LinkedHashMap<>();

    MFactoryComparison(
            Macros macros) {

        setMacros(macros);
        this.list_VarName = new LinkedHashMap<>();
    }

    void setVarName(
            Context context,
            StringValue value) {

        if (value == null) {
            throw new RuntimeException("value cannot be null here");
        }

        this.list_VarName.put(context, value);
    }

    private String buildVarName(
            Context context) {

        StringValue stringValue = this.list_VarName.get(context);
        return stringValue.build();
    }

    private StringValue getVarName(
            Context context) {

        return this.list_VarName.get(context);
    }

    @Override
    void apply(
            InternalsInitializer internalsInitializer) {

        internalsInitializer.setFactoryComparison(this);
    }

    @Override
    String build(
            Context context) {

        CacheBuilder cache_builder = this.cacheBuilders.get(context);

        if (cache_builder == null) {
            cache_builder = new CacheBuilder();
        }
        else if (cache_builder.getExpansion() == null) {
            throw new InternalException("Cycle detection detected lately");
        }
        else {
            return cache_builder.getExpansion();
        }

        this.cacheBuilders.put(context, cache_builder);
        List<String> indentations = new LinkedList<>();

        StringBuilder sb0 = new StringBuilder();

        sb0.append("if(this.getMacros() != ");
        sb0.append(buildVarName(context));
        sb0.append(".getMacros()) ");
        sb0.append("{");
        sb0.append(LINE_SEPARATOR);
        sb0.append("    throw ObjectMacroException.diffMacros();");
        sb0.append(LINE_SEPARATOR);
        sb0.append("}");
        cache_builder.setExpansion(sb0.toString());
        return sb0.toString();
    }

    private void setMacros(
            Macros macros) {

        if (macros == null) {
            throw new InternalException("macros cannot be null");
        }

        this.macros = macros;
    }
}
