Skip to content

Package: ChangeManagerImpl

ChangeManagerImpl

nameinstructionbranchcomplexitylinemethod
ChangeManagerImpl(MetamodelProvider)
M: 0 C: 17
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: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
calculateChangesInternal(ObjectChangeSet)
M: 0 C: 60
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 16
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: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
hasChangesInternal(Object, Object)
M: 4 C: 53
93%
M: 4 C: 10
71%
M: 4 C: 4
50%
M: 2 C: 14
88%
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 org.slf4j.Logger;
24: import org.slf4j.LoggerFactory;
25:
26: import java.lang.reflect.Field;
27: import java.util.IdentityHashMap;
28: import java.util.Map;
29: import java.util.Objects;
30: import java.util.Set;
31:
32: public class ChangeManagerImpl implements ChangeManager {
33:
34: private static final Logger LOG = LoggerFactory.getLogger(ChangeManagerImpl.class);
35:
36: private final Map<Object, Object> visitedObjects;
37:
38: private final MetamodelProvider metamodelProvider;
39: private final ChangeDetector changeDetector;
40:
41: public ChangeManagerImpl(MetamodelProvider metamodelProvider) {
42: this.metamodelProvider = metamodelProvider;
43: this.changeDetector = new ChangeDetectors(metamodelProvider);
44: visitedObjects = new IdentityHashMap<>();
45: }
46:
47: @Override
48: public boolean hasChanges(Object original, Object clone) {
49: boolean res = hasChangesInternal(original, clone);
50: visitedObjects.clear();
51: return res;
52: }
53:
54: /**
55: * This method does the actual check for changes. It is wrapped in the public method since the IdentityMap for
56: * visited objects has to be cleared after the whole check is done.
57: *
58: * @param original The original object.
59: * @param clone The clone that may have changed.
60: * @return True if the clone is in different state than the original.
61: */
62: boolean hasChangesInternal(Object original, Object clone) {
63:• if (clone == null && original == null) {
64: return false;
65: }
66:• if (clone == null || original == null) {
67: return true;
68: }
69:• if (visitedObjects.containsKey(clone)) {
70: return false;
71: }
72: final Class<?> cls = clone.getClass();
73:• for (FieldSpecification<?, ?> fs : getFields(cls)) {
74: final Field f = fs.getJavaField();
75: final Object clVal = EntityPropertiesUtils.getFieldValue(f, clone);
76: final Object origVal = EntityPropertiesUtils.getFieldValue(f, original);
77: final boolean valueChanged = valueChanged(origVal, clVal);
78:• if (valueChanged) {
79: return true;
80: }
81: }
82: return false;
83: }
84:
85: private <X> Set<FieldSpecification<? super X, ?>> getFields(Class<X> cls) {
86: return metamodelProvider.getMetamodel().entity(cls).getFieldSpecifications();
87: }
88:
89: private boolean valueChanged(Object orig, Object clone) {
90: return changeDetector.hasChanges(clone, orig);
91: }
92:
93: @Override
94: public boolean calculateChanges(ObjectChangeSet changeSet) throws OWLInferredAttributeModifiedException {
95: return calculateChangesInternal(Objects.requireNonNull(changeSet));
96: }
97:
98: /**
99: * This internal method does the actual changes calculation. It compares every non-static attribute of the clone to
100: * the original value. If the values are different, a change record is added into the change set.
101: *
102: * @param changeSet The change set where change records will be put in. It also contains reference to the clone and
103: * original object.
104: */
105: private boolean calculateChangesInternal(ObjectChangeSet changeSet) {
106: LOG.trace("Calculating changes for change set {}.", changeSet);
107: Object original = changeSet.getChangedObject();
108: Object clone = changeSet.getCloneObject();
109: boolean changes = false;
110:• for (FieldSpecification<?, ?> fs : getFields(clone.getClass())) {
111: final Field f = fs.getJavaField();
112: Object clVal = EntityPropertiesUtils.getFieldValue(f, clone);
113: Object origVal = EntityPropertiesUtils.getFieldValue(f, original);
114:• if (clVal == null && origVal == null) {
115: continue;
116: }
117: boolean changed = valueChanged(origVal, clVal);
118:• if (changed) {
119: changeSet.addChangeRecord(new ChangeRecordImpl(fs, clVal));
120: changes = true;
121: }
122: }
123: return changes;
124: }
125: }