-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFilter.java
executable file
·69 lines (59 loc) · 1.45 KB
/
Filter.java
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
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.concurrent.*;
/*
* This is the Filter class that all command implementations extend.
*/
//this is an abstract command
public abstract class Filter extends Thread {
protected BlockingQueue<Object> in; //input queue
protected BlockingQueue<Object> out; //output queue
protected volatile boolean done;
/*
* The following flag is for Part 4.
*/
protected volatile boolean killed;
public Filter (BlockingQueue<Object> in, BlockingQueue<Object> out) {
this.in = in;
this.out = out;
this.done = false;
this.killed = false;
}
/*
* This is for Part 4.
*/
public void cmdKill() {
this.killed = true;
}
/*
* This method need to be overridden.
* @see java.lang.Thread#run()
*/
public void run() {
Object o = null;
while(! this.done) {
//if there is an input queue
if (in != null) {
// read from input queue, may block
try {
o = in.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// allow filter to change message
o = transform(o);
//if there is an output queue and o is not null
if (out != null && o != null) {
// forward to output queue
try {
out.put(o);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/*
* This method might need to be overridden.
*/
public abstract Object transform(Object o);
}