-
Notifications
You must be signed in to change notification settings - Fork 14
/
AbstractListIterator.java
115 lines (95 loc) · 3.81 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
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 hasList;
final Property hasNext;
final String context;
int index;
boolean removed = false;
Resource previousNode;
Resource currentNode;
Collection<Statement> cursor;
AbstractListIterator(ListDescriptor descriptor, StorageConnector connector) {
this.hasList = createProperty(descriptor.getListProperty().getIdentifier().toString());
this.hasNext = 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) {
if (context != null) {
this.cursor = connector.find(from, first() ? hasList : hasNext, null, context);
} else {
this.cursor = connector.find(from, first() ? hasList : hasNext, null);
}
}
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) {
if (context != null) {
connector.remove(subject, property, object, context);
} else {
connector.remove(subject, property, object);
}
}
abstract Axiom<NamedResource> nextAxiom();
abstract NamedResource nextValue();
/**
* 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 ? hasList : hasNext, currentNode);
this.removed = true;
}
abstract void replace(Resource replacement);
}