Skip to content

Method: toString()

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.sessions.descriptor;
19:
20: import cz.cvut.kbss.jopa.model.LoadState;
21: import cz.cvut.kbss.jopa.model.annotations.FetchType;
22: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
23: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
24:
25: import java.util.HashMap;
26: import java.util.Map;
27: import java.util.Objects;
28:
29: /**
30: * Describes load state of an instance in a persistence context.
31: */
32: public class LoadStateDescriptor<T> {
33:
34: private final T instance;
35:
36: private final Map<FieldSpecification<? super T, ?>, LoadState> loadState;
37:
38: public LoadStateDescriptor(T instance, EntityType<T> et, LoadState defaultState) {
39: this.instance = Objects.requireNonNull(instance);
40: this.loadState = mapInstanceAttributes(et, defaultState);
41: }
42:
43: public LoadStateDescriptor(T instance, LoadStateDescriptor<T> other) {
44: this.instance = Objects.requireNonNull(instance);
45: this.loadState = new HashMap<>(other.loadState);
46: }
47:
48: private Map<FieldSpecification<? super T, ?>, LoadState> mapInstanceAttributes(EntityType<T> et, LoadState defaultState) {
49: final Map<FieldSpecification<? super T, ?>, LoadState> map = new HashMap<>();
50: for (FieldSpecification<? super T, ?> fs : et.getFieldSpecifications()) {
51: map.put(fs, defaultState);
52: }
53: map.put(et.getIdentifier(), LoadState.LOADED);
54: return map;
55: }
56:
57: public LoadState isLoaded() {
58: boolean unknownFound = false;
59: for (Map.Entry<FieldSpecification<? super T, ?>, LoadState> e : loadState.entrySet()) {
60: if (e.getKey().getFetchType() == FetchType.LAZY) {
61: continue;
62: }
63: if (e.getValue() == LoadState.NOT_LOADED) {
64: return LoadState.NOT_LOADED;
65: } else if (e.getValue() == LoadState.UNKNOWN) {
66: unknownFound = true;
67: }
68: }
69: return unknownFound ? LoadState.UNKNOWN : LoadState.LOADED;
70: }
71:
72: public LoadState isLoaded(FieldSpecification<?, ?> attribute) {
73: return loadState.getOrDefault(Objects.requireNonNull(attribute), LoadState.UNKNOWN);
74: }
75:
76: public Object getInstance() {
77: return instance;
78: }
79:
80: public void setLoaded(FieldSpecification<? super T, ?> fs, LoadState state) {
81: Objects.requireNonNull(fs);
82: Objects.requireNonNull(state);
83: assert fs.getDeclaringType().getJavaType().isAssignableFrom(instance.getClass());
84:
85: loadState.put(fs, state);
86: }
87:
88: @Override
89: public String toString() {
90: return "LoadStateDescriptor{" +
91: "instance=" + instance +
92: ", loadState=" + loadState +
93: '}';
94: }
95: }