View Javadoc

1   /***
2    * Copyright (c) 2003 held jointly by the individual authors.            
3    *                                                                          
4    * This library is free software; you can redistribute it and/or modify it    
5    * under the terms of the GNU Lesser General Public License as published      
6    * by the Free Software Foundation; either version 2.1 of the License, or 
7    * (at your option) any later version.                                            
8    *                                                                            
9    * This library is distributed in the hope that it will be useful, but 
10   * WITHOUT ANY WARRANTY; with out even the implied warranty of 
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
12   * GNU Lesser General Public License for more details.                                                  
13   *                                                                           
14   * You should have received a copy of the GNU Lesser General Public License   
15   * along with this library;  if not, write to the Free Software Foundation,   
16   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.              
17   *                                                                            
18   * > http://www.gnu.org/copyleft/lesser.html                                  
19   * > http://www.opensource.org/licenses/lgpl-license.php
20   */
21  package net.mlw.vlh.adapter;
22  
23  import net.mlw.vlh.DefaultListBackedValueList;
24  import net.mlw.vlh.ValueList;
25  import net.mlw.vlh.ValueListAdapter;
26  import net.mlw.vlh.ValueListInfo;
27  
28  /*** This decorator is a lazy way to implement caching.  This is ideal for 
29   *  select boxes and the like.  
30   * 
31   *  <p>
32   *    Note that to make this thread safe sorting and paging must
33   *    be defined in the parent ValueListAdapter
34   *  </p>
35   * 
36   * @author mwilson
37   */
38  public class ValueListAdapterCacheDecorator implements ValueListAdapter
39  {
40  	/*** The duration in milliseconds to allow cached data to stick around. **/
41  	private long cacheTimeout = Long.MAX_VALUE;
42  	/*** The time in milliseconds the cache was created. **/
43  	private long cacheCreateTime = -1;
44  	/*** The time in milliseconds the cache should be refreshed. **/
45      private long nextRefresh = System.currentTimeMillis();
46      /*** The source for the data. **/
47  	private ValueListAdapter decoratedValueListAdapter;
48  	/*** The cached valuelist. **/
49  	private ValueList valueList;
50  
51  	/***
52  	 * @see net.mlw.vlh.ValueListAdapter#getAdapterType()
53  	 */
54  	public int getAdapterType()
55  	{
56  		return decoratedValueListAdapter.getAdapterType();
57  	}
58  
59  	/***
60  	 * @see net.mlw.vlh.ValueListAdapter#getValueList(java.lang.String, net.mlw.vlh.ValueListInfo)
61  	 */
62  	public ValueList getValueList(String name, ValueListInfo info)
63  	{
64  		//Check to see if we ever invalidate the cache.
65  		if (valueList == null || cacheTimeout != Long.MAX_VALUE)
66  		{
67  			if (nextRefresh < System.currentTimeMillis())
68  			{
69  				cacheCreateTime = System.currentTimeMillis();
70  				nextRefresh = cacheCreateTime + cacheTimeout;
71  				valueList = decoratedValueListAdapter.getValueList(name, info);
72  			}
73  		}
74  
75  		return new DefaultListBackedValueList(valueList.getList(), valueList.getValueListInfo());
76  	}
77  
78  	/*** Sets the duration in milliseconds to allow 
79  	 *  cached data to stick around.
80  	 * 
81  	 * @param cachTimeout Duration in milliseconds.
82  	 */
83  	public void setCacheTimeout(long cacheTimeout)
84  	{
85  		this.cacheTimeout = cacheTimeout;
86  	}
87  
88  	/*** The underling ValueListAdapter that retrieves the ValueList
89  	 *  to be cached.
90  	 * 
91  	 * @param decoratedValueListAdapter The parent ValueListAdapter.
92  	 */
93  	public void setParent(ValueListAdapter decoratedValueListAdapter)
94  	{
95  		this.decoratedValueListAdapter = decoratedValueListAdapter;
96  	}
97  }