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 package mk.fsgrep.util;
26
27
28 import java.util.*;
29
30
31
32 /***
33 * Most recently used list. A list ordered by recent use where the
34 * first element is the most recently used entry. It also trims itself
35 * to a maximum size.
36 *
37 * @author Murali Krishnan
38 *
39 */
40 public class MRUList extends LinkedList<String> {
41
42
43
44
45 /***
46 * The maximum number of recent items kept when saved.
47 */
48 public static final int MAX_SIZE = 10;
49
50
51 /***
52 * Constant representing an undefined persistent map prefix (the default).
53 *
54 */
55 protected static final String UNDEFINED_PREFIX = "undefined";
56
57
58 /***
59 * A reference to the persistent data object (singleton).
60 */
61 private static final PersistentData pdata = PersistentData.getInstance();
62
63
64
65
66
67
68
69
70
71
72
73
74
75 private int _maxSize = MAX_SIZE;
76 private String _persistentTablePrefix = null;
77
78
79
80
81
82 public MRUList() {
83 this (MAX_SIZE, UNDEFINED_PREFIX);
84 }
85
86
87 public MRUList(String pPrefix) {
88 this (MAX_SIZE, pPrefix);
89 }
90
91
92 public MRUList(int pSize, String pPrefix) {
93 _maxSize = pSize;
94
95 readData(pPrefix);
96 }
97
98
99
100
101
102 public int getMaxSize() {return _maxSize;}
103 protected String getPrefix() {return _persistentTablePrefix;}
104
105
106
107
108
109 public void setMaxSize(int val) {
110 _maxSize=val;
111
112 trim();
113 }
114
115
116
117
118
119 /***
120 * Trim the number of entries to the maximum size by removing
121 * the older elements.
122 */
123 protected void trim() {
124 while (size() > getMaxSize()) {
125 removeLast();
126 }
127 }
128
129
130
131
132
133
134
135 /***
136 * Saves the state of this object to the persistent store.
137 *
138 */
139 public void saveData() {
140 pdata.removeKeysPrefixed(getPrefix());
141
142 int i = 0;
143 Iterator myIterator = iterator();
144 while (myIterator.hasNext()) {
145 String spec = (String) myIterator.next();
146 pdata.put(getPrefix(), i++, spec);
147 }
148 pdata.store();
149 }
150
151
152 /***
153 * Read the stored profiles from the persistent data using the given
154 * persistent map prefix.
155 * @param pPrefix The string prefix of the desired entries.
156 */
157 public void readData(String pPrefix) {
158 _persistentTablePrefix = pPrefix;
159
160 readData();
161 }
162
163
164 /***
165 * Read the stored profiles from the persistent data. This will only
166 * work if a persistent map prefix was defined.
167 */
168 public void readData() {
169 if (!UNDEFINED_PREFIX.equals(getPrefix())) {
170 List<String> list = new ArrayList<String>(pdata.getKeysPrefixed(getPrefix()));
171 Collections.reverse(list);
172
173 for (String key : list) {
174 String val = pdata.getProperty(key);
175 add(val);
176 }
177 }
178 }
179
180
181 /***
182 * Remove all items except the first one.
183 *
184 */
185 public void keepFirst() {
186 if (!isEmpty()) {
187 String first = getFirst();
188 clear();
189 add(first);
190 }
191 }
192
193
194
195
196
197 public boolean add(String entry) {
198
199 boolean alreadyExists = remove(entry);
200 addFirst(entry);
201 trim();
202
203 return true;
204 }
205
206
207
208
209
210
211
212
213
214
215
216
217 }