Line | Hits | Source |
---|---|---|
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.Graph; | |
19 | import edu.uci.ics.jung.graph.Vertex; | |
20 | ||
21 | /** | |
22 | * Abstract class that implements a generic filter for accepting arbitrary | |
23 | * vertices (and all edges). 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 | * See sample code at <tt>{@link GeneralEdgeAcceptFilter GeneralEdgeAcceptFilter}</tt> | |
30 | * @author danyelf | |
31 | */ | |
32 | 12 | public abstract class GeneralVertexAcceptFilter implements Filter { |
33 | ||
34 | public abstract boolean acceptVertex(Vertex vert); | |
35 | ||
36 | /** | |
37 | * This method does the actual filtering of the the graph. | |
38 | * It walks through the set of accepted vertices, and | |
39 | * examines each in turn to see whether it is accepted by | |
40 | * acceptVertex. If so, it adds it to the set; if not, it | |
41 | * discards it. This set of filtered vertices is then sent | |
42 | * to the FilteredGraph class. | |
43 | */ | |
44 | public UnassembledGraph filter (Graph g) { | |
45 | 30 | Set vertices = chooseGoodVertices( g.getVertices() ); |
46 | 30 | Set edges = g.getEdges(); |
47 | ||
48 | 30 | return new UnassembledGraph( this, vertices, edges, g ); |
49 | } | |
50 | ||
51 | /** | |
52 | * Returns an <tt>UnassembledGraph</tt> with the subset | |
53 | * of vertices that pass <tt>acceptEdge</tt>. This method | |
54 | * is used only if this class implements <tt>EfficientFilter</tt>, | |
55 | * and, in fact, it contains a runtime check to ensure that the | |
56 | * subclass has been labelled correctly. | |
57 | * @param g An <tt>UnassembledGraph</tt> containing a subset of | |
58 | * vertices and edges from an original graph. | |
59 | * @return An UnassembledGraph containing the subset of <tt>ug</tt> | |
60 | * that pass the filter. | |
61 | * | |
62 | * @see EfficientFilter#filter(UnassembledGraph) | |
63 | */ | |
64 | public UnassembledGraph filter (UnassembledGraph g) { | |
65 | 3 | if (! (this instanceof EfficientFilter)) |
66 | 1 | throw new FatalException("Do not call non-efficient filters with UnassembledGraphs."); |
67 | 2 | Set vertices = chooseGoodVertices( g.getUntouchedVertices() ); |
68 | 2 | Set edges = g.getUntouchedEdges(); |
69 | ||
70 | 2 | return new UnassembledGraph( this, vertices, edges, g ); |
71 | } | |
72 | ||
73 | ||
74 | private Set chooseGoodVertices( Set vertices ) { | |
75 | 32 | Set newVertices = new HashSet(); |
76 | 32 | for (Iterator iter = vertices.iterator(); iter.hasNext();) { |
77 | 343 | Vertex e = (Vertex) iter.next(); |
78 | 343 | if (acceptVertex(e)) { |
79 | 178 | newVertices.add(e); |
80 | } | |
81 | } | |
82 | 32 | return newVertices; |
83 | } | |
84 | ||
85 | ||
86 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |