CachingStatisticMapRepository.java from Tea Stats at Krugle
Show CachingStatisticMapRepository.java syntax highlighted
package net.time4tea.webstats.statistic.repo;
import net.time4tea.webstats.jms.EnvironmentException;
import net.time4tea.webstats.statistic.StatisticMap;
import org.joda.time.Instant;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* Originally richja 24-Jul-2007
*/
public class CachingStatisticMapRepository implements StatisticMapRepository {
private StatisticMapRepository repository;
Map<String,StatisticMap> loaded = new HashMap<String, StatisticMap>();
public CachingStatisticMapRepository(StatisticMapRepository repository) {
this.repository = repository;
}
public StatisticMap getStatisticMapForInstant(String name, Instant instant) throws EnvironmentException {
String key = name + instant;
if ( loaded.containsKey(key)) {
return loaded.get(key);
}
else {
StatisticMap map = repository.getStatisticMapForInstant(name, instant);
loaded.put(key, map);
return map;
}
}
public Collection<StatisticMap> findAll() throws EnvironmentException {
sync();
return repository.findAll();
}
public Collection<Instant> findPeriodsAvailableForNamedMap(String name) throws EnvironmentException {
sync();
return repository.findPeriodsAvailableForNamedMap(name);
}
public void sync() throws EnvironmentException {
loaded = new HashMap<String, StatisticMap>();
repository.sync();
}
}
See more files for this project here