2 * Created on May 19, 2008
4 * Copyright (c) 2008, the JUNG Project and the Regents of the University
8 * This software is open-source under the BSD license; see either
10 * http://jung.sourceforge.net/license.txt for a description.
12 package edu.uci.ics.jung.algorithms.filters;
14 import java.util.Collection;
16 import org.apache.commons.collections15.Predicate;
18 import edu.uci.ics.jung.graph.Graph;
21 * Transforms the input graph into one which contains only those vertices
22 * that pass the specified <code>Predicate</code>. The filtered graph
23 * is a copy of the original graph (same type, uses the same vertex and
24 * edge objects). Only those edges whose entire incident vertex collection
25 * passes the predicate are copied into the new graph.
27 * @author Joshua O'Madadhain
29 public class VertexPredicateFilter<V,E> implements Filter<V,E>
31 protected Predicate<V> vertex_pred;
34 * Creates an instance based on the specified vertex <code>Predicate</code>.
35 * @param vertex_pred the predicate that specifies which vertices to add to the filtered graph
37 public VertexPredicateFilter(Predicate<V> vertex_pred)
39 this.vertex_pred = vertex_pred;
42 @SuppressWarnings("unchecked")
43 public Graph<V,E> transform(Graph<V,E> g)
48 filtered = g.getClass().newInstance();
50 catch (InstantiationException e)
52 throw new RuntimeException("Unable to create copy of existing graph: ", e);
54 catch (IllegalAccessException e)
56 throw new RuntimeException("Unable to create copy of existing graph: ", e);
59 for (V v : g.getVertices())
60 if (vertex_pred.evaluate(v))
61 filtered.addVertex(v);
63 Collection<V> filtered_vertices = filtered.getVertices();
65 for (E e : g.getEdges())
67 Collection<V> incident = g.getIncidentVertices(e);
68 if (filtered_vertices.containsAll(incident))
69 filtered.addEdge(e, incident);