| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CachingIconBundle |
|
| 2.3333333333333335;2.333 |
| 1 | /* | |
| 2 | ||
| 3 | dsh-iconbundle Bundles of variants for icon images. | |
| 4 | Copyright (c) 2003-2013 held jointly by the individual authors. | |
| 5 | ||
| 6 | This library is free software; you can redistribute it and/or modify it | |
| 7 | under the terms of the GNU Lesser General Public License as published | |
| 8 | by the Free Software Foundation; either version 3 of the License, or (at | |
| 9 | your option) any later version. | |
| 10 | ||
| 11 | This library is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or | |
| 13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | |
| 14 | License for more details. | |
| 15 | ||
| 16 | You should have received a copy of the GNU Lesser General Public License | |
| 17 | along with this library; if not, write to the Free Software Foundation, | |
| 18 | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
| 19 | ||
| 20 | > http://www.fsf.org/licensing/licenses/lgpl.html | |
| 21 | > http://www.opensource.org/licenses/lgpl-license.php | |
| 22 | ||
| 23 | */ | |
| 24 | package org.dishevelled.iconbundle.impl; | |
| 25 | ||
| 26 | import java.awt.Image; | |
| 27 | import java.awt.Component; | |
| 28 | ||
| 29 | import java.util.Map; | |
| 30 | import java.util.HashMap; | |
| 31 | ||
| 32 | import org.dishevelled.iconbundle.IconSize; | |
| 33 | import org.dishevelled.iconbundle.IconState; | |
| 34 | import org.dishevelled.iconbundle.IconBundle; | |
| 35 | import org.dishevelled.iconbundle.IconTextDirection; | |
| 36 | ||
| 37 | /** | |
| 38 | * Wrapper around an existing implementation of IconBundle | |
| 39 | * that caches images. | |
| 40 | * | |
| 41 | * @author Michael Heuer | |
| 42 | */ | |
| 43 | public class CachingIconBundle | |
| 44 | implements IconBundle | |
| 45 | { | |
| 46 | /** Wrapped icon bundle. */ | |
| 47 | private final IconBundle iconBundle; | |
| 48 | ||
| 49 | /** Image cache, keyed by { direction, state, size }. */ | |
| 50 | private final Map cache; | |
| 51 | //private final Map<IconBundleKey, Image> cache; | |
| 52 | ||
| 53 | ||
| 54 | /** | |
| 55 | * Create a new caching icon bundle which wraps the | |
| 56 | * specified icon bundle. | |
| 57 | * | |
| 58 | * @param iconBundle icon bundle to wrap, must not be null | |
| 59 | */ | |
| 60 | public CachingIconBundle(final IconBundle iconBundle) | |
| 61 | 14 | { |
| 62 | 14 | if (iconBundle == null) |
| 63 | { | |
| 64 | 1 | throw new IllegalArgumentException("iconBundle must not be null"); |
| 65 | } | |
| 66 | ||
| 67 | 13 | this.iconBundle = iconBundle; |
| 68 | 13 | this.cache = new HashMap(); |
| 69 | //this.cache = new HashMap<IconBundleKey, Image>(); | |
| 70 | 13 | } |
| 71 | ||
| 72 | ||
| 73 | /** | |
| 74 | * Invalidate the cache of images. | |
| 75 | */ | |
| 76 | public void invalidateCache() | |
| 77 | { | |
| 78 | 1 | cache.clear(); |
| 79 | 1 | } |
| 80 | ||
| 81 | /** {@inheritDoc} */ | |
| 82 | public Image getImage(final Component component, | |
| 83 | final IconTextDirection direction, | |
| 84 | final IconState state, | |
| 85 | final IconSize size) | |
| 86 | { | |
| 87 | 2551 | IconBundleKey key = new IconBundleKey(direction, state, size); |
| 88 | 2548 | if (cache.containsKey(key)) |
| 89 | { | |
| 90 | // return cache.get(key); | |
| 91 | 1274 | return (Image) cache.get(key); |
| 92 | } | |
| 93 | else | |
| 94 | { | |
| 95 | 1274 | Image image = iconBundle.getImage(component, direction, state, size); |
| 96 | 1274 | cache.put(key, image); |
| 97 | 1274 | return image; |
| 98 | } | |
| 99 | } | |
| 100 | } |