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.*; | |
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() { | |
46 | 0 | String rv = "Serial["; |
47 | 0 | boolean first = true; |
48 | 0 | for (Iterator iter = filters.iterator(); iter.hasNext();) { |
49 | 0 | Filter f = (Filter) iter.next(); |
50 | 0 | rv += (first ? "" : ",") + f.getName(); |
51 | 0 | first = false; |
52 | } | |
53 | 0 | rv += "]"; |
54 | 0 | 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 | */ | |
63 | 0 | public SerialFilter(Filter f1, Filter f2) { |
64 | 0 | filters = new ArrayList(); |
65 | 0 | filters.add(f1); |
66 | 0 | filters.add(f2); |
67 | 0 | } |
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 | */ | |
74 | 0 | public SerialFilter(List filters) { |
75 | 0 | if (filters == null) { |
76 | 0 | throw new IllegalArgumentException("List can not be null."); |
77 | } | |
78 | 0 | this.filters = new LinkedList(filters); |
79 | 0 | } |
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 | */ | |
87 | 1 | public SerialFilter() { |
88 | 1 | this.filters = new LinkedList(); |
89 | 1 | } |
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) { | |
97 | 3 | filters.add(f); |
98 | 3 | } |
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) { | |
106 | 1 | UnassembledGraph ug = TrivialFilter.getInstance().filter(g); |
107 | 1 | 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) { | |
116 | 1 | UnassembledGraph ug = g; |
117 | 1 | for (Iterator iter = filters.iterator(); iter.hasNext();) { |
118 | 3 | Filter f = (Filter) iter.next(); |
119 | ||
120 | // we have a current UG. Can we process it efficiently? | |
121 | 3 | if (f instanceof EfficientFilter) { |
122 | // yes: process it directly | |
123 | 2 | EfficientFilter nfe = (EfficientFilter) f; |
124 | 2 | ug = nfe.filter(ug); |
125 | } else { | |
126 | // no: assemble it, then process | |
127 | 1 | Graph prev = ug.assemble(); |
128 | 1 | ug = f.filter(prev); |
129 | } | |
130 | } | |
131 | 1 | return ug; |
132 | } | |
133 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |