Skip to content

Package: InstanceDescriptor

InstanceDescriptor

nameinstructionbranchcomplexitylinemethod
InstanceDescriptor(Object, EntityType)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
InstanceDescriptor(Object, InstanceDescriptor)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getInstance()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isLoaded()
M: 0 C: 40
100%
M: 0 C: 10
100%
M: 0 C: 6
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
isLoaded(FieldSpecification)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
mapInstanceAttributes(EntityType)
M: 0 C: 29
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
setLoaded(FieldSpecification, LoadState)
M: 4 C: 22
85%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 5
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toString()
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.sessions.descriptor;
16:
17: import cz.cvut.kbss.jopa.model.LoadState;
18: import cz.cvut.kbss.jopa.model.annotations.FetchType;
19: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
20: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
21:
22: import java.util.HashMap;
23: import java.util.Map;
24: import java.util.Objects;
25:
26: /**
27: * Describes an instance managed by a persistence context.
28: */
29: public class InstanceDescriptor<T> {
30:
31: private final T instance;
32:
33: private final Map<FieldSpecification<? super T, ?>, LoadState> loadState;
34:
35: InstanceDescriptor(T instance, EntityType<T> et) {
36: this.instance = Objects.requireNonNull(instance);
37: this.loadState = mapInstanceAttributes(et);
38: }
39:
40: InstanceDescriptor(T instance, InstanceDescriptor<T> other) {
41: this.instance = Objects.requireNonNull(instance);
42: this.loadState = new HashMap<>(other.loadState);
43: }
44:
45: private Map<FieldSpecification<? super T, ?>, LoadState> mapInstanceAttributes(EntityType<T> et) {
46: final Map<FieldSpecification<? super T, ?>, LoadState> map = new HashMap<>();
47:• for (FieldSpecification<? super T, ?> fs : et.getFieldSpecifications()) {
48: map.put(fs, LoadState.NOT_LOADED);
49: }
50: map.put(et.getIdentifier(), LoadState.LOADED);
51: return map;
52: }
53:
54: public LoadState isLoaded() {
55: boolean unknownFound = false;
56:• for (Map.Entry<FieldSpecification<? super T, ?>, LoadState> e : loadState.entrySet()) {
57:• if (e.getKey().getFetchType() == FetchType.LAZY) {
58: continue;
59: }
60:• if (e.getValue() == LoadState.NOT_LOADED) {
61: return LoadState.NOT_LOADED;
62:• } else if (e.getValue() == LoadState.UNKNOWN) {
63: unknownFound = true;
64: }
65: }
66:• return unknownFound ? LoadState.UNKNOWN : LoadState.LOADED;
67: }
68:
69: public LoadState isLoaded(FieldSpecification<?, ?> attribute) {
70: return loadState.getOrDefault(Objects.requireNonNull(attribute), LoadState.UNKNOWN);
71: }
72:
73: public Object getInstance() {
74: return instance;
75: }
76:
77: public void setLoaded(FieldSpecification<? super T, ?> fs, LoadState state) {
78: Objects.requireNonNull(fs);
79: Objects.requireNonNull(state);
80:• assert fs.getDeclaringType().getJavaType().isAssignableFrom(instance.getClass());
81:
82: loadState.put(fs, state);
83: }
84:
85: @Override
86: public String toString() {
87: return "InstanceDescriptor{" +
88: "instance=" + instance +
89: ", loadState=" + loadState +
90: '}';
91: }
92: }