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