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.io.*;
29  import java.util.*;
30  
31  
32  /***
33   * A simple persistent data store using the Java Properties framework.
34   * This gives crude support for collection storage by key prefix
35   * identification.
36   * 
37   * @author  Murali Krishnan
38   *
39   */
40  public class PersistentData extends Properties {
41  
42      //------------------------------------------------------------
43      //- Class Variables
44  
45  
46      /***
47       * The singleton instance.
48       */
49      private static PersistentData sInstance = null;
50  
51  
52      /***
53       * The file where this object is persisted.
54       */
55      protected static final File DATA_FILE = new File(AppProperties.APP_DIR + File.separator + "pdata.properties");
56  
57  
58      //------------------------------------------------------------
59      //- Class Functions
60  
61      /***
62       * @return the singleton, created lazily.
63       */
64      public static synchronized PersistentData getInstance() {
65          if (sInstance == null) {
66              sInstance = new PersistentData();
67          }
68  
69          return sInstance;
70      }
71  
72  
73      //------------------------------------------------------------
74      //- Instance Variables
75  
76  
77  
78      //------------------------------------------------------------
79      //- Constructors
80  
81      /***
82       * The default constructor loads the file.
83       */
84      protected PersistentData() {
85          load();
86      }
87  
88  
89      //------------------------------------------------------------
90      //- Accessors
91  
92  
93  
94      //------------------------------------------------------------
95      //- Settors
96  
97  
98  
99      //------------------------------------------------------------
100     //- Private/Protected Utility Functions
101 
102     /***
103      * Read the data file to populate this object.
104      * It should only be called by the (default) constructor.
105      */
106     protected synchronized void load() {
107         if (DATA_FILE.exists()) {
108             try {
109                 FileInputStream fis = new FileInputStream(DATA_FILE);
110                 load(fis);
111                 fis.close();
112             } catch (IOException ioe) {
113                 System.err.println("Could not read persistent data.");
114                 ioe.printStackTrace();
115             }
116         } else {
117             System.out.println("Creating default persistent data file.");
118             store();
119         }
120     }
121 
122 
123     //------------------------------------------------------------
124     //- Public Interface Functions
125 
126     /***
127      * Lookup the value for the given key and convert it to a boolean.
128      * If it is not found, return false.
129      * 
130      * @return  The property value converted to a boolean value.
131      * @param  key  The association key.
132      */
133     public boolean getBooleanProperty(String key) {
134         String value = getProperty(key);
135 
136         boolean result = Boolean.TRUE.toString().equals(value);
137 
138         return result;
139     }
140 
141     /***
142      * Save this objects values to the file.
143      */
144     public synchronized void store() {
145         AppProperties.ensureAppDir();
146 
147         try {
148             FileOutputStream fos = new FileOutputStream(DATA_FILE);
149             store(fos, "fsgrep persistent data");
150             fos.close();
151         } catch (IOException ioe) {
152             System.err.println("Could not save persistent data.");
153             ioe.printStackTrace();
154         }
155     }
156 
157 
158     /***
159      * Return the sorted set of all keys that have the give prefix.
160      *
161      * @param prefix  The string begining of the keys to be extracted.
162      * @return all the property keys with the given prefix.
163      */
164     public synchronized Collection<String> getKeysPrefixed(String prefix) {
165         // Note: Numeric sorting is only valid if the number of digits is
166         // consistent.
167         Collection<String> result = new TreeSet<String>();
168 
169         for (Object o : keySet()) {
170             String key = (String) o;
171             if (key.startsWith(prefix)) {
172                 result.add(key);
173             }
174         }
175         return result;
176     }
177 
178 
179     /***
180      * Remove all keys that have the give prefix.
181      * 
182      * @param prefix  The string begining of the keys to be removed.
183      */
184     public synchronized void removeKeysPrefixed(String prefix) {
185         // Note: Numeric sorting is only valid if the number of digits is
186         // consistent.
187         Collection<String> keys = getKeysPrefixed(prefix);
188 
189         for (String key : keys) {
190             remove(key);
191         }
192     }
193 
194 
195     /***
196      * Save the key/value mapping where the key is a prefix/index.
197      * 
198      * @param  prefix  The key prefix.
199      * @param  index   The index (numeric) part of the key.
200      * @param  value   The value associated with the key.
201      */
202     public void put(String prefix, int index, String value) {
203         StringBuffer indexPart = new StringBuffer(String.valueOf(index));
204         while (indexPart.length() < 3) {
205             indexPart.insert(0, "0");
206         }
207 
208         String key = prefix + indexPart;
209 
210         setProperty(key, value);
211     }
212 
213     //------------------------------------------------------------
214     //- Class Interface Functions
215 
216 
217 
218     //------------------------------------------------------------
219     //- Inner Classes
220 
221 
222 
223     //------------------------------------------------------------
224     //- Main
225 
226 
227 
228 }