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