class KieMemoryCompiler {

    public static Map<String, Class<?>> compile(Map<String, String> classNameSourceMap, ClassLoader classLoader) {
        Map<String, KieMemoryCompilerSourceCode> sourceCodes = classNameSourceMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
                                                                                                                               entry -> new KieMemoryCompilerSourceCode(entry.getKey(), entry.getValue())));
        KieMemoryCompilerClassLoader kieMemoryCompilerClassLoader = new KieMemoryCompilerClassLoader(classLoader);
        DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>();
        KieMemoryCompilerFileManager fileManager = new KieMemoryCompilerFileManager(JAVA_COMPILER.getStandardFileManager(null, null, null), kieMemoryCompilerClassLoader);
        JavaCompiler.CompilationTask task = JAVA_COMPILER.getTask(null, fileManager, collector, OPTIONS, null, sourceCodes.values());

        boolean compilationSuccess = task.call();
        boolean hasCompilerError = collector.getDiagnostics().stream().anyMatch(d -> d.getKind().equals(Diagnostic.Kind.ERROR));
        if (!compilationSuccess || hasCompilerError) {
            compilerError(collector);
        }

        Map<String, Class<?>> toReturn = new HashMap<>();
        for (String className : sourceCodes.keySet()) {
            try {
                toReturn.put(className, kieMemoryCompilerClassLoader.loadClass(className));
            } catch (ClassNotFoundException e) {
                throw new KieMemoryCompilerException(e.getMessage(), e);
            }
        }
        return toReturn;
    }

    private  KieMemoryCompiler();

}