| 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.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 | */ | |
| 44 | 30 | public 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) { | |
| 64 | 44 | Set vertices = g.getVertices(); |
| 65 | 44 | Set edges = g.getEdges(); |
| 66 | ||
| 67 | 44 | Set newEdges = chooseGoodEdges( edges ); |
| 68 | 44 | 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 | ||
| 86 | 2 | if (! (this instanceof EfficientFilter)) |
| 87 | 0 | throw new FatalException("Do not call non-efficient filters with UnassembledGraphs."); |
| 88 | ||
| 89 | 2 | Set vertices = null; |
| 90 | 2 | Set edges = null; |
| 91 | ||
| 92 | 2 | vertices = ug.getUntouchedVertices(); |
| 93 | 2 | edges = ug.getUntouchedEdges(); |
| 94 | 2 | if (vertices == null) { |
| 95 | 0 | vertices = ug.getOriginalGraph().getVertices(); |
| 96 | } | |
| 97 | 2 | if (edges == null) { |
| 98 | 0 | edges = ug.getOriginalGraph().getEdges(); |
| 99 | } | |
| 100 | 2 | Set newEdges = chooseGoodEdges( edges ); |
| 101 | 2 | return new UnassembledGraph(this, vertices, newEdges, ug); |
| 102 | } | |
| 103 | ||
| 104 | private Set chooseGoodEdges( Set edges ) { | |
| 105 | 46 | Set newEdges = new HashSet(); |
| 106 | 46 | for (Iterator iter = edges.iterator(); iter.hasNext();) { |
| 107 | 368 | Edge e = (Edge) iter.next(); |
| 108 | 368 | if (acceptEdge(e)) { |
| 109 | 254 | newEdges.add(e); |
| 110 | } | |
| 111 | } | |
| 112 | 46 | return newEdges; |
| 113 | } | |
| 114 | ||
| 115 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |