/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.annotations.javac;

import java.util.ArrayList;
import java.util.List;
import javax.tools.Diagnostic;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class JavaCompilationResult {
    
    private final boolean success;
    
    private final List<Diagnostic<? extends JavaFileObject>> diagnostics;
    
    private final List<JavaFileObject> files;

    JavaCompilationResult(
        boolean successful, 
        List<Diagnostic<? extends JavaFileObject>> diagnostics,
        List<JavaFileObject> generatedFiles
    ) {
        this.success = successful && !diagnostics.stream().anyMatch(d -> d.getKind() == Diagnostic.Kind.ERROR);
        this.diagnostics = new ArrayList<>(diagnostics);
        this.files = new ArrayList<>(generatedFiles);
    }

    public boolean success() {
        return success;
    }

    public List<Diagnostic<? extends JavaFileObject>> diagnostics() {
        return diagnostics;
    }
    
    public List<JavaFileObject> files() {
        return files;
    }
    
    public JavaFileObject file(JavaFileManager.Location location, String packageName, String relativeName) {
        return file(JavaCompilationFM.getName(location, packageName, relativeName));
    }
    
    public JavaFileObject file(JavaFileManager.Location location, String className, JavaFileObject.Kind kind) {
        return file(JavaCompilationFM.getName(location, className, kind));
    }

    private JavaFileObject file(String name) {
        return files.stream().filter(o -> name.equals(o.getName())).findFirst().orElse(null);
    }
    
}
