Skip to content

Method: hasChanges()

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.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
19: import cz.cvut.kbss.jopa.sessions.ChangeRecord;
20: import cz.cvut.kbss.jopa.sessions.ObjectChangeSet;
21:
22: import java.net.URI;
23: import java.util.*;
24:
25: public class ObjectChangeSetImpl implements ObjectChangeSet {
26:
27: // The object the changes are bound to
28: private final Object changedObject;
29:
30: // Reference to the clone
31: private final Object cloneObject;
32:
33: // A map of attributeName-ChangeRecord pairs to easily find the attributes to change
34: private final Map<FieldSpecification<?, ?>, ChangeRecord> attributesToChange = new HashMap<>();
35:
36: // Does this change set represent a new object
37: private boolean isNew;
38:
39: private final Descriptor descriptor;
40:
41: public ObjectChangeSetImpl(Object changedObject, Object cloneObject, Descriptor descriptor) {
42: this.changedObject = Objects.requireNonNull(changedObject);
43: this.cloneObject = Objects.requireNonNull(cloneObject);
44: this.descriptor = Objects.requireNonNull(descriptor);
45: }
46:
47: @Override
48: public void addChangeRecord(ChangeRecord record) {
49: Objects.requireNonNull(record);
50: attributesToChange.put(record.getAttribute(), record);
51: }
52:
53: @Override
54: public Set<ChangeRecord> getChanges() {
55: return new HashSet<>(attributesToChange.values());
56: }
57:
58: @Override
59: public boolean hasChanges() {
60:• return !attributesToChange.isEmpty();
61: }
62:
63: @Override
64: public Class<?> getObjectClass() {
65: return cloneObject.getClass();
66: }
67:
68: @Override
69: public Object getChangedObject() {
70: return changedObject;
71: }
72:
73: @Override
74: public Object getCloneObject() {
75: return cloneObject;
76: }
77:
78: @Override
79: public void setNew(boolean isNew) {
80: this.isNew = isNew;
81: }
82:
83: @Override
84: public boolean isNew() {
85: return isNew;
86: }
87:
88: @Override
89: public URI getEntityContext() {
90: return descriptor.getSingleContext().orElse(null);
91: }
92:
93: @Override
94: public Descriptor getEntityDescriptor() {
95: return descriptor;
96: }
97: }