View Javadoc
1   /*
2    * Copyright (c) 2012, Dienst Landelijk Gebied - Ministerie van Economische Zaken
3    * 
4    * Gepubliceerd onder de BSD 2-clause licentie, 
5    * zie https://github.com/MinELenI/CBSviewer/blob/master/LICENSE.md voor de volledige licentie.
6    */
7   package nl.mineleni.cbsviewer.servlet.wms.cache;
8   
9   import java.awt.image.BufferedImage;
10  
11  /**
12   * Cachable image implementatie.
13   * 
14   * @author prinsmc
15   * @since 1.6
16   */
17  public class CacheImage implements CachableImage<BufferedImage> {
18  
19  	/** de expire-by timestamp in milliseconden (UNIX time). */
20  	private final long expireBy;
21  
22  	/** bestandsnaam. */
23  	private String fName;
24  
25  	/** image. */
26  	private BufferedImage image = null;
27  
28  	/**
29  	 * Maak een nieuwe cache afbeelding aan.
30  	 * 
31  	 * @param image
32  	 *            de afbeelding
33  	 * @param filename
34  	 *            de bestandsnaam
35  	 * @param expireBy
36  	 *            de expire-by timestamp in milliseconden (UNIX time)
37  	 */
38  	public CacheImage(final BufferedImage image, final String filename,
39  			final long expireBy) {
40  		this.image = image;
41  		this.fName = filename;
42  		this.expireBy = expireBy;
43  	}
44  
45  	/**
46  	 * Maak een nieuwe cache afbeelding aan.
47  	 * 
48  	 * @param image
49  	 *            de afbeelding
50  	 * @param secondsToLive
51  	 *            seconds to live in de cache
52  	 */
53  	public CacheImage(final BufferedImage image, final long secondsToLive) {
54  		final long expires = secondsToLive != -1 ? System.currentTimeMillis()
55  				+ (secondsToLive * 1000) : secondsToLive;
56  		this.image = image;
57  		this.expireBy = expires;
58  	}
59  
60  	/*
61  	 * (non-Javadoc)
62  	 * 
63  	 * @see nl.mineleni.cbsviewer.servlet.wms.cache.CachableImage#getExpireBy()
64  	 */
65  	@Override
66  	public long getExpireBy() {
67  		return this.expireBy;
68  	}
69  
70  	/*
71  	 * (non-Javadoc)
72  	 * 
73  	 * @see nl.eleni.gcc.vpziek.cache.CachableImage#getImage()
74  	 */
75  	@Override
76  	public BufferedImage getItem() {
77  		return this.image;
78  	}
79  
80  	/*
81  	 * (non-Javadoc)
82  	 * 
83  	 * @see nl.eleni.gcc.vpziek.cache.CachableImage#getName()
84  	 */
85  	@Override
86  	public String getName() {
87  		return this.fName;
88  	}
89  
90  	/*
91  	 * (non-Javadoc)
92  	 * 
93  	 * @see nl.eleni.gcc.vpziek.cache.CachableImage#isValid()
94  	 */
95  	@Override
96  	public boolean isValid() {
97  		return ((this.image != null) && (this.fName != null) && (this.expireBy > System
98  				.currentTimeMillis()));
99  	}
100 
101 	/**
102 	 * Gets the image.
103 	 * 
104 	 * @return the image
105 	 */
106 	public BufferedImage getImage() {
107 		return getItem();
108 	}
109 
110 	/**
111 	 * Sets the file name.
112 	 * 
113 	 * @param name
114 	 *            the new file name
115 	 */
116 	public void setFileName(final String name) {
117 		this.fName = name;
118 	}
119 }