Skip to content

Method: LazyLoadingMapProxy(Object, FieldSpecification, UnitOfWork)

1: package cz.cvut.kbss.jopa.proxy.lazy;
2:
3: import cz.cvut.kbss.jopa.exception.LazyLoadingException;
4: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
5: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
6: import cz.cvut.kbss.jopa.utils.CollectionFactory;
7:
8: import java.util.Collection;
9: import java.util.Map;
10: import java.util.Set;
11:
12: /**
13: * {@link Map} proxy that triggers lazy loading when its contents is accessed.
14: *
15: * @param <T> Type of the object whose attribute value is this map proxy
16: * @param <K> Map key type
17: * @param <V> Map value type
18: */
19: public class LazyLoadingMapProxy<T, K, V> implements LazyLoadingProxy<Map<K, V>>, Map<K, V> {
20:
21: protected final T owner;
22: protected final FieldSpecification<? super T, Map<K, V>> fieldSpec;
23: protected final UnitOfWork persistenceContext;
24:
25: private Map<K, V> value;
26:
27: public LazyLoadingMapProxy(T owner, FieldSpecification<? super T, Map<K, V>> fieldSpec,
28: UnitOfWork persistenceContext) {
29: this.owner = owner;
30: this.fieldSpec = fieldSpec;
31: this.persistenceContext = persistenceContext;
32: }
33:
34: @Override
35: public Map<K, V> triggerLazyLoading() {
36: if (value != null) {
37: return value;
38: }
39: if (persistenceContext == null || !persistenceContext.isActive()) {
40: throw new LazyLoadingException("No active persistence context is available in lazy loading proxy for attribute "
41: + fieldSpec + " of entity " + owner);
42: }
43: this.value = (Map<K, V>) persistenceContext.loadEntityField(owner, fieldSpec);
44: return value;
45: }
46:
47: @Override
48: public boolean isLoaded() {
49: return value != null;
50: }
51:
52: @Override
53: public Map<K, V> getLoadedValue() {
54: if (value == null) {
55: throw new IllegalStateException("Proxy has not been loaded, yet.");
56: }
57: return value;
58: }
59:
60: @Override
61: public Map<K, V> unwrap() {
62: return (Map<K, V>) CollectionFactory.createDefaultMap();
63: }
64:
65: @Override
66: public int size() {
67: return triggerLazyLoading().size();
68: }
69:
70: @Override
71: public boolean isEmpty() {
72: return triggerLazyLoading().isEmpty();
73: }
74:
75: @Override
76: public boolean containsKey(Object key) {
77: return triggerLazyLoading().containsKey(key);
78: }
79:
80: @Override
81: public boolean containsValue(Object value) {
82: return triggerLazyLoading().containsValue(value);
83: }
84:
85: @Override
86: public V get(Object key) {
87: return triggerLazyLoading().get(key);
88: }
89:
90: @Override
91: public V put(K key, V value) {
92: return triggerLazyLoading().put(key, value);
93: }
94:
95: @Override
96: public V remove(Object key) {
97: return triggerLazyLoading().remove(key);
98: }
99:
100: @Override
101: public void putAll(Map<? extends K, ? extends V> m) {
102: triggerLazyLoading().putAll(m);
103: }
104:
105: @Override
106: public void clear() {
107: triggerLazyLoading().clear();
108: }
109:
110: @Override
111: public Set<K> keySet() {
112: return triggerLazyLoading().keySet();
113: }
114:
115: @Override
116: public Collection<V> values() {
117: return triggerLazyLoading().values();
118: }
119:
120: @Override
121: public Set<Entry<K, V>> entrySet() {
122: return triggerLazyLoading().entrySet();
123: }
124:
125: @Override
126: public String toString() {
127: return getClass().getSimpleName() + "[" + owner.getClass().getSimpleName() + "." + fieldSpec.getName() + "]";
128: }
129: }