1 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 38 public class ValueListAdapterCacheDecorator implements ValueListAdapter 39 { 40 41 private long cacheTimeout = Long.MAX_VALUE; 42 43 private long cacheCreateTime = -1; 44 45 private long nextRefresh = System.currentTimeMillis(); 46 47 private ValueListAdapter decoratedValueListAdapter; 48 49 private ValueList valueList; 50 51 54 public int getAdapterType() 55 { 56 return decoratedValueListAdapter.getAdapterType(); 57 } 58 59 62 public ValueList getValueList(String name, ValueListInfo info) 63 { 64 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 83 public void setCacheTimeout(long cacheTimeout) 84 { 85 this.cacheTimeout = cacheTimeout; 86 } 87 88 93 public void setParent(ValueListAdapter decoratedValueListAdapter) 94 { 95 this.decoratedValueListAdapter = decoratedValueListAdapter; 96 } 97 } | Popular Tags |