01: /*
02: * User: Michael Rettig
03: * Date: Sep 20, 2002
04: * Time: 8:42:05 PM
05: */
06: package net.sourceforge.jaxor;
07:
08: import net.sourceforge.jaxor.api.QueryCache;
09: import net.sourceforge.jaxor.util.SystemException;
10:
11: import java.util.ArrayList;
12: import java.util.List;
13:
14: public abstract class CachingLazyLoader extends LazyLoader {
15:
16: private final QueryParams _args;
17: private boolean _unique;
18: private final String _sql;
19: private final Class _clzz;
20: private final QueryCache _cache;
21:
22: public CachingLazyLoader(Class clzz, String method,
23: QueryParams args, QueryCache cache, boolean unique) {
24: _args = args;
25: _unique = unique;
26: this ._sql = method;
27: _clzz = clzz;
28: _cache = cache;
29: }
30:
31: public Object getValue() {
32: List result = _cache.get(_clzz, _sql, _args);
33: if (result == null) {
34: result = (List) super .getValue();
35: _cache.put(_clzz, _sql, _args, result);
36: }
37: if (_unique) {
38: if (result.size() == 0)
39: throw new EntityNotFoundException(_sql, _args);
40: else if (result.size() > 1)
41: throw new SystemException(
42: "Unique Query Expected one result, not "
43: + result.size());
44: return result.get(0);
45: }
46: // defensive copy so changes to the list made in domain code do not affect cache.
47: return new ArrayList(result);
48: }
49: }
|