Skip to content

Method: removeObsoleteNodes(AbstractListIterator)

1: /**
2: * Copyright (C) 2019 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.ontodriver.jena.list;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.ListDescriptor;
18: import cz.cvut.kbss.ontodriver.descriptor.ListValueDescriptor;
19: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
20: import cz.cvut.kbss.ontodriver.model.Axiom;
21: import cz.cvut.kbss.ontodriver.model.NamedResource;
22: import org.apache.jena.rdf.model.Resource;
23:
24: import java.util.ArrayList;
25: import java.util.List;
26:
27: import static org.apache.jena.rdf.model.ResourceFactory.createResource;
28:
29: public abstract class ListHandler<D extends ListDescriptor, V extends ListValueDescriptor> {
30:
31: final StorageConnector connector;
32:
33: ListHandler(StorageConnector connector) {
34: this.connector = connector;
35: }
36:
37: List<Axiom<NamedResource>> loadList(D descriptor) {
38: final List<Axiom<NamedResource>> result = new ArrayList<>();
39: final AbstractListIterator it = iterator(descriptor);
40: while (it.hasNext()) {
41: result.add(it.nextAxiom());
42: }
43: return result;
44: }
45:
46: abstract AbstractListIterator iterator(D descriptor);
47:
48: abstract AbstractListIterator iterator(V descriptor);
49:
50: void persistList(V descriptor) {
51: final List<NamedResource> values = descriptor.getValues();
52: if (values.isEmpty()) {
53: return;
54: }
55: Resource owner = createResource(descriptor.getListOwner().getIdentifier().toString());
56: appendNewNodes(descriptor, 0, owner);
57: }
58:
59: abstract void appendNewNodes(V descriptor, int index, Resource lastNode);
60:
61: void updateList(V descriptor) {
62: final AbstractListIterator it = iterator(descriptor);
63: int i = 0;
64: while (it.hasNext() && i < descriptor.getValues().size()) {
65: final NamedResource update = descriptor.getValues().get(i);
66: final NamedResource existing = it.nextValue();
67: if (!existing.equals(update)) {
68: it.replace(createResource(update.getIdentifier().toString()));
69: }
70: i++;
71: }
72: removeObsoleteNodes(it);
73: if (i < descriptor.getValues().size()) {
74: appendNewNodes(descriptor, i, it.getCurrentNode());
75: }
76: }
77:
78: private static void removeObsoleteNodes(AbstractListIterator it) {
79:• while (it.hasNext()) {
80: it.nextValue();
81: it.removeWithoutReconnect();
82: }
83: }
84:
85: public static SimpleListHandler simpleListHandler(StorageConnector connector) {
86: return new SimpleListHandler(connector);
87: }
88:
89: public static ReferencedListHandler referencedListHandler(StorageConnector connector) {
90: return new ReferencedListHandler(connector);
91: }
92: }