View Javadoc

1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.codehaus.activemq.util;
19  
20  import java.util.Map;
21  
22  /***
23   * A bunch of utility methods for working with maps
24   *
25   * @version $Revision: 1.1 $
26   */
27  public class MapHelper {
28      /***
29       * Extracts the value from the map and coerces to a String
30       */
31      public static String getString(Map map, String key) {
32          Object 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 map, String key, int defaultValue) {
41          Object value = map.get(key);
42          if (value instanceof Number) {
43              return ((Number) value).intValue();
44          }
45          else if (value instanceof String) {
46              return Integer.parseInt((String) value);
47          }
48          return defaultValue;
49      }
50  }