forked from jjfiv/CSC262P1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTail.java
More file actions
50 lines (43 loc) · 1.4 KB
/
Tail.java
File metadata and controls
50 lines (43 loc) · 1.4 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
//method to split arraylist found here https://stackoverflow.com/questions/2895342/java-how-can-i-split-an-arraylist-in-multiple-small-arraylists
package edu.smith.cs.csc262.coopsh.apps;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
//so I'll know how many total lines there are
//so I will need to get the offset from total-desired # to total
import edu.smith.cs.csc262.coopsh.InputLine;
import edu.smith.cs.csc262.coopsh.ShellEnvironment;
import edu.smith.cs.csc262.coopsh.Task;
public class Tail extends Task {
int lineCount;
//arraylist of inputlines to be sorted
ArrayList<String> inputLines = new ArrayList<String>();
public Tail(ShellEnvironment env, String[] args) {
super(env, args);
}
@Override
protected void update() {
int numLines = Integer.parseInt(args[0]);
InputLine line = this.input.poll();
if (line == null) {
// still waiting for more...
return;
}
// only sort and print when we've seen the whole file!
if (line.isEndOfFile()) {
int splitIndex = lineCount-numLines;
//split arraylist so we only keep last n lines
List<String> tail = inputLines.subList(splitIndex+1, lineCount);
//loop through tail to print it
for(String str : tail){
this.println(str);
}
this.closeOutput();
this.exit(0);
return;
}
lineCount++;
// Otherwise, increment add this line to the array of strings!
inputLines.add(line.get());
}
}