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

package org.sablecc.objectmacro.intermediate.macro;

import java.util.*;

class MacroValue
        extends Value {

    private final List<Macro> macros;

    MacroValue(
            List<Macro> macros,
            Context context) {

        super(context);
        this.macros = macros;
    }

    @Override
    String build() {

        if (this.cache != null) {
            return this.cache;
        }

        StringBuilder sb = new StringBuilder();
        int i = 0;
        int nb_macros = this.macros.size();

        if (this.dNone != null) {
            sb.append(this.dNone.apply(i, "", nb_macros));
        }

        for (Macro macro : this.macros) {
            String expansion = macro.build(this.context);

            if (this.dBeforeFirst != null) {
                expansion = this.dBeforeFirst.apply(i, expansion, nb_macros);
            }

            if (this.dAfterLast != null) {
                expansion = this.dAfterLast.apply(i, expansion, nb_macros);
            }

            if (this.dSeparator != null) {
                expansion = this.dSeparator.apply(i, expansion, nb_macros);
            }

            sb.append(expansion);
            i++;
        }

        this.cache = sb.toString();
        return this.cache;
    }
}
