Line | |
---|
1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package CommandLine;
|
---|
3 |
|
---|
4 | import java.util.LinkedList;
|
---|
5 |
|
---|
6 | public class History {
|
---|
7 | private final LinkedList<String> historyList;
|
---|
8 | private final int maxLen;
|
---|
9 | private int num;
|
---|
10 |
|
---|
11 | public History(int len) {
|
---|
12 | num = 0;
|
---|
13 | maxLen = len;
|
---|
14 | historyList = new LinkedList<>();
|
---|
15 | }
|
---|
16 |
|
---|
17 | public void addItem(String item) {
|
---|
18 | if (!item.equals("")) {
|
---|
19 | String prevItem = historyList.peekFirst();
|
---|
20 | if (prevItem == null) {
|
---|
21 | historyList.addFirst(item);
|
---|
22 | } else {
|
---|
23 | if (!prevItem.equalsIgnoreCase(item))
|
---|
24 | historyList.addFirst(item);
|
---|
25 | }
|
---|
26 | if (historyList.size() > maxLen) {
|
---|
27 | historyList.removeLast();
|
---|
28 | }
|
---|
29 | }
|
---|
30 | num = -1;
|
---|
31 | }
|
---|
32 |
|
---|
33 | public String getPrevItem() {
|
---|
34 | num += 1;
|
---|
35 | if (num >= historyList.size()) {
|
---|
36 | num = historyList.size() - 1;
|
---|
37 | }
|
---|
38 | if (num < 0) {
|
---|
39 | num = -1;
|
---|
40 | return "";
|
---|
41 | }
|
---|
42 | return historyList.get(num);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public String getLastItem() {
|
---|
46 | if (historyList.size() > 0)
|
---|
47 | return historyList.get(0);
|
---|
48 | return "";
|
---|
49 | }
|
---|
50 |
|
---|
51 | public String getNextItem() {
|
---|
52 | num -= 1;
|
---|
53 | if (num < 0) {
|
---|
54 | num = -1;
|
---|
55 | return "";
|
---|
56 | }
|
---|
57 | return historyList.get(num);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.