View Javadoc

1   /*
2    * Fsgrep is a simple Java application which allows a user to
3    * search all files in a directory structure for lines matching
4    * a given pattern.  Its functionality is a combination of the
5    * Unix 'find' and 'grep' utilities.
6    * Visit [http://fsgrep.sourceforge.net/] for more information.
7    * 
8    * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9    * 
10   * Fsgrep is free software; you can redistribute it and/or modify
11   * it under the terms of version 2 of the GNU General Public
12   * License as published by the Free Software Foundation.
13   * 
14   * Fsgrep is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Fsgrep (see the file named LICENSE.txt); if not, write
21   * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22   * Boston, MA  02111-1307  USA
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      //- Class Variables
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      //- Class Functions
68  
69  
70  
71  
72      //------------------------------------------------------------
73      //- Instance Variables
74  
75      private int _maxSize = MAX_SIZE;
76      private String _persistentTablePrefix = null;
77  
78  
79      //------------------------------------------------------------
80      //- Constructors
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     //- Accessors
101 
102     public int getMaxSize() {return _maxSize;}
103     protected String getPrefix() {return _persistentTablePrefix;}
104 
105 
106     //------------------------------------------------------------
107     //- Settors
108 
109     public void setMaxSize(int val) {
110         _maxSize=val;
111 
112         trim();
113     }
114 
115 
116     //------------------------------------------------------------
117     //- Private/Protected Utility Functions
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     //- Public Interface Functions
133 
134 
135     /***
136      * Saves the state of this object to the persistent store.
137      * 
138      */
139     public void saveData() {
140         pdata.removeKeysPrefixed(getPrefix());  // Clear stale entries.
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);  // Add last to first.
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     //- Class Interface Functions
196 
197     public boolean add(String entry) {
198         //noinspection UnusedDeclaration
199         boolean alreadyExists = remove(entry);
200         addFirst(entry);
201         trim();
202 
203         return true;
204     }
205 
206 
207     //------------------------------------------------------------
208     //- Inner Classes
209 
210 
211 
212     //------------------------------------------------------------
213     //- Main
214 
215 
216 
217 }