Skip to content

Method: hasChanges(Object, Object)

1: /*
2: * JOPA
3: * Copyright (C) 2024 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.sessions.MetamodelProvider;
21:
22: import java.util.Collection;
23: import java.util.Map;
24:
25: class ChangeDetectors implements ChangeDetector {
26:
27: private final MetamodelProvider metamodelProvider;
28:
29: private final ChangeDetector mapChangeDetector;
30: private final ChangeDetector collectionChangeDetector;
31: private final ChangeDetector managedTypeDetector;
32:
33: ChangeDetectors(MetamodelProvider metamodelProvider) {
34: this.metamodelProvider = metamodelProvider;
35: this.mapChangeDetector = new MapChangeDetector(this);
36: this.collectionChangeDetector = new CollectionChangeDetector(this, metamodelProvider);
37: this.managedTypeDetector = new ManagedTypeChangeDetector(metamodelProvider);
38: }
39:
40: @Override
41: public boolean hasChanges(Object clone, Object original) {
42:• if ((clone == null && original != null) || (clone != null && original == null)) {
43:• return isNonEmptyCollection(clone) && isNonEmptyCollection(original);
44: }
45:• if (clone == null) {
46: return false;
47: }
48:
49:• if (metamodelProvider.isEntityType(clone.getClass())) {
50: return managedTypeDetector.hasChanges(clone, original);
51:• } else if (clone instanceof Collection) {
52: return collectionChangeDetector.hasChanges(clone, original);
53:• } else if (clone instanceof Map) {
54: return mapChangeDetector.hasChanges(clone, original);
55: }
56:• return !clone.equals(original);
57: }
58:
59: static boolean isNonEmptyCollection(Object instance) {
60: return !(instance instanceof Collection) || !((Collection<?>) instance).isEmpty();
61: }
62: }