Package: ObjectChangeSetImpl
ObjectChangeSetImpl
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ObjectChangeSetImpl(Object, Object, Descriptor) |
|
|
|
|
|
||||||||||||||||||||
addChangeRecord(ChangeRecord) |
|
|
|
|
|
||||||||||||||||||||
getChangedObject() |
|
|
|
|
|
||||||||||||||||||||
getChanges() |
|
|
|
|
|
||||||||||||||||||||
getCloneObject() |
|
|
|
|
|
||||||||||||||||||||
getEntityContext() |
|
|
|
|
|
||||||||||||||||||||
getEntityDescriptor() |
|
|
|
|
|
||||||||||||||||||||
getObjectClass() |
|
|
|
|
|
||||||||||||||||||||
hasChanges() |
|
|
|
|
|
||||||||||||||||||||
isNew() |
|
|
|
|
|
||||||||||||||||||||
setNew(boolean) |
|
|
|
|
|
Coverage
1: /*
2: * JOPA
3: * Copyright (C) 2023 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.sessions.change;
19:
20: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
21: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
22: import cz.cvut.kbss.jopa.sessions.ChangeRecord;
23: import cz.cvut.kbss.jopa.sessions.ObjectChangeSet;
24:
25: import java.net.URI;
26: import java.util.*;
27:
28: public class ObjectChangeSetImpl implements ObjectChangeSet {
29:
30: // The object the changes are bound to
31: private final Object changedObject;
32:
33: // Reference to the clone
34: private final Object cloneObject;
35:
36: // A map of attributeName-ChangeRecord pairs to easily find the attributes to change
37: private final Map<FieldSpecification<?, ?>, ChangeRecord> attributesToChange = new HashMap<>();
38:
39: // Does this change set represent a new object
40: private boolean isNew;
41:
42: private final Descriptor descriptor;
43:
44: public ObjectChangeSetImpl(Object changedObject, Object cloneObject, Descriptor descriptor) {
45: this.changedObject = Objects.requireNonNull(changedObject);
46: this.cloneObject = Objects.requireNonNull(cloneObject);
47: this.descriptor = Objects.requireNonNull(descriptor);
48: }
49:
50: @Override
51: public void addChangeRecord(ChangeRecord record) {
52: Objects.requireNonNull(record);
53: attributesToChange.put(record.getAttribute(), record);
54: }
55:
56: @Override
57: public Set<ChangeRecord> getChanges() {
58: return new HashSet<>(attributesToChange.values());
59: }
60:
61: @Override
62: public boolean hasChanges() {
63:• return !attributesToChange.isEmpty();
64: }
65:
66: @Override
67: public Class<?> getObjectClass() {
68: return cloneObject.getClass();
69: }
70:
71: @Override
72: public Object getChangedObject() {
73: return changedObject;
74: }
75:
76: @Override
77: public Object getCloneObject() {
78: return cloneObject;
79: }
80:
81: @Override
82: public void setNew(boolean isNew) {
83: this.isNew = isNew;
84: }
85:
86: @Override
87: public boolean isNew() {
88: return isNew;
89: }
90:
91: @Override
92: public URI getEntityContext() {
93: return descriptor.getSingleContext().orElse(null);
94: }
95:
96: @Override
97: public Descriptor getEntityDescriptor() {
98: return descriptor;
99: }
100: }