Skip to content

Package: LazyLoadingMapProxy

LazyLoadingMapProxy

nameinstructionbranchcomplexitylinemethod
LazyLoadingMapProxy(Object, FieldSpecification, UnitOfWork)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
clear()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
containsKey(Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
containsValue(Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
entrySet()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
get(Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getLoadedValue()
M: 0 C: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
isEmpty()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isLoaded()
M: 1 C: 6
86%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 1
100%
M: 0 C: 1
100%
keySet()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
put(Object, Object)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
putAll(Map)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
remove(Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
size()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toString()
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
triggerLazyLoading()
M: 0 C: 35
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
unwrap()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
values()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.proxy.lazy;
19:
20: import cz.cvut.kbss.jopa.exception.LazyLoadingException;
21: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
22: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
23: import cz.cvut.kbss.jopa.utils.CollectionFactory;
24:
25: import java.util.Collection;
26: import java.util.Map;
27: import java.util.Set;
28:
29: /**
30: * {@link Map} proxy that triggers lazy loading when its contents is accessed.
31: *
32: * @param <T> Type of the object whose attribute value is this map proxy
33: * @param <K> Map key type
34: * @param <V> Map value type
35: */
36: public class LazyLoadingMapProxy<T, K, V> implements LazyLoadingProxy<Map<K, V>>, Map<K, V> {
37:
38: protected final T owner;
39: protected final FieldSpecification<? super T, Map<K, V>> fieldSpec;
40: protected final UnitOfWork persistenceContext;
41:
42: private Map<K, V> value;
43:
44: public LazyLoadingMapProxy(T owner, FieldSpecification<? super T, Map<K, V>> fieldSpec,
45: UnitOfWork persistenceContext) {
46: this.owner = owner;
47: this.fieldSpec = fieldSpec;
48: this.persistenceContext = persistenceContext;
49: }
50:
51: @Override
52: public Map<K, V> triggerLazyLoading() {
53:• if (value != null) {
54: return value;
55: }
56:• if (persistenceContext == null || !persistenceContext.isActive()) {
57: throw new LazyLoadingException("No active persistence context is available in lazy loading proxy for attribute "
58: + fieldSpec + " of entity " + owner);
59: }
60: this.value = (Map<K, V>) persistenceContext.loadEntityField(owner, fieldSpec);
61: return value;
62: }
63:
64: @Override
65: public boolean isLoaded() {
66:• return value != null;
67: }
68:
69: @Override
70: public Map<K, V> getLoadedValue() {
71:• if (value == null) {
72: throw new IllegalStateException("Proxy has not been loaded, yet.");
73: }
74: return value;
75: }
76:
77: @Override
78: public Map<K, V> unwrap() {
79: return (Map<K, V>) CollectionFactory.createDefaultMap();
80: }
81:
82: @Override
83: public int size() {
84: return triggerLazyLoading().size();
85: }
86:
87: @Override
88: public boolean isEmpty() {
89: return triggerLazyLoading().isEmpty();
90: }
91:
92: @Override
93: public boolean containsKey(Object key) {
94: return triggerLazyLoading().containsKey(key);
95: }
96:
97: @Override
98: public boolean containsValue(Object value) {
99: return triggerLazyLoading().containsValue(value);
100: }
101:
102: @Override
103: public V get(Object key) {
104: return triggerLazyLoading().get(key);
105: }
106:
107: @Override
108: public V put(K key, V value) {
109: return triggerLazyLoading().put(key, value);
110: }
111:
112: @Override
113: public V remove(Object key) {
114: return triggerLazyLoading().remove(key);
115: }
116:
117: @Override
118: public void putAll(Map<? extends K, ? extends V> m) {
119: triggerLazyLoading().putAll(m);
120: }
121:
122: @Override
123: public void clear() {
124: triggerLazyLoading().clear();
125: }
126:
127: @Override
128: public Set<K> keySet() {
129: return triggerLazyLoading().keySet();
130: }
131:
132: @Override
133: public Collection<V> values() {
134: return triggerLazyLoading().values();
135: }
136:
137: @Override
138: public Set<Entry<K, V>> entrySet() {
139: return triggerLazyLoading().entrySet();
140: }
141:
142: @Override
143: public String toString() {
144: return getClass().getSimpleName() + "[" + owner.getClass().getSimpleName() + "." + fieldSpec.getName() + "]";
145: }
146: }