1 /*
2 * $HeadURL: $
3 * $Date: $
4 * $Revision: $
5 * $Author: $
6 *
7 * Copyright (c) 2005 MindTree Consulting Ltd.
8 *
9 * This file is part of Insight.
10 *
11 * Insight is free software: you can redistribute it and/or modify it under the
12 * terms of the GNU General Public License as published by the Free Software
13 * Foundation, either version 3 of the License, or (at your option) any later
14 * version.
15 *
16 * Insight is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * Insight. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 package com.mindtree.techworks.insight.preferences.xmlpersistence;
26
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.Iterator;
30
31 import com.mindtree.techworks.insight.preferences.model.Preference;
32
33
34 /**
35 * This class will be used to cache the XML Preference Related Data. This is a
36 * singleton class.
37 *
38 * @see com.mindtree.techworks.insight.preferences.xmlpersistence.XMLPreferenceDataHandler
39 * XMLPreferenceDataHandler
40 * @author Bindul Bhowmik
41 * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $
42 */
43 public class XMLPreferenceDataCache {
44
45 /**
46 * The singleton instance
47 */
48 private static XMLPreferenceDataCache instance = null;
49
50 /**
51 * Preferences stored in the hashmap.
52 */
53 private HashMap preferences = null;
54
55 /**
56 * Indicates if the XML file has already been validated aginst the Schema
57 */
58 private boolean isXMLValidated = false;
59
60 /**
61 * Indicates if the XML file is already loaded
62 */
63 private boolean isXMLLoaded = false;
64
65 /**
66 * Singleton, so private Constructor
67 */
68 private XMLPreferenceDataCache () {
69
70 preferences = new HashMap();
71 }
72
73 /**
74 * Returns the singleton instance.
75 *
76 * @return the singleton instance.
77 */
78 public static XMLPreferenceDataCache getInstance () {
79
80 if (null == instance) {
81 //Instantiate the instance - double check lock!
82 synchronized (XMLPreferenceDataCache.class) {
83 if (null == instance) {
84 instance = new XMLPreferenceDataCache();
85 }
86 }
87 }
88 return instance;
89 }
90
91 /**
92 * Adds a preference to the cache. If the preference with the same
93 * {@link Preference#getId() key} is already present in the cache, it will
94 * be overwritten.
95 *
96 * @param preference The preference to add to the cache.
97 */
98 public void addPreference (Preference preference) {
99
100 if (null != preference) {
101 preferences.put(preference.getId(), preference);
102 }
103 }
104
105 /**
106 * Returns a preference from the cache, if present or retuns null.
107 *
108 * @param preferenceId The id for which preference is required.
109 * @return The preference if present or null.
110 */
111 public Preference getPreference (String preferenceId) {
112
113 if (preferences.containsKey(preferenceId)) {
114 return (Preference) preferences.get(preferenceId);
115 }
116 return null;
117 }
118
119 /**
120 * Returns all the preferences from the cache.
121 * @return A collection of {@link Preference Preference} objects.
122 */
123 public Collection getAllPreferences () {
124
125 return preferences.values();
126 }
127
128 /**
129 * Returns an iterator to all cached preferences.
130 * @return An iterator to all the cache preferences.
131 */
132 public Iterator iteratePreferences () {
133
134 return preferences.values().iterator();
135 }
136
137
138 /**
139 * If the XML is loaded.
140 * @return Returns the isXMLLoaded.
141 */
142 public boolean isXMLLoaded () {
143
144 return isXMLLoaded;
145 }
146
147 /**
148 * Sets the value to the xml loaded status.
149 * @param isXMLLoaded The isXMLLoaded to set.
150 */
151 public synchronized void setXMLLoaded (boolean isXMLLoaded) {
152
153 this.isXMLLoaded = isXMLLoaded;
154 }
155
156 /**
157 * @return Returns the isXMLValidated.
158 */
159 public boolean isXMLValidated () {
160
161 return isXMLValidated;
162 }
163
164 /**
165 * @param isXMLValidated The isXMLValidated to set.
166 */
167 public synchronized void setXMLValidated (boolean isXMLValidated) {
168
169 this.isXMLValidated = isXMLValidated;
170 }
171 }