Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
HashMapUtils |
|
| 1.25;1.25 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with 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, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | package org.apache.myfaces.shared_impl.util; | |
20 | ||
21 | import java.util.HashMap; | |
22 | import java.util.Map; | |
23 | import java.util.Set; | |
24 | import java.util.Iterator; | |
25 | ||
26 | ||
27 | /** | |
28 | * @author Anton Koinov (latest modification by $Author: matzew $) | |
29 | * @version $Revision: 557350 $ $Date: 2007-07-18 13:19:50 -0500 (Wed, 18 Jul 2007) $ | |
30 | */ | |
31 | public class HashMapUtils | |
32 | { | |
33 | //~ Constructors ------------------------------------------------------------------------------- | |
34 | ||
35 | protected HashMapUtils() | |
36 | 0 | { |
37 | // block public access | |
38 | 0 | } |
39 | ||
40 | //~ Methods ------------------------------------------------------------------------------------ | |
41 | ||
42 | /** | |
43 | * Calculates initial capacity needed to hold <code>size</code> elements in | |
44 | * a HashMap or Hashtable without forcing an expensive increase in internal | |
45 | * capacity. Capacity is based on the default load factor of .75. | |
46 | * <p> | |
47 | * Usage: <code>Map map = new HashMap(HashMapUtils.calcCapacity(10));<code> | |
48 | * </p> | |
49 | * @param size the number of items that will be put into a HashMap | |
50 | * @return initial capacity needed | |
51 | */ | |
52 | public static final int calcCapacity(int size) | |
53 | { | |
54 | 0 | return ((size * 4) + 3) / 3; |
55 | } | |
56 | ||
57 | /** | |
58 | * Creates a new <code>HashMap</code> that has all of the elements | |
59 | * of <code>map1</code> and <code>map2</code> (on key collision, the latter | |
60 | * override the former). | |
61 | * | |
62 | * @param map1 the fist hashmap to merge | |
63 | * @param map2 the second hashmap to merge | |
64 | * @return new hashmap | |
65 | */ | |
66 | public static HashMap merge(Map map1, Map map2) | |
67 | { | |
68 | 0 | HashMap retval = new HashMap(calcCapacity(map1.size() + map2.size())); |
69 | ||
70 | 0 | retval.putAll(map1); |
71 | 0 | retval.putAll(map2); |
72 | ||
73 | 0 | return retval; |
74 | } | |
75 | ||
76 | /** | |
77 | * spit out each name/value pair | |
78 | */ | |
79 | public static String mapToString(Map map){ | |
80 | 0 | Set entries = map.entrySet(); |
81 | 0 | Iterator iter = entries.iterator(); |
82 | 0 | StringBuffer buff = new StringBuffer(); |
83 | 0 | while (iter.hasNext()) |
84 | { | |
85 | 0 | Map.Entry entry = (Map.Entry) iter.next(); |
86 | 0 | buff.append("[" + entry.getKey() + "," + entry.getValue() + "]\n"); |
87 | 0 | } |
88 | 0 | return buff.toString(); |
89 | } | |
90 | ||
91 | } |