Skip to contentMethod: getInstance()
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: }