| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CacheHashMap |
|
| 2.25;2.25 |
| 1 | package org.jtheque.core.managers.persistence; | |
| 2 | ||
| 3 | import java.util.HashMap; | |
| 4 | import java.util.Iterator; | |
| 5 | import java.util.Map; | |
| 6 | ||
| 7 | /* | |
| 8 | * This file is part of JTheque. | |
| 9 | * | |
| 10 | * JTheque is free software: you can redistribute it and/or modify | |
| 11 | * it under the terms of the GNU General Public License as published by | |
| 12 | * the Free Software Foundation, either version 3 of the License. | |
| 13 | * | |
| 14 | * JTheque is distributed in the hope that it will be useful, | |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | * GNU General Public License for more details. | |
| 18 | * | |
| 19 | * You should have received a copy of the GNU General Public License | |
| 20 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
| 21 | */ | |
| 22 | ||
| 23 | /** | |
| 24 | * Simple HashMap who doesn't support null value. | |
| 25 | * | |
| 26 | * @author Baptiste Wicht | |
| 27 | * @param <K> The type of the key. | |
| 28 | * @param <V> The type of the value. | |
| 29 | */ | |
| 30 | final class CacheHashMap<K, V> extends HashMap<K, V> { | |
| 31 | /** | |
| 32 | * Create a new CacheHashMap. | |
| 33 | * | |
| 34 | * @param initialCapacity The initial capacity of the map. | |
| 35 | */ | |
| 36 | CacheHashMap(int initialCapacity) { | |
| 37 | 0 | super(initialCapacity); |
| 38 | 0 | } |
| 39 | ||
| 40 | @Override | |
| 41 | public V put(K key, V value) { | |
| 42 | 0 | if (value == null) { |
| 43 | 0 | return null; |
| 44 | } | |
| 45 | ||
| 46 | 0 | return super.put(key, value); |
| 47 | } | |
| 48 | ||
| 49 | @Override | |
| 50 | public void putAll(Map<? extends K, ? extends V> m) { | |
| 51 | 0 | super.putAll(m); |
| 52 | ||
| 53 | 0 | if (containsValue(null)) { |
| 54 | 0 | clearNullValues(); |
| 55 | } | |
| 56 | 0 | } |
| 57 | ||
| 58 | /** | |
| 59 | * Clear all entries who contains null values. | |
| 60 | */ | |
| 61 | private void clearNullValues() { | |
| 62 | 0 | Iterator<Map.Entry<K, V>> iterator = entrySet().iterator(); |
| 63 | ||
| 64 | 0 | while (iterator.hasNext()) { |
| 65 | 0 | Map.Entry<K, V> entry = iterator.next(); |
| 66 | ||
| 67 | 0 | if (entry.getValue() == null) { |
| 68 | 0 | iterator.remove(); |
| 69 | } | |
| 70 | 0 | } |
| 71 | 0 | } |
| 72 | } |