Skip to content

Commit

Permalink
Minor changes to the Dependencies utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Nov 29, 2012
1 parent 188e8c3 commit 1711d7d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
/**
* When an addon is installed, another addons could be required. This object returns the necessary information for the
* installation of an addon to succeed, like required addons and dependencies
*
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*
*/
public class InstallRequest
{
Expand Down Expand Up @@ -111,7 +111,7 @@ public void perform()
private AddonEntry toAddonEntry(DependencyNode node)
{
Coordinate coord = node.getDependency().getCoordinate();
List<DependencyNode> forgeApi = Dependencies.collect(Dependencies.breadthFirstIterator(node),
DependencyNode forgeApi = Dependencies.selectFirst(Dependencies.breadthFirstIterator(node),
new DependencyNodeFilter()
{
@Override
Expand All @@ -124,9 +124,9 @@ public boolean accept(DependencyNode node)
});

String apiVersion = null;
if (forgeApi.size() > 0)
if (forgeApi != null)
{
apiVersion = forgeApi.get(0).getDependency().getCoordinate().getVersion();
apiVersion = forgeApi.getDependency().getCoordinate().getVersion();
}

return AddonEntry.from(coord.getGroupId() + ":" + coord.getArtifactId(), coord.getVersion(), apiVersion);
Expand All @@ -137,11 +137,11 @@ private void deploy(AddonEntry addon, DependencyNode root)
Coordinate coordinate = root.getDependency().getCoordinate();

dependencyResolver.resolveDependencyHierarchy(DependencyQueryBuilder.create(coordinate));
List<File> resourceJars = toResourceJars(Dependencies.collect(root, new LocalResourceFilter()));
List<File> resourceJars = toResourceJars(Dependencies.select(root, new LocalResourceFilter()));
resourceJars.add(dependencyResolver.resolveArtifact(DependencyQueryBuilder.create(coordinate)).getArtifact());

List<AddonDependency> addonDependencies =
toAddonDependencies(Dependencies.collect(root, new DirectAddonFilter(root)));
toAddonDependencies(Dependencies.select(root, new DirectAddonFilter(root)));

repository.deploy(addon, addonDependencies, resourceJars);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

/**
* Provides utility methods for working with {@link Dependency} and {@link DependencyNode} objects
*
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*
*/
public final class Dependencies
{
Expand All @@ -36,68 +36,72 @@ private Dependencies()
}

/**
* Returns the {@link DependencyNode} objects that satisfy the filter. The method
* {@link DependencyNode#getChildren()} is called for each found node.
*
* @param root a {@link DependencyNode} as the starting point
* Returns the first {@link DependencyNode} object found that satisfy the filter.
*
* @param nodeIterator A tree iterator
* @param filter the {@link DependencyNodeFilter} being used
* @return list of matched elements
*
* @return the first element that matches the filter. null if nothing is found
*
* @see #breadthFirstIterator(DependencyNode)
* @see #depthFirstIterator(DependencyNode)
* @see #preorderIterator(DependencyNode)
*/
public static List<DependencyNode> collect(Iterator<DependencyNode> nodeIterator, DependencyNodeFilter filter)
public static DependencyNode selectFirst(Iterator<DependencyNode> nodeIterator, DependencyNodeFilter filter)
{
List<DependencyNode> result = new ArrayList<DependencyNode>();
while (nodeIterator.hasNext())
{
DependencyNode node = nodeIterator.next();
if (filter.accept(node))
{
result.add(node);
return node;
}
}
return result;
return null;
}

/**
* Returns the {@link DependencyNode} objects that satisfy the filter. The method
* Returns a {@link List} of {@link DependencyNode} objects that satisfy the filter. The method
* {@link DependencyNode#getChildren()} is called for each found node.
*
*
* @param root a {@link DependencyNode} as the starting point
* @param filter the {@link DependencyNodeFilter} being used
* @return the {@link Collection} with the output
*
* @return list of matched elements
*
*/
public static List<DependencyNode> collect(DependencyNode node, DependencyNodeFilter filter)
public static List<DependencyNode> select(Iterator<DependencyNode> nodeIterator, DependencyNodeFilter filter)
{
List<DependencyNode> result = new ArrayList<DependencyNode>();
collect(node, filter, result);
while (nodeIterator.hasNext())
{
DependencyNode node = nodeIterator.next();
if (filter.accept(node))
{
result.add(node);
}
}
return result;
}

/**
* Returns the {@link DependencyNode} objects that satisfy the filter. The method
* {@link DependencyNode#getChildren()} is called for each found node.
*
* {@link DependencyNode#getChildren()} is called for each found node. The nodes are traversed using a pre-order
* iterator
*
* @param root a {@link DependencyNode} as the starting point
* @param filter the {@link DependencyNodeFilter} being used
* @param outputList the {@link Collection} with the output
*
* @return the {@link Collection} with the output
*
* @see #preorderIterator(DependencyNode)
*
*/
private static void collect(DependencyNode node, DependencyNodeFilter filter, Collection<DependencyNode> outputList)
public static List<DependencyNode> select(DependencyNode node, DependencyNodeFilter filter)
{
if (filter.accept(node))
{
outputList.add(node);
}
for (DependencyNode child : node.getChildren())
{
collect(child, filter, outputList);
}
return select(preorderIterator(node), filter);
}

/**
* Prints a tree-like structure for this object
*
*
* @param root
* @return
*/
Expand Down Expand Up @@ -129,10 +133,10 @@ private static void prettyPrint(DependencyNode node, StringBuilder builder, int
* Creates and returns an iterator that traverses the subtree rooted at this node in depth-first order. The first
* node returned by {@link Iterator#next()} is the leftmost leaf.
* <P>
*
*
* Modifying the tree by inserting, removing, or moving a node invalidates any iterators created before the
* modification.
*
*
* @see #breadthFirstIterator(DependencyNode)
* @see #preorderIterator(DependencyNode)
* @return an iterator for traversing the tree in depth-first order
Expand All @@ -146,10 +150,10 @@ public static Iterator<DependencyNode> depthFirstIterator(DependencyNode depende
* Creates and returns an iterator that traverses the subtree rooted at this node in breadth-first order. The first
* node returned by {@link Iterator#next()} is this node.
* <P>
*
*
* Modifying the tree by inserting, removing, or moving a node invalidates any iterators created before the
* modification.
*
*
* @see #depthFirstIterator(DependencyNode)
* @see #preorderIterator(DependencyNode)
* @return an enumeration for traversing the tree in breadth-first order
Expand All @@ -163,13 +167,13 @@ public static Iterator<DependencyNode> breadthFirstIterator(DependencyNode depen
* Creates and returns an iterator that traverses the subtree rooted at this node in preorder. The first node
* returned by {@link Iterator#next()} is this node.
* <P>
*
*
* Modifying the tree by inserting, removing, or moving a node invalidates any iterators created before the
* modification.
*
*
* @see #depthFirstIterator(DependencyNode)
* @see #breadthFirstIterator(DependencyNode)
*
*
* @return an enumeration for traversing the tree in breadth-first order
*/
public static Iterator<DependencyNode> preorderIterator(DependencyNode dependencyNode)
Expand All @@ -179,7 +183,7 @@ public static Iterator<DependencyNode> preorderIterator(DependencyNode dependenc

/**
* Checks if the {@link DependencyNode} object is holding a Forge Addon dependency
*
*
* @param node
* @return
*/
Expand All @@ -190,7 +194,7 @@ public static boolean isForgeAddon(DependencyNode node)

/**
* Check if the {@link Dependency} object is pointing to a Forge Addon artifact
*
*
* @param dependency
* @return
*/
Expand All @@ -201,9 +205,9 @@ public static boolean isForgeAddon(Dependency dependency)

/**
* Based on {@link DefaultMutableTreeNode#preorderEnumeration()}
*
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*
*/
private static final class PreorderFirstIterator implements Iterator<DependencyNode>
{
Expand Down Expand Up @@ -250,9 +254,9 @@ public void remove()

/**
* Based on {@link DefaultMutableTreeNode#breadthFirstEnumeration()}
*
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*
*/
private static final class BreadthFirstIterator implements Iterator<DependencyNode>
{
Expand Down Expand Up @@ -295,9 +299,9 @@ public void remove()

/**
* Based on {@link DefaultMutableTreeNode#postorderEnumeration()}
*
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*
*/
private static final class PostorderIterator implements Iterator<DependencyNode>
{
Expand Down

0 comments on commit 1711d7d

Please sign in to comment.