Coverage details for edu.uci.ics.jung.graph.filters.GeneralEdgeAcceptFilter

LineHitsSource
1 /*
2 * Copyright (c) 2003, the JUNG Project and the Regents of the University
3 * of California
4 * All rights reserved.
5 *
6 * This software is open-source under the BSD license; see either
7 * "license.txt" or
8 * http://jung.sourceforge.net/license.txt for a description.
9 */
10 package edu.uci.ics.jung.graph.filters;
11  
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.Set;
15  
16  
17 import edu.uci.ics.jung.exceptions.FatalException;
18 import edu.uci.ics.jung.graph.Edge;
19 import edu.uci.ics.jung.graph.Graph;
20  
21 /**
22  * Abstract class that implements a generic filter for accepting arbitrary
23  * edges (and all vertices). To use it, subclass this and override
24  * <tt>acceptEdge</tt>. This is compatible with both <tt>EfficientFilter</tt>;
25  * in order to use it as such, make sure to label your class as an
26  * <tt>EfficientFilter</tt> with
27  * <tt>implements EfficientFilter</tt>.
28  * <p>
29  * <h2>Sample code</h2>
30  * <pre>
31  * // Returns a version of the graph that only has blue edges.
32  * class OnlyBlueEdgeFilter extends GeneralEdgeAcceptFilter
33  * implements EfficientFilter {
34  *
35  * // BlueChecker is a helper class that I've implemented somewhere else
36  * boolean acceptEdge( Edge e ) {
37  * return BlueChecker.checkBlue( e );
38  * }
39  * }
40  * </pre>
41  *
42  * @author danyelf
43  */
4430public abstract class GeneralEdgeAcceptFilter implements Filter {
45  
46     /**
47      * Determines whether the current edge should be accepted
48      * into the Graph. User should override this method.
49      * @param edge the input edge that is being evaluated.
50      * @return whether the edge should be accepted or not
51      */
52     public abstract boolean acceptEdge(Edge edge);
53  
54     /**
55      * Returns an <tt>UnassembledGraph</tt> with the subset
56      * of edges that pass <tt>acceptEdge</tt>.
57      * @param g A <tt>Graph</tt> to be filtered.
58      * @return An UnassembledGraph containing the subset of <tt>g</tt>
59      * that pass the filter.
60      *
61      * @see Filter#filter(Graph)
62      */
63     public UnassembledGraph filter(Graph g) {
6444        Set vertices = g.getVertices();
6544        Set edges = g.getEdges();
66  
6744        Set newEdges = chooseGoodEdges( edges );
6844        return new UnassembledGraph(this, vertices, newEdges, g);
69     }
70  
71     /**
72      * Returns an <tt>UnassembledGraph</tt> with the subset
73      * of edges that pass <tt>acceptEdge</tt>. This method
74      * is used only if this class implements <tt>EfficientFilter</tt>,
75      * and, in fact, it contains a runtime check to ensure that the
76      * subclass has been labelled correctly.
77      * @param ug An <tt>UnassembledGraph</tt> containing a subset of
78      * vertices and edges from an original graph.
79      * @return An UnassembledGraph containing the subset of <tt>ug</tt>
80      * that pass the filter.
81      *
82      * @see EfficientFilter#filter(UnassembledGraph)
83      */
84     public UnassembledGraph filter(UnassembledGraph ug) {
85  
862        if (! (this instanceof EfficientFilter))
870            throw new FatalException("Do not call non-efficient filters with UnassembledGraphs.");
88  
892        Set vertices = null;
902        Set edges = null;
91  
922        vertices = ug.getUntouchedVertices();
932        edges = ug.getUntouchedEdges();
942        if (vertices == null) {
950            vertices = ug.getOriginalGraph().getVertices();
96         }
972        if (edges == null) {
980            edges = ug.getOriginalGraph().getEdges();
99         }
1002        Set newEdges = chooseGoodEdges( edges );
1012        return new UnassembledGraph(this, vertices, newEdges, ug);
102     }
103  
104     private Set chooseGoodEdges( Set edges ) {
10546        Set newEdges = new HashSet();
10646        for (Iterator iter = edges.iterator(); iter.hasNext();) {
107368            Edge e = (Edge) iter.next();
108368            if (acceptEdge(e)) {
109254                newEdges.add(e);
110             }
111         }
11246        return newEdges;
113     }
114  
115 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.