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