Example #1
0
  public static <T extends GraphNode<T>> Collection<Collection<T>> getConnectedComponents(
      Set<T> constraints) {
    Collection<T> unvisited = HashSetFactory.make();

    for (T constraint : constraints) {
      unvisited.add(constraint);
    }

    // DFS of the constraints graph
    LinkedList<T> rPostorder = new LinkedList<T>();
    while (!unvisited.isEmpty()) {
      Iterator<T> it = unvisited.iterator();
      T constraint = it.next();
      DFS(constraint, unvisited, rPostorder, false);
    }

    // Set nodes numbering in reverse postorder
    int id = 0;
    for (Iterator<T> it = rPostorder.iterator(); it.hasNext(); id++) {
      T node = it.next();
      node.id = id;
    }

    // DFS of the transpose graph in reverse postorder
    LinkedList<Collection<T>> components = new LinkedList<Collection<T>>();
    while (!rPostorder.isEmpty()) {
      LinkedList<T> temp = new LinkedList<T>();
      T constraint = rPostorder.removeFirst();
      DFS(constraint, rPostorder, temp, true);
      SortedSet<T> component = new TreeSet<T>();
      component.addAll(temp);
      for (Iterator<T> it = component.iterator(); it.hasNext(); ) {
        constraint = it.next();
        SortedSet<T> tempSet = new TreeSet<T>();
        for (Iterator<T> itDeps = constraint.dependents.iterator(); itDeps.hasNext(); ) {
          T dependent = itDeps.next();
          if (component.contains(dependent)) {
            tempSet.add(dependent);
            // constraint.strongDependents.add(dependent);
          }
        }
        constraint.setStrongDependents(tempSet);
      }
      components.add(new LinkedList<T>(component));
    }
    return components;
  }
  // Naive implementation, which traverses all elements in the list
  private <T extends Resource> Optional<T> lookup(
      TransactionalMap<DiscreteResource, Set<Resource>> map, T resource) {
    // if it is root, always returns itself
    if (!resource.parent().isPresent()) {
      return Optional.of(resource);
    }

    Set<Resource> values = map.get(resource.parent().get());
    if (values == null) {
      return Optional.empty();
    }

    @SuppressWarnings("unchecked")
    Optional<T> result =
        values.stream().filter(x -> x.id().equals(resource.id())).map(x -> (T) x).findFirst();
    return result;
  }
 /**
  * Creates ResourceConsumerId instance from Identifier object.
  *
  * @param <T> resource consumer class type
  * @param id identifier object backed by Long value
  * @return created ResourceConsumerId object
  */
 public static <T extends Identifier<Long> & ResourceConsumer> ResourceConsumerId of(T id) {
   return new ResourceConsumerId(id.id(), id.getClass());
 }
Example #4
0
 public Builder<T> setId(int Id) {
   item.id = Id;
   return this;
 }
 public AbstractLocationScopedResource(T resource, String cloud, String credential) {
   super(resource, cloud, credential);
   IdScopedByLocation idScopedByLocation = IdScopeByLocations.from(resource.id());
   this.id = idScopedByLocation.getId();
   this.location = idScopedByLocation.getLocationId();
 }