KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > utils > StringKeyDirtyFlagMap


1 /*
2  * Copyright 2004-2006 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16 package org.quartz.utils;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * <p>
24  * An implementation of <code>Map</code> that wraps another <code>Map</code>
25  * and flags itself 'dirty' when it is modified, enforces that all keys are
26  * Strings.
27  * </p>
28  *
29  * <p>
30  * All allowsTransientData flag related methods are deprecated as of version 1.6.
31  * </p>
32  */

33 public class StringKeyDirtyFlagMap extends DirtyFlagMap {
34     static final long serialVersionUID = -9076749120524952280L;
35     
36     /**
37      * @deprecated JDBCJobStores no longer prune out transient data. If you
38      * include non-Serializable values in the Map, you will now get an
39      * exception when attempting to store it in a database.
40      */

41     private boolean allowsTransientData = false;
42
43     public StringKeyDirtyFlagMap() {
44         super();
45     }
46
47     public StringKeyDirtyFlagMap(int initialCapacity) {
48         super(initialCapacity);
49     }
50
51     public StringKeyDirtyFlagMap(int initialCapacity, float loadFactor) {
52         super(initialCapacity, loadFactor);
53     }
54
55     /**
56      * Get a copy of the Map's String keys in an array of Strings.
57      */

58     public String JavaDoc[] getKeys() {
59         return (String JavaDoc[]) keySet().toArray(new String JavaDoc[size()]);
60     }
61
62     /**
63      * Tell the <code>StringKeyDirtyFlagMap</code> that it should
64      * allow non-<code>Serializable</code> values. Enforces that the Map
65      * doesn't already include transient data.
66      *
67      * @deprecated JDBCJobStores no longer prune out transient data. If you
68      * include non-Serializable values in the Map, you will now get an
69      * exception when attempting to store it in a database.
70      */

71     public void setAllowsTransientData(boolean allowsTransientData) {
72     
73         if (containsTransientData() && !allowsTransientData) {
74             throw new IllegalStateException JavaDoc(
75                 "Cannot set property 'allowsTransientData' to 'false' "
76                     + "when data map contains non-serializable objects.");
77         }
78     
79         this.allowsTransientData = allowsTransientData;
80     }
81
82     /**
83      * Whether the <code>StringKeyDirtyFlagMap</code> allows
84      * non-<code>Serializable</code> values.
85      *
86      * @deprecated JDBCJobStores no longer prune out transient data. If you
87      * include non-Serializable values in the Map, you will now get an
88      * exception when attempting to store it in a database.
89      */

90     public boolean getAllowsTransientData() {
91         return allowsTransientData;
92     }
93
94     /**
95      * Determine whether any values in this Map do not implement
96      * <code>Serializable</code>. Always returns false if this Map
97      * is flagged to not allow transient data.
98      *
99      * @deprecated JDBCJobStores no longer prune out transient data. If you
100      * include non-Serializable values in the Map, you will now get an
101      * exception when attempting to store it in a database.
102      */

103     public boolean containsTransientData() {
104         if (!getAllowsTransientData()) { // short circuit...
105
return false;
106         }
107     
108         String JavaDoc[] keys = getKeys();
109         for (int i = 0; i < keys.length; i++) {
110             Object JavaDoc o = super.get(keys[i]);
111             if (!(o instanceof Serializable JavaDoc)) {
112                 return true;
113             }
114         }
115     
116         return false;
117     }
118
119     /**
120      * Removes any data values in the map that are non-Serializable. Does
121      * nothing if this Map does not allow transient data.
122      *
123      * @deprecated JDBCJobStores no longer prune out transient data. If you
124      * include non-Serializable values in the Map, you will now get an
125      * exception when attempting to store it in a database.
126      */

127     public void removeTransientData() {
128         if (!getAllowsTransientData()) { // short circuit...
129
return;
130         }
131     
132         String JavaDoc[] keys = getKeys();
133         for (int i = 0; i < keys.length; i++) {
134             Object JavaDoc o = super.get(keys[i]);
135             if (!(o instanceof Serializable JavaDoc)) {
136                 remove(keys[i]);
137             }
138         }
139     }
140     
141     /**
142      * <p>
143      * Adds the name-value pairs in the given <code>Map</code> to the
144      * <code>StringKeyDirtyFlagMap</code>.
145      * </p>
146      *
147      * <p>
148      * All keys must be <code>String</code>s.
149      * </p>
150      */

151     public void putAll(Map JavaDoc map) {
152         for (Iterator JavaDoc entryIter = map.entrySet().iterator(); entryIter.hasNext();) {
153             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entryIter.next();
154             
155             // will throw IllegalArgumentException if key is not a String
156
put(entry.getKey(), entry.getValue());
157         }
158     }
159
160     /**
161      * <p>
162      * Adds the given <code>int</code> value to the <code>StringKeyDirtyFlagMap</code>.
163      * </p>
164      */

165     public void put(String JavaDoc key, int value) {
166         super.put(key, new Integer JavaDoc(value));
167     }
168
169     /**
170      * <p>
171      * Adds the given <code>long</code> value to the <code>StringKeyDirtyFlagMap</code>.
172      * </p>
173      */

174     public void put(String JavaDoc key, long value) {
175         super.put(key, new Long JavaDoc(value));
176     }
177
178     /**
179      * <p>
180      * Adds the given <code>float</code> value to the <code>StringKeyDirtyFlagMap</code>.
181      * </p>
182      */

183     public void put(String JavaDoc key, float value) {
184         super.put(key, new Float JavaDoc(value));
185     }
186
187     /**
188      * <p>
189      * Adds the given <code>double</code> value to the <code>StringKeyDirtyFlagMap</code>.
190      * </p>
191      */

192     public void put(String JavaDoc key, double value) {
193         super.put(key, new Double JavaDoc(value));
194     }
195
196     /**
197      * <p>
198      * Adds the given <code>boolean</code> value to the <code>StringKeyDirtyFlagMap</code>.
199      * </p>
200      */

201     public void put(String JavaDoc key, boolean value) {
202         super.put(key, new Boolean JavaDoc(value));
203     }
204
205     /**
206      * <p>
207      * Adds the given <code>char</code> value to the <code>StringKeyDirtyFlagMap</code>.
208      * </p>
209      */

210     public void put(String JavaDoc key, char value) {
211         super.put(key, new Character JavaDoc(value));
212     }
213
214     /**
215      * <p>
216      * Adds the given <code>String</code> value to the <code>StringKeyDirtyFlagMap</code>.
217      * </p>
218      */

219     public void put(String JavaDoc key, String JavaDoc value) {
220         super.put(key, value);
221     }
222
223     /**
224      * <p>
225      * Adds the given <code>Object</code> value to the <code>StringKeyDirtyFlagMap</code>.
226      * </p>
227      */

228     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
229         if (!(key instanceof String JavaDoc)) {
230             throw new IllegalArgumentException JavaDoc(
231                     "Keys in map must be Strings.");
232         }
233     
234         return super.put(key, value);
235     }
236
237     /**
238      * <p>
239      * Retrieve the identified <code>int</code> value from the <code>StringKeyDirtyFlagMap</code>.
240      * </p>
241      *
242      * @throws ClassCastException
243      * if the identified object is not an Integer.
244      */

245     public int getInt(String JavaDoc key) {
246         Object JavaDoc obj = get(key);
247     
248         try {
249             return ((Integer JavaDoc) obj).intValue();
250         } catch (Exception JavaDoc e) {
251             throw new ClassCastException JavaDoc("Identified object is not an Integer.");
252         }
253     }
254
255     /**
256      * <p>
257      * Retrieve the identified <code>long</code> value from the <code>StringKeyDirtyFlagMap</code>.
258      * </p>
259      *
260      * @throws ClassCastException
261      * if the identified object is not a Long.
262      */

263     public long getLong(String JavaDoc key) {
264         Object JavaDoc obj = get(key);
265     
266         try {
267             return ((Long JavaDoc) obj).longValue();
268         } catch (Exception JavaDoc e) {
269             throw new ClassCastException JavaDoc("Identified object is not a Long.");
270         }
271     }
272
273     /**
274      * <p>
275      * Retrieve the identified <code>float</code> value from the <code>StringKeyDirtyFlagMap</code>.
276      * </p>
277      *
278      * @throws ClassCastException
279      * if the identified object is not a Float.
280      */

281     public float getFloat(String JavaDoc key) {
282         Object JavaDoc obj = get(key);
283     
284         try {
285             return ((Float JavaDoc) obj).floatValue();
286         } catch (Exception JavaDoc e) {
287             throw new ClassCastException JavaDoc("Identified object is not a Float.");
288         }
289     }
290
291     /**
292      * <p>
293      * Retrieve the identified <code>double</code> value from the <code>StringKeyDirtyFlagMap</code>.
294      * </p>
295      *
296      * @throws ClassCastException
297      * if the identified object is not a Double.
298      */

299     public double getDouble(String JavaDoc key) {
300         Object JavaDoc obj = get(key);
301     
302         try {
303             return ((Double JavaDoc) obj).doubleValue();
304         } catch (Exception JavaDoc e) {
305             throw new ClassCastException JavaDoc("Identified object is not a Double.");
306         }
307     }
308
309     /**
310      * <p>
311      * Retrieve the identified <code>boolean</code> value from the <code>StringKeyDirtyFlagMap</code>.
312      * </p>
313      *
314      * @throws ClassCastException
315      * if the identified object is not a Boolean.
316      */

317     public boolean getBoolean(String JavaDoc key) {
318         Object JavaDoc obj = get(key);
319     
320         try {
321             return ((Boolean JavaDoc) obj).booleanValue();
322         } catch (Exception JavaDoc e) {
323             throw new ClassCastException JavaDoc("Identified object is not a Boolean.");
324         }
325     }
326
327     /**
328      * <p>
329      * Retrieve the identified <code>char</code> value from the <code>StringKeyDirtyFlagMap</code>.
330      * </p>
331      *
332      * @throws ClassCastException
333      * if the identified object is not a Character.
334      */

335     public char getChar(String JavaDoc key) {
336         Object JavaDoc obj = get(key);
337     
338         try {
339             return ((Character JavaDoc) obj).charValue();
340         } catch (Exception JavaDoc e) {
341             throw new ClassCastException JavaDoc("Identified object is not a Character.");
342         }
343     }
344
345     /**
346      * <p>
347      * Retrieve the identified <code>String</code> value from the <code>StringKeyDirtyFlagMap</code>.
348      * </p>
349      *
350      * @throws ClassCastException
351      * if the identified object is not a String.
352      */

353     public String JavaDoc getString(String JavaDoc key) {
354         Object JavaDoc obj = get(key);
355     
356         try {
357             return (String JavaDoc) obj;
358         } catch (Exception JavaDoc e) {
359             throw new ClassCastException JavaDoc("Identified object is not a String.");
360         }
361     }
362 }
363
Popular Tags