protected void addResource(Resource resource) { for (Capability cap : resource.getCapabilities(null)) { String ns = cap.getNamespace(); CapabilitySet cs = capSets.get(ns); if (cs == null) { cs = new CapabilitySet(Collections.singletonList(ns)); capSets.put(ns, cs); } cs.addCapability(cap); } resources.add(resource); }
protected void addResource(Resource resource) { for (Capability cap : resource.getCapabilities(null)) { String ns = cap.getNamespace(); CapabilitySet cs = capSets.get(ns); if (cs == null) { cs = new CapabilitySet(Collections.singletonList(ns)); capSets.put(ns, cs); } cs.addCapability(cap); } resources.add(resource); }
/** * Sort {@link Resource} based on their {@link Requirement}s and {@link Capability}s. * * @param resources the resource to sort. * @param <T> the resources type. * @return sorted collection of resources. */ public static <T extends Resource> Collection<T> sort(Collection<T> resources) { Set<String> namespaces = new HashSet<>(); for (Resource r : resources) { for (Capability cap : r.getCapabilities(null)) { namespaces.add(cap.getNamespace()); } } CapabilitySet capSet = new CapabilitySet(new ArrayList<>(namespaces)); for (Resource r : resources) { for (Capability cap : r.getCapabilities(null)) { capSet.addCapability(cap); } } Set<T> sorted = new LinkedHashSet<>(); Set<T> visited = new LinkedHashSet<>(); for (T r : resources) { visit(r, visited, sorted, capSet); } return sorted; }