extended graph wrapper API with content easily taken from tree API
git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@1351166 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/DependencyNode.java b/src/main/java/org/apache/maven/shared/dependency/graph/DependencyNode.java
index 42fa532..76b2177 100644
--- a/src/main/java/org/apache/maven/shared/dependency/graph/DependencyNode.java
+++ b/src/main/java/org/apache/maven/shared/dependency/graph/DependencyNode.java
@@ -22,6 +22,7 @@
import java.util.List;
import org.apache.maven.artifact.Artifact;
+import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
/**
* Represents an artifact node within a Maven project's dependency graph.
@@ -34,4 +35,14 @@
public Artifact getArtifact();
public List<DependencyNode> getChildren();
+
+ /**
+ * Applies the specified dependency node visitor to this dependency node and its children.
+ *
+ * @param visitor
+ * the dependency node visitor to use
+ * @return the visitor result of ending the visit to this node
+ * @since 1.1
+ */
+ public boolean accept( DependencyNodeVisitor visitor );
}
diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/filter/AndDependencyNodeFilter.java b/src/main/java/org/apache/maven/shared/dependency/graph/filter/AndDependencyNodeFilter.java
new file mode 100644
index 0000000..d50cef1
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/dependency/graph/filter/AndDependencyNodeFilter.java
@@ -0,0 +1,100 @@
+package org.apache.maven.shared.dependency.graph.filter;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+
+/**
+ * A dependency node filter that logically ANDs together a number of other dependency node filters.
+ *
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ * @version $Id$
+ * @since 1.1
+ */
+public class AndDependencyNodeFilter
+ implements DependencyNodeFilter
+{
+ // fields -----------------------------------------------------------------
+
+ /**
+ * The dependency node filters that this filter ANDs together.
+ */
+ private final List<DependencyNodeFilter> filters;
+
+ // constructors -----------------------------------------------------------
+
+ /**
+ * Creates a dependency node filter that logically ANDs together the two specified dependency node filters.
+ *
+ * @param filter1
+ * the first dependency node filter to logically AND together
+ * @param filter2
+ * the second dependency node filter to logically AND together
+ */
+ public AndDependencyNodeFilter( DependencyNodeFilter filter1, DependencyNodeFilter filter2 )
+ {
+ this( Arrays.asList( new DependencyNodeFilter[] { filter1, filter2 } ) );
+ }
+
+ /**
+ * Creates a dependency node filter that logically ANDs together the specified dependency node filters.
+ *
+ * @param filters
+ * the list of dependency node filters to logically AND together
+ */
+ public AndDependencyNodeFilter( List<DependencyNodeFilter> filters )
+ {
+ this.filters = Collections.unmodifiableList( filters );
+ }
+
+ // DependencyNodeFilter methods -------------------------------------------
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean accept( DependencyNode node )
+ {
+ for ( DependencyNodeFilter filter : filters )
+ {
+ if ( !filter.accept( node ) )
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ // public methods ---------------------------------------------------------
+
+ /**
+ * Gets the list of dependency node filters that this filter ANDs together.
+ *
+ * @return the dependency node filters that this filter ANDs together
+ */
+ public List<DependencyNodeFilter> getDependencyNodeFilters()
+ {
+ return filters;
+ }
+}
diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/filter/ArtifactDependencyNodeFilter.java b/src/main/java/org/apache/maven/shared/dependency/graph/filter/ArtifactDependencyNodeFilter.java
new file mode 100644
index 0000000..fbe18c7
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/dependency/graph/filter/ArtifactDependencyNodeFilter.java
@@ -0,0 +1,79 @@
+package org.apache.maven.shared.dependency.graph.filter;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+
+/**
+ * A dependency node filter that delegates to an artifact filter.
+ *
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ * @version $Id$
+ * @since 1.1
+ */
+public class ArtifactDependencyNodeFilter
+ implements DependencyNodeFilter
+{
+ // fields -----------------------------------------------------------------
+
+ /**
+ * The artifact filter this dependency node filter delegates to.
+ */
+ private final ArtifactFilter filter;
+
+ // constructors -----------------------------------------------------------
+
+ /**
+ * Creates a dependency node filter that delegates to the specified artifact filter.
+ *
+ * @param filter
+ * the artifact filter to delegate to
+ */
+ public ArtifactDependencyNodeFilter( ArtifactFilter filter )
+ {
+ this.filter = filter;
+ }
+
+ // DependencyNodeFilter methods -------------------------------------------
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean accept( DependencyNode node )
+ {
+ Artifact artifact = node.getArtifact();
+
+ return filter.include( artifact );
+ }
+
+ // public methods ---------------------------------------------------------
+
+ /**
+ * Gets the artifact filter this dependency node filter delegates to.
+ *
+ * @return the artifact filter this dependency node filter delegates to
+ */
+ public ArtifactFilter getArtifactFilter()
+ {
+ return filter;
+ }
+}
diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/filter/DependencyNodeFilter.java b/src/main/java/org/apache/maven/shared/dependency/graph/filter/DependencyNodeFilter.java
new file mode 100644
index 0000000..7b0f481
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/dependency/graph/filter/DependencyNodeFilter.java
@@ -0,0 +1,41 @@
+package org.apache.maven.shared.dependency.graph.filter;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+
+/**
+ * Defines a filter for dependency nodes.
+ *
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ * @version $Id$
+ * @since 1.1
+ */
+public interface DependencyNodeFilter
+{
+ /**
+ * Gets whether this filter accepts the specified dependency node.
+ *
+ * @param node
+ * the dependency node to check
+ * @return <code>true</code> if this filter accepts the specified dependency node
+ */
+ boolean accept( DependencyNode node );
+}
diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/internal/AbstractDependencyNode.java b/src/main/java/org/apache/maven/shared/dependency/graph/internal/AbstractDependencyNode.java
new file mode 100644
index 0000000..cbb550e
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/dependency/graph/internal/AbstractDependencyNode.java
@@ -0,0 +1,32 @@
+package org.apache.maven.shared.dependency.graph.internal;
+
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
+
+public abstract class AbstractDependencyNode
+ implements DependencyNode
+{
+ /**
+ * Applies the specified dependency node visitor to this dependency node and its children.
+ *
+ * @param visitor
+ * the dependency node visitor to use
+ * @return the visitor result of ending the visit to this node
+ * @since 1.1
+ */
+ public boolean accept( DependencyNodeVisitor visitor )
+ {
+ if ( visitor.visit( this ) )
+ {
+ for ( DependencyNode child : getChildren() )
+ {
+ if ( !child.accept( visitor ) )
+ {
+ break;
+ }
+ }
+ }
+
+ return visitor.endVisit( this );
+ }
+}
diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/internal/Maven2DependencyNode.java b/src/main/java/org/apache/maven/shared/dependency/graph/internal/Maven2DependencyNode.java
index f586fe5..ec51df5 100644
--- a/src/main/java/org/apache/maven/shared/dependency/graph/internal/Maven2DependencyNode.java
+++ b/src/main/java/org/apache/maven/shared/dependency/graph/internal/Maven2DependencyNode.java
@@ -35,7 +35,7 @@
* @since 2.0
*/
public class Maven2DependencyNode
- implements DependencyNode
+ extends AbstractDependencyNode
{
private final Artifact artifact;
diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/internal/Maven3DependencyNode.java b/src/main/java/org/apache/maven/shared/dependency/graph/internal/Maven3DependencyNode.java
index 61a7e2a..a27220a 100644
--- a/src/main/java/org/apache/maven/shared/dependency/graph/internal/Maven3DependencyNode.java
+++ b/src/main/java/org/apache/maven/shared/dependency/graph/internal/Maven3DependencyNode.java
@@ -38,7 +38,7 @@
* @since 2.0
*/
public class Maven3DependencyNode
- implements DependencyNode
+ extends AbstractDependencyNode
{
private final Artifact artifact;
diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/traversal/CollectingDependencyNodeVisitor.java b/src/main/java/org/apache/maven/shared/dependency/graph/traversal/CollectingDependencyNodeVisitor.java
new file mode 100644
index 0000000..0d4b515
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/dependency/graph/traversal/CollectingDependencyNodeVisitor.java
@@ -0,0 +1,87 @@
+package org.apache.maven.shared.dependency.graph.traversal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+
+/**
+ * A dependency node visitor that collects visited nodes for further processing.
+ *
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ * @version $Id$
+ * @since 1.1
+ */
+public class CollectingDependencyNodeVisitor
+ implements DependencyNodeVisitor
+{
+ // fields -----------------------------------------------------------------
+
+ /**
+ * The collected list of nodes.
+ */
+ private final List<DependencyNode> nodes;
+
+ // constructors -----------------------------------------------------------
+
+ /**
+ * Creates a dependency node visitor that collects visited nodes for further processing.
+ */
+ public CollectingDependencyNodeVisitor()
+ {
+ nodes = new ArrayList<DependencyNode>();
+ }
+
+ // DependencyNodeVisitor methods ------------------------------------------
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean visit( DependencyNode node )
+ {
+ // collect node
+ nodes.add( node );
+
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean endVisit( DependencyNode node )
+ {
+ return true;
+ }
+
+ // public methods ---------------------------------------------------------
+
+ /**
+ * Gets the list of collected dependency nodes.
+ *
+ * @return the list of collected dependency nodes
+ */
+ public List<DependencyNode> getNodes()
+ {
+ return Collections.unmodifiableList( nodes );
+ }
+}
diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/traversal/DependencyNodeVisitor.java b/src/main/java/org/apache/maven/shared/dependency/graph/traversal/DependencyNodeVisitor.java
new file mode 100644
index 0000000..7e271d7
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/dependency/graph/traversal/DependencyNodeVisitor.java
@@ -0,0 +1,52 @@
+package org.apache.maven.shared.dependency.graph.traversal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+
+/**
+ * Defines a hierarchical visitor for processing dependency node trees.
+ *
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ * @version $Id$
+ * @since 1.1
+ */
+public interface DependencyNodeVisitor
+{
+ /**
+ * Starts the visit to the specified dependency node.
+ *
+ * @param node
+ * the dependency node to visit
+ * @return <code>true</code> to visit the specified dependency node's children, <code>false</code> to skip the
+ * specified dependency node's children and proceed to its next sibling
+ */
+ boolean visit( DependencyNode node );
+
+ /**
+ * Ends the visit to to the specified dependency node.
+ *
+ * @param node
+ * the dependency node to visit
+ * @return <code>true</code> to visit the specified dependency node's next sibling, <code>false</code> to skip
+ * the specified dependency node's next siblings and proceed to its parent
+ */
+ boolean endVisit( DependencyNode node );
+}
diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/traversal/FilteringDependencyNodeVisitor.java b/src/main/java/org/apache/maven/shared/dependency/graph/traversal/FilteringDependencyNodeVisitor.java
new file mode 100644
index 0000000..5131af5
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/dependency/graph/traversal/FilteringDependencyNodeVisitor.java
@@ -0,0 +1,125 @@
+package org.apache.maven.shared.dependency.graph.traversal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+import org.apache.maven.shared.dependency.graph.filter.DependencyNodeFilter;
+
+/**
+ * A dependency node visitor that filters nodes and delegates to another visitor.
+ *
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ * @version $Id$
+ * @since 1.1
+ */
+public class FilteringDependencyNodeVisitor
+ implements DependencyNodeVisitor
+{
+ // fields -----------------------------------------------------------------
+
+ /**
+ * The dependency node visitor to delegate to.
+ */
+ private final DependencyNodeVisitor visitor;
+
+ /**
+ * The dependency node filter to apply before delegation.
+ */
+ private final DependencyNodeFilter filter;
+
+ // constructors -----------------------------------------------------------
+
+ /**
+ * Creates a dependency node visitor that delegates nodes that are accepted by the specified filter to the specified
+ * visitor.
+ *
+ * @param visitor
+ * the dependency node visitor to delegate to
+ * @param filter
+ * the dependency node filter to apply before delegation
+ */
+ public FilteringDependencyNodeVisitor( DependencyNodeVisitor visitor, DependencyNodeFilter filter )
+ {
+ this.visitor = visitor;
+ this.filter = filter;
+ }
+
+ // DependencyNodeVisitor methods ------------------------------------------
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean visit( DependencyNode node )
+ {
+ boolean visit;
+
+ if ( filter.accept( node ) )
+ {
+ visit = visitor.visit( node );
+ }
+ else
+ {
+ visit = true;
+ }
+
+ return visit;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean endVisit( DependencyNode node )
+ {
+ boolean visit;
+
+ if ( filter.accept( node ) )
+ {
+ visit = visitor.endVisit( node );
+ }
+ else
+ {
+ visit = true;
+ }
+
+ return visit;
+ }
+
+ // public methods ---------------------------------------------------------
+
+ /**
+ * Gets the dependency node visitor that this visitor delegates to.
+ *
+ * @return the dependency node visitor
+ */
+ public DependencyNodeVisitor getDependencyNodeVisitor()
+ {
+ return visitor;
+ }
+
+ /**
+ * Gets the dependency node filter that this visitor applies before delegation.
+ *
+ * @return the dependency node filter
+ */
+ public DependencyNodeFilter getDependencyNodeFilter()
+ {
+ return filter;
+ }
+}