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 org.apache.commons.collections15.Predicate;
16 import edu.uci.ics.jung.graph.Graph;
19 * Transforms the input graph into one which contains only those edges
20 * that pass the specified <code>Predicate</code>. The filtered graph
21 * is a copy of the original graph (same type, uses the same vertex and
22 * edge objects). All vertices from the original graph
23 * are copied into the new graph (even if they are not incident to any
24 * edges in the new graph).
26 * @author Joshua O'Madadhain
28 public class EdgePredicateFilter<V, E> implements Filter<V, E>
30 protected Predicate<E> edge_pred;
33 * Creates an instance based on the specified edge <code>Predicate</code>.
34 * @param edge_pred the predicate that specifies which edges to add to the filtered graph
36 public EdgePredicateFilter(Predicate<E> edge_pred)
38 this.edge_pred = edge_pred;
41 @SuppressWarnings("unchecked")
42 public Graph<V,E> transform(Graph<V,E> g)
47 filtered = g.getClass().newInstance();
49 catch (InstantiationException e)
51 throw new RuntimeException("Unable to create copy of existing graph: ", e);
53 catch (IllegalAccessException e)
55 throw new RuntimeException("Unable to create copy of existing graph: ", e);
58 for (V v : g.getVertices())
59 filtered.addVertex(v);
61 for (E e : g.getEdges())
63 if (edge_pred.evaluate(e))
64 filtered.addEdge(e, g.getIncidentVertices(e));