001: /*
002: * Copyright 2004-2005 OpenSymphony
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy
006: * of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations
014: * under the License.
015: *
016: */
017:
018: /*
019: * Previously Copyright (c) 2001-2004 James House
020: */
021: package org.quartz;
022:
023: import java.io.Serializable;
024: import java.util.Map;
025:
026: import org.quartz.utils.StringKeyDirtyFlagMap;
027:
028: /**
029: * <p>
030: * Holds state information for <code>Job</code> instances.
031: * </p>
032: *
033: * <p>
034: * <code>JobDataMap</code> instances are stored once when the <code>Job</code>
035: * is added to a scheduler. They are also re-persisted after every execution of
036: * <code>StatefulJob</code> instances.
037: * </p>
038: *
039: * <p>
040: * <code>JobDataMap</code> instances can also be stored with a
041: * <code>Trigger</code>. This can be useful in the case where you have a Job
042: * that is stored in the scheduler for regular/repeated use by multiple
043: * Triggers, yet with each independent triggering, you want to supply the
044: * Job with different data inputs.
045: * </p>
046: *
047: * <p>
048: * The <code>JobExecutionContext</code> passed to a Job at execution time
049: * also contains a convenience <code>JobDataMap</code> that is the result
050: * of merging the contents of the trigger's JobDataMap (if any) over the
051: * Job's JobDataMap (if any).
052: * </p>
053: *
054: * @see Job
055: * @see StatefulJob
056: * @see Trigger
057: * @see JobExecutionContext
058: *
059: * @author James House
060: */
061: public class JobDataMap extends StringKeyDirtyFlagMap implements
062: Serializable {
063:
064: private static final long serialVersionUID = -6939901990106713909L;
065:
066: /*
067: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
068: *
069: * Data members.
070: *
071: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
072: */
073:
074: /**
075: * <p>
076: * Create an empty <code>JobDataMap</code>.
077: * </p>
078: */
079: public JobDataMap() {
080: super (15);
081: }
082:
083: /**
084: * <p>
085: * Create a <code>JobDataMap</code> with the given data.
086: * </p>
087: */
088: public JobDataMap(Map map) {
089: this ();
090:
091: putAll(map);
092: }
093:
094: /*
095: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
096: *
097: * Interface.
098: *
099: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100: */
101:
102: /**
103: * <p>
104: * Adds the given <code>boolean</code> value as a string version to the
105: * <code>Job</code>'s data map.
106: * </p>
107: */
108: public void putAsString(String key, boolean value) {
109: String strValue = new Boolean(value).toString();
110:
111: super .put(key, strValue);
112: }
113:
114: /**
115: * <p>
116: * Adds the given <code>Boolean</code> value as a string version to the
117: * <code>Job</code>'s data map.
118: * </p>
119: */
120: public void putAsString(String key, Boolean value) {
121: String strValue = value.toString();
122:
123: super .put(key, strValue);
124: }
125:
126: /**
127: * <p>
128: * Adds the given <code>char</code> value as a string version to the
129: * <code>Job</code>'s data map.
130: * </p>
131: */
132: public void putAsString(String key, char value) {
133: String strValue = new Character(value).toString();
134:
135: super .put(key, strValue);
136: }
137:
138: /**
139: * <p>
140: * Adds the given <code>Character</code> value as a string version to the
141: * <code>Job</code>'s data map.
142: * </p>
143: */
144: public void putAsString(String key, Character value) {
145: String strValue = value.toString();
146:
147: super .put(key, strValue);
148: }
149:
150: /**
151: * <p>
152: * Adds the given <code>double</code> value as a string version to the
153: * <code>Job</code>'s data map.
154: * </p>
155: */
156: public void putAsString(String key, double value) {
157: String strValue = new Double(value).toString();
158:
159: super .put(key, strValue);
160: }
161:
162: /**
163: * <p>
164: * Adds the given <code>Double</code> value as a string version to the
165: * <code>Job</code>'s data map.
166: * </p>
167: */
168: public void putAsString(String key, Double value) {
169: String strValue = value.toString();
170:
171: super .put(key, strValue);
172: }
173:
174: /**
175: * <p>
176: * Adds the given <code>float</code> value as a string version to the
177: * <code>Job</code>'s data map.
178: * </p>
179: */
180: public void putAsString(String key, float value) {
181: String strValue = new Float(value).toString();
182:
183: super .put(key, strValue);
184: }
185:
186: /**
187: * <p>
188: * Adds the given <code>Float</code> value as a string version to the
189: * <code>Job</code>'s data map.
190: * </p>
191: */
192: public void putAsString(String key, Float value) {
193: String strValue = value.toString();
194:
195: super .put(key, strValue);
196: }
197:
198: /**
199: * <p>
200: * Adds the given <code>int</code> value as a string version to the
201: * <code>Job</code>'s data map.
202: * </p>
203: */
204: public void putAsString(String key, int value) {
205: String strValue = new Integer(value).toString();
206:
207: super .put(key, strValue);
208: }
209:
210: /**
211: * <p>
212: * Adds the given <code>Integer</code> value as a string version to the
213: * <code>Job</code>'s data map.
214: * </p>
215: */
216: public void putAsString(String key, Integer value) {
217: String strValue = value.toString();
218:
219: super .put(key, strValue);
220: }
221:
222: /**
223: * <p>
224: * Adds the given <code>long</code> value as a string version to the
225: * <code>Job</code>'s data map.
226: * </p>
227: */
228: public void putAsString(String key, long value) {
229: String strValue = new Long(value).toString();
230:
231: super .put(key, strValue);
232: }
233:
234: /**
235: * <p>
236: * Adds the given <code>Long</code> value as a string version to the
237: * <code>Job</code>'s data map.
238: * </p>
239: */
240: public void putAsString(String key, Long value) {
241: String strValue = value.toString();
242:
243: super .put(key, strValue);
244: }
245:
246: /**
247: * <p>
248: * Retrieve the identified <code>int</code> value from the <code>JobDataMap</code>.
249: * </p>
250: *
251: * @throws ClassCastException
252: * if the identified object is not a String.
253: */
254: public int getIntFromString(String key) {
255: Object obj = get(key);
256:
257: return new Integer((String) obj).intValue();
258: }
259:
260: /**
261: * <p>
262: * Retrieve the identified <code>int</code> value from the <code>JobDataMap</code>.
263: * </p>
264: *
265: * @throws ClassCastException
266: * if the identified object is not a String or Integeger.
267: */
268: public long getIntValue(String key) {
269: Object obj = get(key);
270:
271: if (obj instanceof String) {
272: return getIntFromString(key);
273: } else {
274: return getInt(key);
275: }
276: }
277:
278: /**
279: * <p>
280: * Retrieve the identified <code>int</code> value from the <code>JobDataMap</code>.
281: * </p>
282: *
283: * @throws ClassCastException
284: * if the identified object is not a String.
285: */
286: public Integer getIntegerFromString(String key) {
287: Object obj = get(key);
288:
289: return new Integer((String) obj);
290: }
291:
292: /**
293: * <p>
294: * Retrieve the identified <code>boolean</code> value from the <code>JobDataMap</code>.
295: * </p>
296: *
297: * @throws ClassCastException
298: * if the identified object is not a String.
299: */
300: public boolean getBooleanValueFromString(String key) {
301: Object obj = get(key);
302:
303: return new Boolean((String) obj).booleanValue();
304: }
305:
306: /**
307: * <p>
308: * Retrieve the identified <code>boolean</code> value from the
309: * <code>JobDataMap</code>.
310: * </p>
311: *
312: * @throws ClassCastException
313: * if the identified object is not a String or Boolean.
314: */
315: public boolean getBooleanValue(String key) {
316: Object obj = get(key);
317:
318: if (obj instanceof String) {
319: return getBooleanValueFromString(key);
320: } else {
321: return getBoolean(key);
322: }
323: }
324:
325: /**
326: * <p>
327: * Retrieve the identified <code>Boolean</code> value from the <code>JobDataMap</code>.
328: * </p>
329: *
330: * @throws ClassCastException
331: * if the identified object is not a String.
332: */
333: public Boolean getBooleanFromString(String key) {
334: Object obj = get(key);
335:
336: return new Boolean((String) obj);
337: }
338:
339: /**
340: * <p>
341: * Retrieve the identified <code>char</code> value from the <code>JobDataMap</code>.
342: * </p>
343: *
344: * @throws ClassCastException
345: * if the identified object is not a String.
346: */
347: public char getCharFromString(String key) {
348: Object obj = get(key);
349:
350: return ((String) obj).charAt(0);
351: }
352:
353: /**
354: * <p>
355: * Retrieve the identified <code>Character</code> value from the <code>JobDataMap</code>.
356: * </p>
357: *
358: * @throws ClassCastException
359: * if the identified object is not a String.
360: */
361: public Character getCharacterFromString(String key) {
362: Object obj = get(key);
363:
364: return new Character(((String) obj).charAt(0));
365: }
366:
367: /**
368: * <p>
369: * Retrieve the identified <code>double</code> value from the <code>JobDataMap</code>.
370: * </p>
371: *
372: * @throws ClassCastException
373: * if the identified object is not a String.
374: */
375: public double getDoubleValueFromString(String key) {
376: Object obj = get(key);
377:
378: return new Double((String) obj).doubleValue();
379: }
380:
381: /**
382: * <p>
383: * Retrieve the identified <code>double</code> value from the <code>JobDataMap</code>.
384: * </p>
385: *
386: * @throws ClassCastException
387: * if the identified object is not a String or Double.
388: */
389: public double getDoubleValue(String key) {
390: Object obj = get(key);
391:
392: if (obj instanceof String) {
393: return getDoubleValueFromString(key);
394: } else {
395: return getDouble(key);
396: }
397: }
398:
399: /**
400: * <p>
401: * Retrieve the identified <code>Double</code> value from the <code>JobDataMap</code>.
402: * </p>
403: *
404: * @throws ClassCastException
405: * if the identified object is not a String.
406: */
407: public Double getDoubleFromString(String key) {
408: Object obj = get(key);
409:
410: return new Double((String) obj);
411: }
412:
413: /**
414: * <p>
415: * Retrieve the identified <code>float</code> value from the <code>JobDataMap</code>.
416: * </p>
417: *
418: * @throws ClassCastException
419: * if the identified object is not a String.
420: */
421: public float getFloatValueFromString(String key) {
422: Object obj = get(key);
423:
424: return new Float((String) obj).floatValue();
425: }
426:
427: /**
428: * <p>
429: * Retrieve the identified <code>float</code> value from the <code>JobDataMap</code>.
430: * </p>
431: *
432: * @throws ClassCastException
433: * if the identified object is not a String or Float.
434: */
435: public float getFloatValue(String key) {
436: Object obj = get(key);
437:
438: if (obj instanceof String) {
439: return getFloatValueFromString(key);
440: } else {
441: return getFloat(key);
442: }
443: }
444:
445: /**
446: * <p>
447: * Retrieve the identified <code>Float</code> value from the <code>JobDataMap</code>.
448: * </p>
449: *
450: * @throws ClassCastException
451: * if the identified object is not a String.
452: */
453: public Float getFloatFromString(String key) {
454: Object obj = get(key);
455:
456: return new Float((String) obj);
457: }
458:
459: /**
460: * <p>
461: * Retrieve the identified <code>long</code> value from the <code>JobDataMap</code>.
462: * </p>
463: *
464: * @throws ClassCastException
465: * if the identified object is not a String.
466: */
467: public long getLongValueFromString(String key) {
468: Object obj = get(key);
469:
470: return new Long((String) obj).longValue();
471: }
472:
473: /**
474: * <p>
475: * Retrieve the identified <code>long</code> value from the <code>JobDataMap</code>.
476: * </p>
477: *
478: * @throws ClassCastException
479: * if the identified object is not a String or Long.
480: */
481: public long getLongValue(String key) {
482: Object obj = get(key);
483:
484: if (obj instanceof String) {
485: return getLongValueFromString(key);
486: } else {
487: return getLong(key);
488: }
489: }
490:
491: /**
492: * <p>
493: * Retrieve the identified <code>Long</code> value from the <code>JobDataMap</code>.
494: * </p>
495: *
496: * @throws ClassCastException
497: * if the identified object is not a String.
498: */
499: public Long getLongFromString(String key) {
500: Object obj = get(key);
501:
502: return new Long((String) obj);
503: }
504: }
|