Clover coverage report -
Coverage timestamp: Fri Nov 19 2004 13:41:51 PST
file stats: LOC: 129   Methods: 11
NCLOC: 113   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ImageCache.java 63.6% 71.4% 72.7% 69.5%
coverage coverage
 1    package photospace.graphics;
 2   
 3    import java.awt.*;
 4    import java.awt.image.*;
 5    import java.io.*;
 6    import java.util.*;
 7    import java.util.List;
 8    import org.apache.commons.logging.*;
 9    import com.sun.image.codec.jpeg.*;
 10    import photospace.vfs.*;
 11    import photospace.vfs.event.*;
 12   
 13    public class ImageCache
 14    implements java.io.Serializable
 15    {
 16    private static final Log log = LogFactory.getLog(ImageCache.class);
 17   
 18    private static final Transform[] transforms = new Transform[] {
 19    new Transform(new Dimension(1000, 500), Transform.FIT),
 20    new Transform(new Dimension(100, 100), Transform.FILL),
 21    new Transform(new Dimension(50, 50), Transform.FILL),
 22    };
 23   
 24    private File root;
 25    private FileSystem filesystem;
 26    private FileSystemWatcher watcher;
 27   
 28  1 public void setRoot(File root)
 29    {
 30  1 this.root = root;
 31  1 log.info("Using image cache root " + root);
 32    }
 33   
 34  0 public int sync()
 35    {
 36  0 return sync(getPhotoFiles());
 37    }
 38   
 39  0 private File[] getPhotoFiles()
 40    {
 41  0 List files = new ArrayList(watcher.getSnapshot().keySet());
 42  0 FileFilter filter = new PhotoFileFilter();
 43  0 for (Iterator i = files.iterator(); i.hasNext();)
 44    {
 45  0 File file = (File) i.next();
 46  0 if (!filter.accept(file)) i.remove();
 47    }
 48  0 return (File[]) files.toArray(new File[] {});
 49    }
 50   
 51  2 public int sync(File[] files)
 52    {
 53  2 int count = 0;
 54  2 for (int i = 0; i < files.length; i++)
 55    {
 56  0 if (files[i].isDirectory()) continue;
 57  0 if (!files[i].exists()) continue;
 58  4 String path = filesystem.getPath(files[i]);
 59  0 if (log.isDebugEnabled()) log.debug("Updating cache for " + transforms.length + " sizes of " + path);
 60  4 boolean updated = update(files[i], path, transforms);
 61  2 if (updated) count++;
 62    }
 63  2 return count;
 64    }
 65   
 66  4 public boolean update(File file, String path, Transform[] transforms)
 67    {
 68  4 boolean updated = false;
 69  4 try
 70    {
 71  4 BufferedImage image = null;
 72  4 for (int i = 0; i < transforms.length; i++)
 73    {
 74  12 File cached = cachedFile(path, transforms[i]);
 75  6 if (cached.exists() && cached.lastModified() >= file.lastModified()) continue;
 76   
 77  2 if (image == null) image = Sampler.read(file);
 78  6 scale(image, path, transforms[i]);
 79  6 updated = true;
 80    }
 81    }
 82    catch (Exception e)
 83    {
 84  0 log.error("Error updating image cache for " + file);
 85    }
 86  4 return updated;
 87    }
 88   
 89  6 public BufferedImage scale(BufferedImage image, String path, Transform transform)
 90    throws IOException
 91    {
 92  6 BufferedImage scaled = Sampler.scale(image, transform);
 93  6 cache(scaled, path, transform);
 94  6 return scaled;
 95    }
 96   
 97  6 public void cache(BufferedImage image, String path, Transform transform)
 98    throws IOException
 99    {
 100  6 File cached = cachedFile(path, transform);
 101  0 if (!cached.getParentFile().exists()) cached.getParentFile().mkdirs();
 102  6 write(image, new FileOutputStream(cached));
 103    }
 104   
 105  6 private void write(BufferedImage image, OutputStream out) throws IOException
 106    {
 107  6 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
 108  6 encoder.encode(image);
 109  6 out.close();
 110    }
 111   
 112  18 public File cachedFile(String path, Transform transform)
 113    {
 114  18 return new File(root, path + "#" +
 115    "w=" + transform.getDimension().width +
 116    "&h=" + transform.getDimension().height +
 117    "&scale=" + transform.getScale());
 118    }
 119   
 120  1 public void setFilesystem(FileSystem filesystem)
 121    {
 122  1 this.filesystem = filesystem;
 123    }
 124   
 125  0 public void setWatcher(FileSystemWatcher watcher)
 126    {
 127  0 this.watcher = watcher;
 128    }
 129    }