-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLc.java
84 lines (76 loc) · 2.24 KB
/
Lc.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.concurrent.BlockingQueue;
/**
* A class that represents a lc command object that extends Filter class and
* that contains main functionality of lc command.
* @author michaelroytman
*/
public class Lc extends Filter {
private int count; //line count
private boolean countReturned; //boolean flag representing whether the
//count has been returned
/**
* Constructor that accepts an input and output blocking queue.
* @param in input blocking queue
* @param out output blocking queue
*/
public Lc(BlockingQueue<Object> in, BlockingQueue<Object> out) {
super(in, out);
count = 0;
countReturned = false;
}
/**
* @override
* Override run method for lc command that adds the countReturned condition
* to the functionality of the method.
*/
public void run() {
Object o = null;
while(! this.done) {
//if there is an input queue and if count has not been returned
if (in != null && !countReturned) {
// 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();
}
}
}
}
/**
* @override
* Transform method that keeps track of line count and returns it to the
* output queue at the end of input; once count is returned; an EndOfFileMarker
* is returned to signal end of output.
*/
public Object transform(Object o) {
//if o is a String, line is incremented
if (o instanceof String) {
count++;
return null;
}
// if o is an EndOfFileMarker, signal end of input
else if (o instanceof EndOfFileMarker) {
countReturned = true; //count is returned
return ((Integer)count).toString(); //returns count as an Integer
}
//if count has been returned, returns EndOfFileMarker to output queue
//to signal end of output
else if (countReturned) {
this.done = true;
return new EndOfFileMarker();
}
return null;
}
}