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

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.*;
13  
14 import edu.uci.ics.jung.graph.Graph;
15  
16 /**
17  * This is a generic filter that takes at least two other filters
18  * and runs them seially. That is, it filters first on F1,
19  * and then on F2. Note that it stores links to the filters;
20  * mutable filters therefore are at risk of causing odd side effects:
21  * <code>
22  * <pre>
23  * Filter f1 = new HeightFilter( "tall" );
24  * Filter f2 = new ColorFilter("Green");
25  * Filter serial = new SerialFilter( f1, f2 );
26  * Graph tallGreen = serial.filter( graph ).assemble();
27  * // this contains all tall, green things
28  *
29  * f1.setHeight("short")
30  * // careful, f1 is stored in serial!
31  * Graph otherGreen = serial.filter( graph ).assemble();
32  * // this now contains all short green things.
33  * </pre>
34  * </code>
35  *
36  * @author danyelf
37  */
38 public class SerialFilter implements Filter, EfficientFilter {
39  
40     private List filters;
41  
42     /**
43      * Returns the name of the serial filter.
44      */
45     public String getName() {
460        String rv = "Serial[";
470        boolean first = true;
480        for (Iterator iter = filters.iterator(); iter.hasNext();) {
490            Filter f = (Filter) iter.next();
500            rv += (first ? "" : ",") + f.getName();
510            first = false;
52         }
530        rv += "]";
540        return rv;
55     }
56  
57     /**
58      * Small constructor for two filters. Simply adds them in sequence.
59      * The first filter will be run on the graph before the second one.
60      * @param f1 The first filter.
61      * @param f2 The second filter.
62      */
630    public SerialFilter(Filter f1, Filter f2) {
640        filters = new ArrayList();
650        filters.add(f1);
660        filters.add(f2);
670    }
68  
69  
70     /**
71      * Constructor for an arbitrary list of filters. When <tt>filter()</tt> is called,
72      * will run through them from first to last.
73      */
740    public SerialFilter(List filters) {
750        if (filters == null) {
760            throw new IllegalArgumentException("List can not be null.");
77         }
780        this.filters = new LinkedList(filters);
790    }
80     
81     /**
82      * Creates an empty list of filters. By default, if <tt>filter</tt>
83      * is called, will return the original graph.
84      *
85      * @see TrivialFilter
86      */
871    public SerialFilter() {
881        this.filters = new LinkedList();
891    }
90  
91     /**
92      * Adds a filter to the end of the sequence of filters. Thus, appended
93      * filters will be processed last in the sequence.
94      * @param f Adds the filter to the end of the list.
95      */
96     public void append(Filter f) {
973        filters.add(f);
983    }
99  
100     /**
101      * Runs through the sequence of filters, one at a time. Starts with the
102      * first filter in sequence and works forward. When the filter is able to
103      * be efficient, it does so.
104      */
105     public UnassembledGraph filter(Graph g) {
1061        UnassembledGraph ug = TrivialFilter.getInstance().filter(g);
1071        return filter(ug);
108     }
109  
110     /**
111      * Runs through the sequence of filters, one at a time. Starts with the
112      * first filter in sequence and works forward. When the filter is able to
113      * be efficient, it does so.
114      */
115     public UnassembledGraph filter(UnassembledGraph g) {
1161        UnassembledGraph ug = g;
1171        for (Iterator iter = filters.iterator(); iter.hasNext();) {
1183            Filter f = (Filter) iter.next();
119  
120             // we have a current UG. Can we process it efficiently?
1213            if (f instanceof EfficientFilter) {
122                 // yes: process it directly
1232                EfficientFilter nfe = (EfficientFilter) f;
1242                ug = nfe.filter(ug);
125             } else {
126                 // no: assemble it, then process
1271                Graph prev = ug.assemble();
1281                ug = f.filter(prev);
129             }
130         }
1311        return ug;
132     }
133 }

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.