Coverage Report - org.dishevelled.iconbundle.impl.CachingIconBundle
 
Classes in this File Line Coverage Branch Coverage Complexity
CachingIconBundle
100%
14/14
100%
4/4
2.333
 
 1  
 /*
 2  
 
 3  
     dsh-iconbundle  Bundles of variants for icon images.
 4  
     Copyright (c) 2003-2011 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  
  * @version $Revision: 974 $ $Date: 2011-01-04 21:40:23 -0600 (Tue, 04 Jan 2011) $
 43  
  */
 44  
 public class CachingIconBundle
 45  
     implements IconBundle
 46  
 {
 47  
     /** Wrapped icon bundle. */
 48  
     private final IconBundle iconBundle;
 49  
 
 50  
     /** Image cache, keyed by { direction, state, size }. */
 51  
     private final Map cache;
 52  
     //private final Map<IconBundleKey, Image> cache;
 53  
 
 54  
 
 55  
     /**
 56  
      * Create a new caching icon bundle which wraps the
 57  
      * specified icon bundle.
 58  
      *
 59  
      * @param iconBundle icon bundle to wrap, must not be null
 60  
      */
 61  
     public CachingIconBundle(final IconBundle iconBundle)
 62  14
     {
 63  14
         if (iconBundle == null)
 64  
         {
 65  1
             throw new IllegalArgumentException("iconBundle must not be null");
 66  
         }
 67  
 
 68  13
         this.iconBundle = iconBundle;
 69  13
         this.cache = new HashMap();
 70  
         //this.cache = new HashMap<IconBundleKey, Image>();
 71  13
     }
 72  
 
 73  
 
 74  
     /**
 75  
      * Invalidate the cache of images.
 76  
      */
 77  
     public void invalidateCache()
 78  
     {
 79  1
         cache.clear();
 80  1
     }
 81  
 
 82  
     /** {@inheritDoc} */
 83  
     public Image getImage(final Component component,
 84  
                           final IconTextDirection direction,
 85  
                           final IconState state,
 86  
                           final IconSize size)
 87  
     {
 88  2551
         IconBundleKey key = new IconBundleKey(direction, state, size);
 89  2548
         if (cache.containsKey(key))
 90  
         {
 91  
             // return cache.get(key);
 92  1274
             return (Image) cache.get(key);
 93  
         }
 94  
         else
 95  
         {
 96  1274
             Image image = iconBundle.getImage(component, direction, state, size);
 97  1274
             cache.put(key, image);
 98  1274
             return image;
 99  
         }
 100  
     }
 101  
 }