KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > MapUtils


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.io.PrintStream JavaDoc;
19 import java.text.NumberFormat JavaDoc;
20 import java.text.ParseException JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.ResourceBundle JavaDoc;
28 import java.util.SortedMap JavaDoc;
29 import java.util.TreeMap JavaDoc;
30
31 import org.apache.commons.collections.map.FixedSizeMap;
32 import org.apache.commons.collections.map.FixedSizeSortedMap;
33 import org.apache.commons.collections.map.LazyMap;
34 import org.apache.commons.collections.map.LazySortedMap;
35 import org.apache.commons.collections.map.ListOrderedMap;
36 import org.apache.commons.collections.map.PredicatedMap;
37 import org.apache.commons.collections.map.PredicatedSortedMap;
38 import org.apache.commons.collections.map.TransformedMap;
39 import org.apache.commons.collections.map.TransformedSortedMap;
40 import org.apache.commons.collections.map.TypedMap;
41 import org.apache.commons.collections.map.TypedSortedMap;
42 import org.apache.commons.collections.map.UnmodifiableMap;
43 import org.apache.commons.collections.map.UnmodifiableSortedMap;
44
45 /**
46  * Provides utility methods and decorators for
47  * {@link Map} and {@link SortedMap} instances.
48  * <p>
49  * It contains various type safe methods
50  * as well as other useful features like deep copying.
51  * <p>
52  * It also provides the following decorators:
53  *
54  * <ul>
55  * <li>{@link #fixedSizeMap(Map)}
56  * <li>{@link #fixedSizeSortedMap(SortedMap)}
57  * <li>{@link #lazyMap(Map,Factory)}
58  * <li>{@link #lazyMap(Map,Transformer)}
59  * <li>{@link #lazySortedMap(SortedMap,Factory)}
60  * <li>{@link #lazySortedMap(SortedMap,Transformer)}
61  * <li>{@link #predicatedMap(Map,Predicate,Predicate)}
62  * <li>{@link #predicatedSortedMap(SortedMap,Predicate,Predicate)}
63  * <li>{@link #transformedMap(Map, Transformer, Transformer)}
64  * <li>{@link #transformedSortedMap(SortedMap, Transformer, Transformer)}
65  * <li>{@link #typedMap(Map, Class, Class)}
66  * <li>{@link #typedSortedMap(SortedMap, Class, Class)}
67  * </ul>
68  *
69  * @since Commons Collections 1.0
70  * @version $Revision: 1.46 $ $Date: 2004/04/21 20:34:11 $
71  *
72  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
73  * @author <a HREF="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
74  * @author <a HREF="mailto:knielsen@apache.org">Kasper Nielsen</a>
75  * @author Paul Jack
76  * @author Stephen Colebourne
77  * @author Matthew Hawthorne
78  * @author Arun Mammen Thomas
79  * @author Janek Bogucki
80  * @author Max Rydahl Andersen
81  * @author <a HREF="mailto:equinus100@hotmail.com">Ashwin S</a>
82  */

83 public class MapUtils {
84     
85     /**
86      * An empty unmodifiable map.
87      * This was not provided in JDK1.2.
88      */

89     public static final Map JavaDoc EMPTY_MAP = UnmodifiableMap.decorate(new HashMap JavaDoc(1));
90     /**
91      * An empty unmodifiable sorted map.
92      * This is not provided in the JDK.
93      */

94     public static final SortedMap JavaDoc EMPTY_SORTED_MAP = UnmodifiableSortedMap.decorate(new TreeMap JavaDoc());
95     /**
96      * String used to indent the verbose and debug Map prints.
97      */

98     private static final String JavaDoc INDENT_STRING = " ";
99
100     /**
101      * <code>MapUtils</code> should not normally be instantiated.
102      */

103     public MapUtils() {
104     }
105     
106     // Type safe getters
107
//-------------------------------------------------------------------------
108
/**
109      * Gets from a Map in a null-safe manner.
110      *
111      * @param map the map to use
112      * @param key the key to look up
113      * @return the value in the Map, <code>null</code> if null map input
114      */

115     public static Object JavaDoc getObject(final Map JavaDoc map, final Object JavaDoc key) {
116         if (map != null) {
117             return map.get(key);
118         }
119         return null;
120     }
121
122     /**
123      * Gets a String from a Map in a null-safe manner.
124      * <p>
125      * The String is obtained via <code>toString</code>.
126      *
127      * @param map the map to use
128      * @param key the key to look up
129      * @return the value in the Map as a String, <code>null</code> if null map input
130      */

131     public static String JavaDoc getString(final Map JavaDoc map, final Object JavaDoc key) {
132         if (map != null) {
133             Object JavaDoc answer = map.get(key);
134             if (answer != null) {
135                 return answer.toString();
136             }
137         }
138         return null;
139     }
140
141     /**
142      * Gets a Boolean from a Map in a null-safe manner.
143      * <p>
144      * If the value is a <code>Boolean</code> it is returned directly.
145      * If the value is a <code>String</code> and it equals 'true' ignoring case
146      * then <code>true</code> is returned, otherwise <code>false</code>.
147      * If the value is a <code>Number</code> an integer zero value returns
148      * <code>false</code> and non-zero returns <code>true</code>.
149      * Otherwise, <code>null</code> is returned.
150      *
151      * @param map the map to use
152      * @param key the key to look up
153      * @return the value in the Map as a Boolean, <code>null</code> if null map input
154      */

155     public static Boolean JavaDoc getBoolean(final Map JavaDoc map, final Object JavaDoc key) {
156         if (map != null) {
157             Object JavaDoc answer = map.get(key);
158             if (answer != null) {
159                 if (answer instanceof Boolean JavaDoc) {
160                     return (Boolean JavaDoc) answer;
161                     
162                 } else if (answer instanceof String JavaDoc) {
163                     return new Boolean JavaDoc((String JavaDoc) answer);
164                     
165                 } else if (answer instanceof Number JavaDoc) {
166                     Number JavaDoc n = (Number JavaDoc) answer;
167                     return (n.intValue() != 0) ? Boolean.TRUE : Boolean.FALSE;
168                 }
169             }
170         }
171         return null;
172     }
173
174     /**
175      * Gets a Number from a Map in a null-safe manner.
176      * <p>
177      * If the value is a <code>Number</code> it is returned directly.
178      * If the value is a <code>String</code> it is converted using
179      * {@link NumberFormat#parse(String)} on the system default formatter
180      * returning <code>null</code> if the conversion fails.
181      * Otherwise, <code>null</code> is returned.
182      *
183      * @param map the map to use
184      * @param key the key to look up
185      * @return the value in the Map as a Number, <code>null</code> if null map input
186      */

187     public static Number JavaDoc getNumber(final Map JavaDoc map, final Object JavaDoc key) {
188         if (map != null) {
189             Object JavaDoc answer = map.get(key);
190             if (answer != null) {
191                 if (answer instanceof Number JavaDoc) {
192                     return (Number JavaDoc) answer;
193                     
194                 } else if (answer instanceof String JavaDoc) {
195                     try {
196                         String JavaDoc text = (String JavaDoc) answer;
197                         return NumberFormat.getInstance().parse(text);
198                         
199                     } catch (ParseException JavaDoc e) {
200                         logInfo(e);
201                     }
202                 }
203             }
204         }
205         return null;
206     }
207
208     /**
209      * Gets a Byte from a Map in a null-safe manner.
210      * <p>
211      * The Byte is obtained from the results of {@link #getNumber(Map,Object)}.
212      *
213      * @param map the map to use
214      * @param key the key to look up
215      * @return the value in the Map as a Byte, <code>null</code> if null map input
216      */

217     public static Byte JavaDoc getByte(final Map JavaDoc map, final Object JavaDoc key) {
218         Number JavaDoc answer = getNumber(map, key);
219         if (answer == null) {
220             return null;
221         } else if (answer instanceof Byte JavaDoc) {
222             return (Byte JavaDoc) answer;
223         }
224         return new Byte JavaDoc(answer.byteValue());
225     }
226
227     /**
228      * Gets a Short from a Map in a null-safe manner.
229      * <p>
230      * The Short is obtained from the results of {@link #getNumber(Map,Object)}.
231      *
232      * @param map the map to use
233      * @param key the key to look up
234      * @return the value in the Map as a Short, <code>null</code> if null map input
235      */

236     public static Short JavaDoc getShort(final Map JavaDoc map, final Object JavaDoc key) {
237         Number JavaDoc answer = getNumber(map, key);
238         if (answer == null) {
239             return null;
240         } else if (answer instanceof Short JavaDoc) {
241             return (Short JavaDoc) answer;
242         }
243         return new Short JavaDoc(answer.shortValue());
244     }
245
246     /**
247      * Gets a Integer from a Map in a null-safe manner.
248      * <p>
249      * The Integer is obtained from the results of {@link #getNumber(Map,Object)}.
250      *
251      * @param map the map to use
252      * @param key the key to look up
253      * @return the value in the Map as a Integer, <code>null</code> if null map input
254      */

255     public static Integer JavaDoc getInteger(final Map JavaDoc map, final Object JavaDoc key) {
256         Number JavaDoc answer = getNumber(map, key);
257         if (answer == null) {
258             return null;
259         } else if (answer instanceof Integer JavaDoc) {
260             return (Integer JavaDoc) answer;
261         }
262         return new Integer JavaDoc(answer.intValue());
263     }
264
265     /**
266      * Gets a Long from a Map in a null-safe manner.
267      * <p>
268      * The Long is obtained from the results of {@link #getNumber(Map,Object)}.
269      *
270      * @param map the map to use
271      * @param key the key to look up
272      * @return the value in the Map as a Long, <code>null</code> if null map input
273      */

274     public static Long JavaDoc getLong(final Map JavaDoc map, final Object JavaDoc key) {
275         Number JavaDoc answer = getNumber(map, key);
276         if (answer == null) {
277             return null;
278         } else if (answer instanceof Long JavaDoc) {
279             return (Long JavaDoc) answer;
280         }
281         return new Long JavaDoc(answer.longValue());
282     }
283
284     /**
285      * Gets a Float from a Map in a null-safe manner.
286      * <p>
287      * The Float is obtained from the results of {@link #getNumber(Map,Object)}.
288      *
289      * @param map the map to use
290      * @param key the key to look up
291      * @return the value in the Map as a Float, <code>null</code> if null map input
292      */

293     public static Float JavaDoc getFloat(final Map JavaDoc map, final Object JavaDoc key) {
294         Number JavaDoc answer = getNumber(map, key);
295         if (answer == null) {
296             return null;
297         } else if (answer instanceof Float JavaDoc) {
298             return (Float JavaDoc) answer;
299         }
300         return new Float JavaDoc(answer.floatValue());
301     }
302
303     /**
304      * Gets a Double from a Map in a null-safe manner.
305      * <p>
306      * The Double is obtained from the results of {@link #getNumber(Map,Object)}.
307      *
308      * @param map the map to use
309      * @param key the key to look up
310      * @return the value in the Map as a Double, <code>null</code> if null map input
311      */

312     public static Double JavaDoc getDouble(final Map JavaDoc map, final Object JavaDoc key) {
313         Number JavaDoc answer = getNumber(map, key);
314         if (answer == null) {
315             return null;
316         } else if (answer instanceof Double JavaDoc) {
317             return (Double JavaDoc) answer;
318         }
319         return new Double JavaDoc(answer.doubleValue());
320     }
321
322     /**
323      * Gets a Map from a Map in a null-safe manner.
324      * <p>
325      * If the value returned from the specified map is not a Map then
326      * <code>null</code> is returned.
327      *
328      * @param map the map to use
329      * @param key the key to look up
330      * @return the value in the Map as a Map, <code>null</code> if null map input
331      */

332     public static Map JavaDoc getMap(final Map JavaDoc map, final Object JavaDoc key) {
333         if (map != null) {
334             Object JavaDoc answer = map.get(key);
335             if (answer != null && answer instanceof Map JavaDoc) {
336                 return (Map JavaDoc) answer;
337             }
338         }
339         return null;
340     }
341
342     // Type safe getters with default values
343
//-------------------------------------------------------------------------
344
/**
345      * Looks up the given key in the given map, converting null into the
346      * given default value.
347      *
348      * @param map the map whose value to look up
349      * @param key the key of the value to look up in that map
350      * @param defaultValue what to return if the value is null
351      * @return the value in the map, or defaultValue if the original value
352      * is null or the map is null
353      */

354     public static Object JavaDoc getObject( Map JavaDoc map, Object JavaDoc key, Object JavaDoc defaultValue ) {
355         if ( map != null ) {
356             Object JavaDoc answer = map.get( key );
357             if ( answer != null ) {
358                 return answer;
359             }
360         }
361         return defaultValue;
362     }
363
364     /**
365      * Looks up the given key in the given map, converting the result into
366      * a string, using the default value if the the conversion fails.
367      *
368      * @param map the map whose value to look up
369      * @param key the key of the value to look up in that map
370      * @param defaultValue what to return if the value is null or if the
371      * conversion fails
372      * @return the value in the map as a string, or defaultValue if the
373      * original value is null, the map is null or the string conversion
374      * fails
375      */

376     public static String JavaDoc getString( Map JavaDoc map, Object JavaDoc key, String JavaDoc defaultValue ) {
377         String JavaDoc answer = getString( map, key );
378         if ( answer == null ) {
379             answer = defaultValue;
380         }
381         return answer;
382     }
383
384     /**
385      * Looks up the given key in the given map, converting the result into
386      * a boolean, using the default value if the the conversion fails.
387      *
388      * @param map the map whose value to look up
389      * @param key the key of the value to look up in that map
390      * @param defaultValue what to return if the value is null or if the
391      * conversion fails
392      * @return the value in the map as a boolean, or defaultValue if the
393      * original value is null, the map is null or the boolean conversion
394      * fails
395      */

396     public static Boolean JavaDoc getBoolean( Map JavaDoc map, Object JavaDoc key, Boolean JavaDoc defaultValue ) {
397         Boolean JavaDoc answer = getBoolean( map, key );
398         if ( answer == null ) {
399             answer = defaultValue;
400         }
401         return answer;
402     }
403
404     /**
405      * Looks up the given key in the given map, converting the result into
406      * a number, using the default value if the the conversion fails.
407      *
408      * @param map the map whose value to look up
409      * @param key the key of the value to look up in that map
410      * @param defaultValue what to return if the value is null or if the
411      * conversion fails
412      * @return the value in the map as a number, or defaultValue if the
413      * original value is null, the map is null or the number conversion
414      * fails
415      */

416     public static Number JavaDoc getNumber( Map JavaDoc map, Object JavaDoc key, Number JavaDoc defaultValue ) {
417         Number JavaDoc answer = getNumber( map, key );
418         if ( answer == null ) {
419             answer = defaultValue;
420         }
421         return answer;
422     }
423
424     /**
425      * Looks up the given key in the given map, converting the result into
426      * a byte, using the default value if the the conversion fails.
427      *
428      * @param map the map whose value to look up
429      * @param key the key of the value to look up in that map
430      * @param defaultValue what to return if the value is null or if the
431      * conversion fails
432      * @return the value in the map as a number, or defaultValue if the
433      * original value is null, the map is null or the number conversion
434      * fails
435      */

436     public static Byte JavaDoc getByte( Map JavaDoc map, Object JavaDoc key, Byte JavaDoc defaultValue ) {
437         Byte JavaDoc answer = getByte( map, key );
438         if ( answer == null ) {
439             answer = defaultValue;
440         }
441         return answer;
442     }
443
444     /**
445      * Looks up the given key in the given map, converting the result into
446      * a short, using the default value if the the conversion fails.
447      *
448      * @param map the map whose value to look up
449      * @param key the key of the value to look up in that map
450      * @param defaultValue what to return if the value is null or if the
451      * conversion fails
452      * @return the value in the map as a number, or defaultValue if the
453      * original value is null, the map is null or the number conversion
454      * fails
455      */

456     public static Short JavaDoc getShort( Map JavaDoc map, Object JavaDoc key, Short JavaDoc defaultValue ) {
457         Short JavaDoc answer = getShort( map, key );
458         if ( answer == null ) {
459             answer = defaultValue;
460         }
461         return answer;
462     }
463
464     /**
465      * Looks up the given key in the given map, converting the result into
466      * an integer, using the default value if the the conversion fails.
467      *
468      * @param map the map whose value to look up
469      * @param key the key of the value to look up in that map
470      * @param defaultValue what to return if the value is null or if the
471      * conversion fails
472      * @return the value in the map as a number, or defaultValue if the
473      * original value is null, the map is null or the number conversion
474      * fails
475      */

476     public static Integer JavaDoc getInteger( Map JavaDoc map, Object JavaDoc key, Integer JavaDoc defaultValue ) {
477         Integer JavaDoc answer = getInteger( map, key );
478         if ( answer == null ) {
479             answer = defaultValue;
480         }
481         return answer;
482     }
483
484     /**
485      * Looks up the given key in the given map, converting the result into
486      * a long, using the default value if the the conversion fails.
487      *
488      * @param map the map whose value to look up
489      * @param key the key of the value to look up in that map
490      * @param defaultValue what to return if the value is null or if the
491      * conversion fails
492      * @return the value in the map as a number, or defaultValue if the
493      * original value is null, the map is null or the number conversion
494      * fails
495      */

496     public static Long JavaDoc getLong( Map JavaDoc map, Object JavaDoc key, Long JavaDoc defaultValue ) {
497         Long JavaDoc answer = getLong( map, key );
498         if ( answer == null ) {
499             answer = defaultValue;
500         }
501         return answer;
502     }
503
504     /**
505      * Looks up the given key in the given map, converting the result into
506      * a float, using the default value if the the conversion fails.
507      *
508      * @param map the map whose value to look up
509      * @param key the key of the value to look up in that map
510      * @param defaultValue what to return if the value is null or if the
511      * conversion fails
512      * @return the value in the map as a number, or defaultValue if the
513      * original value is null, the map is null or the number conversion
514      * fails
515      */

516     public static Float JavaDoc getFloat( Map JavaDoc map, Object JavaDoc key, Float JavaDoc defaultValue ) {
517         Float JavaDoc answer = getFloat( map, key );
518         if ( answer == null ) {
519             answer = defaultValue;
520         }
521         return answer;
522     }
523
524     /**
525      * Looks up the given key in the given map, converting the result into
526      * a double, using the default value if the the conversion fails.
527      *
528      * @param map the map whose value to look up
529      * @param key the key of the value to look up in that map
530      * @param defaultValue what to return if the value is null or if the
531      * conversion fails
532      * @return the value in the map as a number, or defaultValue if the
533      * original value is null, the map is null or the number conversion
534      * fails
535      */

536     public static Double JavaDoc getDouble( Map JavaDoc map, Object JavaDoc key, Double JavaDoc defaultValue ) {
537         Double JavaDoc answer = getDouble( map, key );
538         if ( answer == null ) {
539             answer = defaultValue;
540         }
541         return answer;
542     }
543
544     /**
545      * Looks up the given key in the given map, converting the result into
546      * a map, using the default value if the the conversion fails.
547      *
548      * @param map the map whose value to look up
549      * @param key the key of the value to look up in that map
550      * @param defaultValue what to return if the value is null or if the
551      * conversion fails
552      * @return the value in the map as a number, or defaultValue if the
553      * original value is null, the map is null or the map conversion
554      * fails
555      */

556     public static Map JavaDoc getMap( Map JavaDoc map, Object JavaDoc key, Map JavaDoc defaultValue ) {
557         Map JavaDoc answer = getMap( map, key );
558         if ( answer == null ) {
559             answer = defaultValue;
560         }
561         return answer;
562     }
563     
564
565     // Type safe primitive getters
566
//-------------------------------------------------------------------------
567
/**
568      * Gets a boolean from a Map in a null-safe manner.
569      * <p>
570      * If the value is a <code>Boolean</code> its value is returned.
571      * If the value is a <code>String</code> and it equals 'true' ignoring case
572      * then <code>true</code> is returned, otherwise <code>false</code>.
573      * If the value is a <code>Number</code> an integer zero value returns
574      * <code>false</code> and non-zero returns <code>true</code>.
575      * Otherwise, <code>false</code> is returned.
576      *
577      * @param map the map to use
578      * @param key the key to look up
579      * @return the value in the Map as a Boolean, <code>false</code> if null map input
580      */

581     public static boolean getBooleanValue(final Map JavaDoc map, final Object JavaDoc key) {
582         Boolean JavaDoc booleanObject = getBoolean(map, key);
583         if (booleanObject == null) {
584             return false;
585         }
586         return booleanObject.booleanValue();
587     }
588
589     /**
590      * Gets a byte from a Map in a null-safe manner.
591      * <p>
592      * The byte is obtained from the results of {@link #getNumber(Map,Object)}.
593      *
594      * @param map the map to use
595      * @param key the key to look up
596      * @return the value in the Map as a byte, <code>0</code> if null map input
597      */

598     public static byte getByteValue(final Map JavaDoc map, final Object JavaDoc key) {
599         Byte JavaDoc byteObject = getByte(map, key);
600         if (byteObject == null) {
601             return 0;
602         }
603         return byteObject.byteValue();
604     }
605
606     /**
607      * Gets a short from a Map in a null-safe manner.
608      * <p>
609      * The short is obtained from the results of {@link #getNumber(Map,Object)}.
610      *
611      * @param map the map to use
612      * @param key the key to look up
613      * @return the value in the Map as a short, <code>0</code> if null map input
614      */

615     public static short getShortValue(final Map JavaDoc map, final Object JavaDoc key) {
616         Short JavaDoc shortObject = getShort(map, key);
617         if (shortObject == null) {
618             return 0;
619         }
620         return shortObject.shortValue();
621     }
622
623     /**
624      * Gets an int from a Map in a null-safe manner.
625      * <p>
626      * The int is obtained from the results of {@link #getNumber(Map,Object)}.
627      *
628      * @param map the map to use
629      * @param key the key to look up
630      * @return the value in the Map as an int, <code>0</code> if null map input
631      */

632     public static int getIntValue(final Map JavaDoc map, final Object JavaDoc key) {
633         Integer JavaDoc integerObject = getInteger(map, key);
634         if (integerObject == null) {
635             return 0;
636         }
637         return integerObject.intValue();
638     }
639
640     /**
641      * Gets a long from a Map in a null-safe manner.
642      * <p>
643      * The long is obtained from the results of {@link #getNumber(Map,Object)}.
644      *
645      * @param map the map to use
646      * @param key the key to look up
647      * @return the value in the Map as a long, <code>0L</code> if null map input
648      */

649     public static long getLongValue(final Map JavaDoc map, final Object JavaDoc key) {
650         Long JavaDoc longObject = getLong(map, key);
651         if (longObject == null) {
652             return 0L;
653         }
654         return longObject.longValue();
655     }
656
657     /**
658      * Gets a float from a Map in a null-safe manner.
659      * <p>
660      * The float is obtained from the results of {@link #getNumber(Map,Object)}.
661      *
662      * @param map the map to use
663      * @param key the key to look up
664      * @return the value in the Map as a float, <code>0.0F</code> if null map input
665      */

666     public static float getFloatValue(final Map JavaDoc map, final Object JavaDoc key) {
667         Float JavaDoc floatObject = getFloat(map, key);
668         if (floatObject == null) {
669             return 0f;
670         }
671         return floatObject.floatValue();
672     }
673
674     /**
675      * Gets a double from a Map in a null-safe manner.
676      * <p>
677      * The double is obtained from the results of {@link #getNumber(Map,Object)}.
678      *
679      * @param map the map to use
680      * @param key the key to look up
681      * @return the value in the Map as a double, <code>0.0</code> if null map input
682      */

683     public static double getDoubleValue(final Map JavaDoc map, final Object JavaDoc key) {
684         Double JavaDoc doubleObject = getDouble(map, key);
685         if (doubleObject == null) {
686             return 0d;
687         }
688         return doubleObject.doubleValue();
689     }
690
691     // Type safe primitive getters with default values
692
//-------------------------------------------------------------------------
693
/**
694      * Gets a boolean from a Map in a null-safe manner,
695      * using the default value if the the conversion fails.
696      * <p>
697      * If the value is a <code>Boolean</code> its value is returned.
698      * If the value is a <code>String</code> and it equals 'true' ignoring case
699      * then <code>true</code> is returned, otherwise <code>false</code>.
700      * If the value is a <code>Number</code> an integer zero value returns
701      * <code>false</code> and non-zero returns <code>true</code>.
702      * Otherwise, <code>defaultValue</code> is returned.
703      *
704      * @param map the map to use
705      * @param key the key to look up
706      * @param defaultValue return if the value is null or if the
707      * conversion fails
708      * @return the value in the Map as a Boolean, <code>defaultValue</code> if null map input
709      */

710     public static boolean getBooleanValue(final Map JavaDoc map, final Object JavaDoc key, boolean defaultValue) {
711         Boolean JavaDoc booleanObject = getBoolean(map, key);
712         if (booleanObject == null) {
713             return defaultValue;
714         }
715         return booleanObject.booleanValue();
716     }
717
718     /**
719      * Gets a byte from a Map in a null-safe manner,
720      * using the default value if the the conversion fails.
721      * <p>
722      * The byte is obtained from the results of {@link #getNumber(Map,Object)}.
723      *
724      * @param map the map to use
725      * @param key the key to look up
726      * @param defaultValue return if the value is null or if the
727      * conversion fails
728      * @return the value in the Map as a byte, <code>defaultValue</code> if null map input
729      */

730     public static byte getByteValue(final Map JavaDoc map, final Object JavaDoc key, byte defaultValue) {
731         Byte JavaDoc byteObject = getByte(map, key);
732         if (byteObject == null) {
733             return defaultValue;
734         }
735         return byteObject.byteValue();
736     }
737
738     /**
739      * Gets a short from a Map in a null-safe manner,
740      * using the default value if the the conversion fails.
741      * <p>
742      * The short is obtained from the results of {@link #getNumber(Map,Object)}.
743      *
744      * @param map the map to use
745      * @param key the key to look up
746      * @param defaultValue return if the value is null or if the
747      * conversion fails
748      * @return the value in the Map as a short, <code>defaultValue</code> if null map input
749      */

750     public static short getShortValue(final Map JavaDoc map, final Object JavaDoc key, short defaultValue) {
751         Short JavaDoc shortObject = getShort(map, key);
752         if (shortObject == null) {
753             return defaultValue;
754         }
755         return shortObject.shortValue();
756     }
757
758     /**
759      * Gets an int from a Map in a null-safe manner,
760      * using the default value if the the conversion fails.
761      * <p>
762      * The int is obtained from the results of {@link #getNumber(Map,Object)}.
763      *
764      * @param map the map to use
765      * @param key the key to look up
766      * @param defaultValue return if the value is null or if the
767      * conversion fails
768      * @return the value in the Map as an int, <code>defaultValue</code> if null map input
769      */

770     public static int getIntValue(final Map JavaDoc map, final Object JavaDoc key, int defaultValue) {
771         Integer JavaDoc integerObject = getInteger(map, key);
772         if (integerObject == null) {
773             return defaultValue;
774         }
775         return integerObject.intValue();
776     }
777
778     /**
779      * Gets a long from a Map in a null-safe manner,
780      * using the default value if the the conversion fails.
781      * <p>
782      * The long is obtained from the results of {@link #getNumber(Map,Object)}.
783      *
784      * @param map the map to use
785      * @param key the key to look up
786      * @param defaultValue return if the value is null or if the
787      * conversion fails
788      * @return the value in the Map as a long, <code>defaultValue</code> if null map input
789      */

790     public static long getLongValue(final Map