Package: ObjectChangeSet
ObjectChangeSet
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ObjectChangeSet(Object, Object, Descriptor) |
|
|
|
|
|
||||||||||||||||||||
addChangeRecord(ChangeRecord) |
|
|
|
|
|
||||||||||||||||||||
getChanges() |
|
|
|
|
|
||||||||||||||||||||
getClone() |
|
|
|
|
|
||||||||||||||||||||
getDescriptor() |
|
|
|
|
|
||||||||||||||||||||
getOriginal() |
|
|
|
|
|
||||||||||||||||||||
hasChanges() |
|
|
|
|
|
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:
23: import java.util.*;
24:
25: public class ObjectChangeSet implements Change {
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: private final Descriptor descriptor;
37:
38: public ObjectChangeSet(Object changedObject, Object cloneObject, Descriptor descriptor) {
39: this.changedObject = Objects.requireNonNull(changedObject);
40: this.cloneObject = Objects.requireNonNull(cloneObject);
41: this.descriptor = Objects.requireNonNull(descriptor);
42: }
43:
44: /**
45: * Adds a new change record to this change set.
46: * <p>
47: * If there was a change for attribute represented by the new record, it will be overwritten.
48: *
49: * @param record The record to add
50: */
51: public void addChangeRecord(ChangeRecord record) {
52: Objects.requireNonNull(record);
53: attributesToChange.put(record.getAttribute(), record);
54: }
55:
56: /**
57: * Gets changes held in this change set.
58: *
59: * @return Set of changes
60: */
61: public Set<ChangeRecord> getChanges() {
62: return new HashSet<>(attributesToChange.values());
63: }
64:
65: /**
66: * Whether this change set contains any changes.
67: *
68: * @return {@code true} if there are any changes in this change set, {@code false} otherwise
69: */
70: public boolean hasChanges() {
71:• return !attributesToChange.isEmpty();
72: }
73:
74: @Override
75: public Object getOriginal() {
76: return changedObject;
77: }
78:
79: @Override
80: public Object getClone() {
81: return cloneObject;
82: }
83:
84: @Override
85: public Descriptor getDescriptor() {
86: return descriptor;
87: }
88: }