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.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 | public class HashMapUtils | |
28 | { | |
29 | //~ Constructors ------------------------------------------------------------------------------- | |
30 | ||
31 | protected HashMapUtils() | |
32 | 0 | { |
33 | // block public access | |
34 | 0 | } |
35 | ||
36 | //~ Methods ------------------------------------------------------------------------------------ | |
37 | ||
38 | /** | |
39 | * Calculates initial capacity needed to hold <code>size</code> elements in | |
40 | * a HashMap or Hashtable without forcing an expensive increase in internal | |
41 | * capacity. Capacity is based on the default load factor of .75. | |
42 | * <p> | |
43 | * Usage: <code>Map map = new HashMap(HashMapUtils.calcCapacity(10));<code> | |
44 | * </p> | |
45 | * @param size the number of items that will be put into a HashMap | |
46 | * @return initial capacity needed | |
47 | */ | |
48 | public static final int calcCapacity(int size) | |
49 | { | |
50 | 0 | return ((size * 4) + 3) / 3; |
51 | } | |
52 | ||
53 | /** | |
54 | * Creates a new <code>HashMap</code> that has all of the elements | |
55 | * of <code>map1</code> and <code>map2</code> (on key collision, the latter | |
56 | * override the former). | |
57 | * | |
58 | * @param map1 the fist hashmap to merge | |
59 | * @param map2 the second hashmap to merge | |
60 | * @return new hashmap | |
61 | */ | |
62 | public static HashMap merge(Map map1, Map map2) | |
63 | { | |
64 | 0 | HashMap retval = new HashMap(calcCapacity(map1.size() + map2.size())); |
65 | ||
66 | 0 | retval.putAll(map1); |
67 | 0 | retval.putAll(map2); |
68 | ||
69 | 0 | return retval; |
70 | } | |
71 | ||
72 | /** | |
73 | * spit out each name/value pair | |
74 | */ | |
75 | public static String mapToString(Map map) | |
76 | { | |
77 | 0 | Set entries = map.entrySet(); |
78 | 0 | Iterator iter = entries.iterator(); |
79 | 0 | StringBuffer buff = new StringBuffer(); |
80 | 0 | while (iter.hasNext()) |
81 | { | |
82 | 0 | Map.Entry entry = (Map.Entry) iter.next(); |
83 | 0 | buff.append("[" + entry.getKey() + "," + entry.getValue() + "]\n"); |
84 | 0 | } |
85 | 0 | return buff.toString(); |
86 | } | |
87 | ||
88 | } |