@Override public Module apply(final TopologicalSort.Node input) { if (input == null) { return null; } ModuleOrModuleBuilder moduleOrModuleBuilder = ((ModuleNodeImpl) input).getReference(); return moduleOrModuleBuilder.getModule(); }
public static List<ModuleBuilder> sortWithContext( final SchemaContext context, final ModuleBuilder... builders) { List<ModuleOrModuleBuilder> all = ModuleOrModuleBuilder.fromAll(context.getModules(), asList(builders)); List<TopologicalSort.Node> sorted = sortInternal(all); // Cast to ModuleBuilder from Node if possible and return return Lists.transform( sorted, new Function<TopologicalSort.Node, ModuleBuilder>() { @Override public ModuleBuilder apply(final TopologicalSort.Node input) { if (input == null) { return null; } ModuleOrModuleBuilder moduleOrModuleBuilder = ((ModuleNodeImpl) input).getReference(); if (moduleOrModuleBuilder.isModuleBuilder()) { return moduleOrModuleBuilder.getModuleBuilder(); } else { return null; } } }); }
/** * Topological sort of module dependency graph. * * @return Sorted list of Modules. Modules can be further processed in returned order. */ public static List<Module> sort(final Module... modules) { List<TopologicalSort.Node> sorted = sortInternal( ModuleOrModuleBuilder.fromAll(asList(modules), Collections.<ModuleBuilder>emptyList())); // Cast to Module from Node and return return Lists.transform(sorted, TOPOLOGY_FUNCTION); }
/** Extract dependencies from module builders or modules to fill dependency graph */ private static void processModules( final Map<String, Map<Date, ModuleNodeImpl>> moduleGraph, final Iterable<ModuleOrModuleBuilder> builders) { // Process nodes for (ModuleOrModuleBuilder momb : builders) { String name; Date rev; if (momb.isModule()) { name = momb.getModule().getName(); rev = momb.getModule().getRevision(); } else { name = momb.getModuleBuilder().getName(); rev = momb.getModuleBuilder().getRevision(); } if (rev == null) { rev = DEFAULT_REVISION; } if (moduleGraph.get(name) == null) { moduleGraph.put(name, Maps.<Date, ModuleNodeImpl>newHashMap()); } if (moduleGraph.get(name).get(rev) != null) { ex(String.format("Module:%s with revision:%s declared twice", name, formatRevDate(rev))); } moduleGraph.get(name).put(rev, new ModuleNodeImpl(name, rev, momb)); } }
public static List<ModuleBuilder> sort(final Collection<ModuleBuilder> builders) { List<TopologicalSort.Node> sorted = sortInternal(ModuleOrModuleBuilder.fromAll(Collections.<Module>emptySet(), builders)); return Lists.transform(sorted, NODE_TO_MODULEBUILDER); }
/** Extract module:revision from module builders */ private static void processDependencies( final Map<String, Map<Date, ModuleNodeImpl>> moduleGraph, final Iterable<ModuleOrModuleBuilder> mmbs) { Map<URI, ModuleOrModuleBuilder> allNS = new HashMap<>(); // Create edges in graph for (ModuleOrModuleBuilder mmb : mmbs) { Map<String, Date> imported = Maps.newHashMap(); String fromName; Date fromRevision; Collection<ModuleImport> imports; URI ns; if (mmb.isModule()) { Module module = mmb.getModule(); fromName = module.getName(); fromRevision = module.getRevision(); imports = module.getImports(); ns = module.getNamespace(); } else { ModuleBuilder moduleBuilder = mmb.getModuleBuilder(); fromName = moduleBuilder.getName(); fromRevision = moduleBuilder.getRevision(); imports = moduleBuilder.getImports().values(); ns = moduleBuilder.getNamespace(); } // check for existence of module with same namespace if (allNS.containsKey(ns)) { ModuleOrModuleBuilder mod = allNS.get(ns); String name = null; Date revision = null; if (mod.isModule()) { name = mod.getModule().getName(); revision = mod.getModule().getRevision(); } else if (mod.isModuleBuilder()) { name = mod.getModuleBuilder().getName(); revision = mod.getModuleBuilder().getRevision(); } if (!(fromName.equals(name))) { LOGGER.warn( "Error while sorting module [{}, {}]: module with same namespace ({}) already loaded: [{}, {}]", fromName, fromRevision, ns, name, revision); } } else { allNS.put(ns, mmb); } // no need to check if other Type of object, check is performed in // process modules if (fromRevision == null) { fromRevision = DEFAULT_REVISION; } for (ModuleImport imprt : imports) { String toName = imprt.getModuleName(); Date toRevision = imprt.getRevision() == null ? DEFAULT_REVISION : imprt.getRevision(); ModuleNodeImpl from = moduleGraph.get(fromName).get(fromRevision); ModuleNodeImpl to = getModuleByNameAndRevision(moduleGraph, fromName, fromRevision, toName, toRevision); /* * Check imports: If module is imported twice with different * revisions then throw exception */ if (imported.get(toName) != null && !imported.get(toName).equals(toRevision) && !imported.get(toName).equals(DEFAULT_REVISION) && !toRevision.equals(DEFAULT_REVISION)) { ex( String.format( "Module:%s imported twice with different revisions:%s, %s", toName, formatRevDate(imported.get(toName)), formatRevDate(toRevision))); } imported.put(toName, toRevision); from.addEdge(to); } } }