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