-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFilterCascade.java
More file actions
57 lines (52 loc) · 1.42 KB
/
Copy pathFilterCascade.java
File metadata and controls
57 lines (52 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.Iterator;
import java.util.List;
/**
* FilterCascade extends Filter. It's constructor
* takes a list of filters, and then when processing the input
* it puts the input through each filter in the list.
* @author gmh73
*
* @param <T>
*/
public class FilterCascade<T> extends Filter<T>{
//The list of filters
private List<Filter<T>> cascade;
/**
* A constructor that takes a list of filters for cascading
* @param filters a list of filters
*/
public FilterCascade(List<Filter<T>> filters){
if(filters.size() <= 0)
throw new IllegalArgumentException("There must be at least one filter in the cascade.");
cascade = filters;
}
/**
* Runs the input through the FilterCascade by getting the output
* from inputting the input into the first filter, then taking that
* output and inputting it as the input to the second filter, and so on.
*/
@Override
protected void processInput(T input) {
//Go through each filter then set the output to
//the final result
Iterator<Filter<T>> it = cascade.iterator();
T prev = input;
while(it.hasNext()){
prev = it.next().filter(prev);
}
setOutput(prev);
}
/**
* Resets all filters in the cascade to r.
* Each filter may have a very different reset function,
* so be aware when using.
*/
@Override
public void reset(T r) {
Iterator<Filter<T>> it = cascade.iterator();
while(it.hasNext()){
it.next().reset(r);
}
setOutput(r);
}
}