View Javadoc

1   package org.molwind.io;
2   
3   import java.awt.image.BufferedImage;
4   import java.io.File;
5   import java.io.IOException;
6   import javax.imageio.ImageIO;
7   
8   
9   import org.apache.commons.collections.map.LRUMap;
10  
11  /*
12   * This file is part of Molwind.
13   *
14   * Molwind is free software: you can redistribute it and/or modify
15   * it under the terms of the GNU General Public License as published by
16   * the Free Software Foundation, either version 3 of the License, or
17   * (at your option) any later version.
18   *
19   * Molwind is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22   * GNU General Public License for more details.
23   *
24   * You should have received a copy of the GNU General Public License
25   * along with Molwind. If not, see <http://www.gnu.org/licenses/>.
26   */
27  
28  /**
29   * Stores generated images in the cache.
30   * <p>
31   *
32   *
33   * @version 0.1
34   */
35  public class ImageFileCache {
36  
37  
38      private static LRUMap imageCache;
39  
40  
41  
42  
43  
44      
45  
46      /**
47       * Get the  imageCache.
48       * @return the  imageCache.
49       */
50      public LRUMap getImageCache() {
51  	return imageCache;
52      }
53  
54      /**
55       * Set the imageCache.
56       * @param new The new  value.
57       */
58      public void set(LRUMap  newCache) {
59  	this.imageCache = newCache;
60      }
61  
62      
63      
64      /**
65       *
66       * Empty constructer to build a Cache with the maximum size of 100
67       *
68       *
69       */
70      public ImageFileCache(){
71  	imageCache = new LRUMap();
72  	
73      }
74      
75      /**
76       *
77       * Constructor to build a Cache with the given size 
78       *
79       *
80       *
81       */
82      public ImageFileCache(int size){
83  	imageCache = new LRUMap(size);
84      }
85  
86      /**
87       * Returns the instance of the image file cache.
88       *
89       * @return
90       *      the image file cache instance
91       */
92      public static ImageFileCache getInstance() {
93          return new ImageFileCache();
94      }
95  
96  
97      /**
98       * Updates the given Image in the image cache.
99       *
100      * @param imageFile
101      *      the image file
102      * @param imageCache
103      *      the image file cache
104      */
105     public void update(final File imageFile, final ImageFileCache imageFileCache) 
106 	throws IOException{
107         
108 	Object image = imageFileCache.getImage(imageFile.getName());
109 	if(image == null)
110 	    imageFileCache.putImage(imageFile);
111 	    	    
112     }
113     /*
114      * Returns the fitting BufferedImage to the given key
115      *
116      * @param filename
117      *       the name of the ImageFile
118      * @return
119      *       the ImageFile
120      */
121     public BufferedImage getImage(String fileName){
122 	return (BufferedImage)imageCache.get(fileName);
123 
124     }
125     
126     /**
127      * Puts a Buffered image to the Chache with the filename as key
128      *
129      * @param imageFile
130      *
131      *
132      */
133     public void putImage(File imageFile)
134 	throws IOException{
135 	BufferedImage bImage = getImageFromFile(imageFile);
136 	if(bImage != null)
137 	    imageCache.put(imageFile.getName(),bImage);
138 	
139     }
140     
141     private BufferedImage getImageFromFile(File imageFile)
142 	throws IOException {
143         
144         if (!imageFile.exists()) {
145             return null;
146         }
147         return ImageIO.read(imageFile);
148     }
149 
150     /**
151      * Returns the given image from the image file cache.
152      *
153      * @param imageFile
154      *      the image file
155      * @return
156      *      the cached image
157      */
158     public BufferedImage getImage(final File imageFile) {
159        	Object image = imageCache.get(imageFile.getName());
160 	if(image != null)
161 	    return (BufferedImage) image;
162         return null;
163     }
164 
165 }