Skip to content

Package: ChangeManagerImpl

ChangeManagerImpl

nameinstructionbranchcomplexitylinemethod
ChangeManagerImpl(MetamodelProvider)
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
calculateChanges(ObjectChangeSet)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
calculateChangesInternal(ObjectChangeSet)
M: 0 C: 82
100%
M: 0 C: 11
100%
M: 0 C: 7
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
getFields(Class)
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%
hasChanges(Object, Object)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
hasChangesInternal(Object, Object)
M: 4 C: 92
96%
M: 4 C: 15
79%
M: 4 C: 7
64%
M: 2 C: 22
92%
M: 0 C: 1
100%
static {...}
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%
valueChanged(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%

Coverage

1: /**
2: * Copyright (C) 2016 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.change;
16:
17: import cz.cvut.kbss.jopa.exceptions.OWLInferredAttributeModifiedException;
18: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
19: import cz.cvut.kbss.jopa.sessions.ChangeManager;
20: import cz.cvut.kbss.jopa.sessions.MetamodelProvider;
21: import cz.cvut.kbss.jopa.sessions.ObjectChangeSet;
22: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
23: import cz.cvut.kbss.jopa.utils.ErrorUtils;
24: import org.slf4j.Logger;
25: import org.slf4j.LoggerFactory;
26:
27: import java.lang.reflect.Field;
28: import java.util.*;
29:
30: public class ChangeManagerImpl implements ChangeManager {
31:
32: private static final Logger LOG = LoggerFactory.getLogger(ChangeManagerImpl.class);
33:
34: private final Map<Object, Object> visitedObjects;
35:
36: private final MetamodelProvider metamodelProvider;
37: private final ChangeDetector changeDetector;
38:
39: public ChangeManagerImpl(MetamodelProvider metamodelProvider) {
40: this.metamodelProvider = metamodelProvider;
41: this.changeDetector = new ChangeDetectors(metamodelProvider, this);
42: visitedObjects = new IdentityHashMap<>();
43: }
44:
45: public boolean hasChanges(Object original, Object clone) {
46: LOG.trace("Checking for changes...");
47: boolean res = hasChangesInternal(original, clone);
48: visitedObjects.clear();
49: return res;
50: }
51:
52: /**
53: * This method does the actual check for changes. It is wrapped in the public method since the IdentityMap for
54: * visited objects has to be cleared after the whole check is done.
55: *
56: * @param original The original object.
57: * @param clone The clone that may have changed.
58: * @return True if the clone is in different state than the original.
59: */
60: boolean hasChangesInternal(Object original, Object clone) {
61:• if (clone == null && original == null) {
62: return false;
63: }
64:• if (clone == null || original == null) {
65: return true;
66: }
67:• if (visitedObjects.containsKey(clone)) {
68: return false;
69: }
70: final Class<?> cls = clone.getClass();
71: Map<Object, Object> composedObjects = new HashMap<>();
72:• for (FieldSpecification<?, ?> fs : getFields(cls)) {
73: final Field f = fs.getJavaField();
74: Object clVal = EntityPropertiesUtils.getFieldValue(f, clone);
75: Object origVal = EntityPropertiesUtils.getFieldValue(f, original);
76: final Changed ch = valueChanged(origVal, clVal);
77:• switch (ch) {
78: case TRUE:
79: return true;
80: case UNDETERMINED:
81: visitedObjects.put(clVal, clVal);
82: composedObjects.put(clVal, origVal);
83: break;
84: default:
85: break;
86: }
87: }
88: // First check all primitive values - performance, then do composed
89:• for (Object cl : composedObjects.keySet()) {
90:• if (hasChangesInternal(cl, composedObjects.get(cl))) {
91: return true;
92: }
93: }
94: return false;
95: }
96:
97: private <X> Set<FieldSpecification<? super X, ?>> getFields(Class<X> cls) {
98: return metamodelProvider.getMetamodel().entity(cls).getFieldSpecifications();
99: }
100:
101: private Changed valueChanged(Object orig, Object clone) {
102: return changeDetector.hasChanges(clone, orig);
103: }
104:
105: public boolean calculateChanges(ObjectChangeSet changeSet) throws IllegalAccessException,
106: IllegalArgumentException,
107: OWLInferredAttributeModifiedException {
108: Objects.requireNonNull(changeSet, ErrorUtils.constructNPXMessage("changeSet"));
109:
110: return calculateChangesInternal(changeSet);
111: }
112:
113: /**
114: * This internal method does the actual changes calculation. It compares every non-static attribute of the clone to
115: * the original value. If the values are different, a change record is added into the change set.
116: *
117: * @param changeSet The change set where change records will be put in. It also contains reference to the clone and
118: * original object.
119: * @throws IllegalArgumentException
120: * @throws IllegalAccessException
121: * @throws OWLInferredAttributeModifiedException
122: */
123: private boolean calculateChangesInternal(ObjectChangeSet changeSet)
124: throws IllegalArgumentException, IllegalAccessException {
125: LOG.trace("Calculating changes for change set {}.", changeSet);
126: Object original = changeSet.getChangedObject();
127: Object clone = changeSet.getCloneObject();
128: boolean changes = false;
129:• for (FieldSpecification<?, ?> fs : getFields(clone.getClass())) {
130: final Field f = fs.getJavaField();
131: Object clVal = EntityPropertiesUtils.getFieldValue(f, clone);
132: Object origVal = EntityPropertiesUtils.getFieldValue(f, original);
133:• if (clVal == null && origVal == null) {
134: continue;
135: }
136: final String attName = f.getName();
137: Changed changed = valueChanged(origVal, clVal);
138:• switch (changed) {
139: case TRUE:
140: changeSet.addChangeRecord(new ChangeRecordImpl(attName, clVal));
141: changes = true;
142: break;
143: case UNDETERMINED:
144:• if (hasChanges(origVal, clVal)) {
145: changeSet.addChangeRecord(new ChangeRecordImpl(attName, clVal));
146: changes = true;
147: }
148: break;
149: default:
150: break;
151: }
152: }
153: return changes;
154: }
155: }