-
Notifications
You must be signed in to change notification settings - Fork 14
/
AbstractListIterator.java
132 lines (111 loc) · 4.57 KB
/
AbstractListIterator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Copyright (C) 2019 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.jena.list;
import cz.cvut.kbss.ontodriver.descriptor.ListDescriptor;
import cz.cvut.kbss.ontodriver.exception.IntegrityConstraintViolatedException;
import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
import cz.cvut.kbss.ontodriver.jena.exception.ListProcessingException;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import java.util.Collection;
import java.util.NoSuchElementException;
import static org.apache.jena.rdf.model.ResourceFactory.createProperty;
import static org.apache.jena.rdf.model.ResourceFactory.createResource;
abstract class AbstractListIterator {
final StorageConnector connector;
final Property hasListProperty;
final Property hasNextProperty;
final String context;
int index;
private boolean removed = false;
Resource previousNode;
Resource currentNode;
Collection<Statement> cursor;
AbstractListIterator(ListDescriptor descriptor, StorageConnector connector) {
this.hasListProperty = createProperty(descriptor.getListProperty().getIdentifier().toString());
this.hasNextProperty = createProperty(descriptor.getNextNode().getIdentifier().toString());
this.context = descriptor.getContext() != null ? descriptor.getContext().toString() : null;
this.connector = connector;
this.index = -1;
this.currentNode = createResource(descriptor.getListOwner().getIdentifier().toString());
moveCursor(currentNode);
}
void moveCursor(Resource from) {
this.cursor = connector.find(from, first() ? hasListProperty : hasNextProperty, null, context);
}
void resolveNextListNode() {
verifySuccessorCount();
final RDFNode node = cursor.iterator().next().getObject();
if (!node.isURIResource()) {
throw new ListProcessingException("Expected successor of node " + currentNode + " to be a named resource.");
}
final Resource item = node.asResource();
index++;
this.removed = false;
this.previousNode = currentNode;
this.currentNode = item;
moveCursor(currentNode);
}
boolean first() {
return index == -1;
}
boolean hasNext() {
return !cursor.isEmpty();
}
void verifySuccessorCount() {
if (!hasNext()) {
throw new NoSuchElementException("No more elements available.");
}
if (cursor.size() > 1) {
throw new IntegrityConstraintViolatedException(
"Encountered multiple successors of list node " + currentNode.getURI());
}
}
void remove(Resource subject, Property property, RDFNode object) {
connector.remove(subject, property, object, context);
}
abstract Axiom<NamedResource> nextAxiom();
abstract NamedResource nextValue();
/**
* Only returns current node.
* <p>
* Does not advance the iterator like {@link #nextAxiom()} and {@link #nextValue()} do.
*
* @return Current list node
*/
Resource getCurrentNode() {
return currentNode;
}
/**
* Removes the current node without reconnecting the subsequent nodes to the previous one.
*/
void removeWithoutReconnect() {
if (first()) {
throw new IllegalStateException("Cannot call remove before calling next.");
}
if (removed) {
throw new IllegalStateException("Cannot call remove multiple times on one element.");
}
assert previousNode != null;
assert currentNode != null;
remove(previousNode, index == 0 ? hasListProperty : hasNextProperty, currentNode);
this.removed = true;
}
abstract void replace(Resource replacement);
}