Skip to content

Package: LoadStateDescriptorFactory

LoadStateDescriptorFactory

nameinstructionbranchcomplexitylinemethod
LoadStateDescriptorFactory()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
create(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%
createAllLoaded(Object, EntityType)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createAllUnknown(Object, EntityType)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createCopy(Object, LoadStateDescriptor)
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%
createNotLoaded(Object, EntityType)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getAttributeLoadState(Object, FieldSpecification)
M: 0 C: 21
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
lambda$create$0(LoadStateDescriptor, Object, FieldSpecification)
M: 0 C: 7
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.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: import cz.cvut.kbss.jopa.proxy.lazy.LazyLoadingProxy;
25: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
26:
27: /**
28: * Builds {@link LoadStateDescriptor}s on various occasions.
29: */
30: public class LoadStateDescriptorFactory {
31:
32: private LoadStateDescriptorFactory() {
33: throw new AssertionError();
34: }
35:
36: /**
37: * Creates an instance descriptor which marks all attributes except the identifier as not loaded.
38: *
39: * @param instance Instance to create descriptor for
40: * @param et Entity type of the instance
41: * @param <T> Instance type
42: * @return Fresh instance descriptor
43: */
44: public static <T> LoadStateDescriptor<T> createNotLoaded(T instance, EntityType<T> et) {
45: return new LoadStateDescriptor<>(instance, et, LoadState.NOT_LOADED);
46: }
47:
48: /**
49: * Creates an instance descriptor which marks all attributes as loaded.
50: *
51: * @param instance Instance to create descriptor for
52: * @param et Entity type of the instance
53: * @param <T> Instance type
54: * @return Fresh instance descriptor with all loaded
55: */
56: public static <T> LoadStateDescriptor<T> createAllLoaded(T instance, EntityType<T> et) {
57: return new LoadStateDescriptor<>(instance, et, LoadState.LOADED);
58: }
59:
60: /**
61: * Creates an instance descriptor which marks all attributes except the identifier as having an unknown load state.
62: *
63: * @param instance Instance to create descriptor for
64: * @param et Entity type of the instance
65: * @param <T> Instance type
66: * @return Fresh instance descriptor
67: */
68: public static <T> LoadStateDescriptor<T> createAllUnknown(T instance, EntityType<T> et) {
69: return new LoadStateDescriptor<>(instance, et, LoadState.UNKNOWN);
70: }
71:
72: /**
73: * Creates an instance descriptor which sets load status of attributes based on their value in the specified
74: * instance as follows:
75: * <p>
76: * If the attribute value is not {@code null}, its status is set to {@link LoadState#LOADED}. If the value is
77: * {@code null} and the attribute fetch type is {@link FetchType#EAGER}, the status is also set to {@code LOADED}.
78: * Otherwise, the status is set to {@link LoadState#UNKNOWN}.
79: *
80: * @param instance Instance to create descriptor for
81: * @param et Entity type of the instance
82: * @param <T> Instance type
83: * @return Fresh instance descriptor
84: */
85: public static <T> LoadStateDescriptor<T> create(T instance, EntityType<T> et) {
86: final LoadStateDescriptor<T> descriptor = createNotLoaded(instance, et);
87: et.getFieldSpecifications()
88: .forEach(fs -> descriptor.setLoaded(fs, getAttributeLoadState(instance, fs)));
89: return descriptor;
90: }
91:
92: private static <T> LoadState getAttributeLoadState(T instance, FieldSpecification<? super T, ?> fs) {
93:• if (fs.getFetchType() == FetchType.EAGER) {
94: return LoadState.LOADED;
95: }
96: final Object attValue = EntityPropertiesUtils.getAttributeValue(fs, instance);
97:• if (attValue instanceof LazyLoadingProxy<?>) {
98: return LoadState.NOT_LOADED;
99: }
100:• return attValue != null ? LoadState.LOADED : LoadState.UNKNOWN;
101: }
102:
103: /**
104: * Copies the load states from the specified original descriptor into a new descriptor for the specified instance.
105: *
106: * @param instance Instance to create descriptor for
107: * @param original Load state to copy
108: * @param <T> Instance type
109: * @return Fresh instance descriptor with state copied from the specified one
110: */
111: public static <T> LoadStateDescriptor<T> createCopy(T instance, LoadStateDescriptor<T> original) {
112: return new LoadStateDescriptor<>(instance, original);
113: }
114: }