package accrue.analysis;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import polyglot.frontend.AbstractPass;
import polyglot.frontend.ExtensionInfo;
import polyglot.frontend.goals.Goal;
import accrue.analysis.goals.MissingCodeReportGoal;

public class MissingCodeReportPass extends AbstractPass {
    private static Map<ExtensionInfo, MissingCodeReportPass> singletons =
            new HashMap<ExtensionInfo, MissingCodeReportPass>();
    private final String reportFile;

    public static MissingCodeReportPass singleton(ExtensionInfo extInfo,
            String reportFile) {
        MissingCodeReportPass singleton = singletons.get(extInfo);
        if (singleton == null) {
            singleton =
                    new MissingCodeReportPass(MissingCodeReportGoal.singleton(extInfo,
                                                                              reportFile),
                                              reportFile);
            singletons.put(extInfo, singleton);
        }
        return singleton;
    }

    public MissingCodeReportPass(Goal goal, String reportFile) {
        super(goal);
        this.reportFile = reportFile;
    }

    @Override
    public boolean run() {
        if (reportFile != null) {
            try {
                MissingCodeReporter.reportToFile(reportFile);
            }
            catch (IOException ioe) {
                MissingCodeReporter.reportToStdErr();
            }
        }
        else {
            MissingCodeReporter.reportToStdErr();
        }
        return true;
    }

}
