KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > MapHelper


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

18 package org.apache.activemq.util;
19
20 import java.util.Map JavaDoc;
21
22 /**
23  * A bunch of utility methods for working with maps
24  *
25  * @version $Revision$
26  */

27 public class MapHelper {
28     /**
29      * Extracts the value from the map and coerces to a String
30      */

31     public static String JavaDoc getString(Map JavaDoc map, String JavaDoc key) {
32         Object JavaDoc answer = map.get(key);
33         return (answer != null) ? answer.toString() : null;
34     }
35
36     /**
37      * Extracts the value from the map and coerces to an int value
38      * or returns a default value if one could not be found or coerced
39      */

40     public static int getInt(Map JavaDoc map, String JavaDoc key, int defaultValue) {
41         Object JavaDoc value = map.get(key);
42         if (value instanceof Number JavaDoc) {
43             return ((Number JavaDoc) value).intValue();
44         }
45         else if (value instanceof String JavaDoc) {
46             return Integer.parseInt((String JavaDoc) value);
47         }
48         return defaultValue;
49     }
50 }
51
Popular Tags