-
Notifications
You must be signed in to change notification settings - Fork 14
/
SimpleListIterator.java
65 lines (57 loc) · 2.34 KB
/
SimpleListIterator.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
package cz.cvut.kbss.ontodriver.jena.list;
import cz.cvut.kbss.ontodriver.descriptor.SimpleListDescriptor;
import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
import cz.cvut.kbss.ontodriver.model.*;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.apache.jena.rdf.model.ResourceFactory.createStatement;
class SimpleListIterator extends AbstractListIterator {
SimpleListIterator(SimpleListDescriptor descriptor, StorageConnector connector) {
super(descriptor, connector);
}
Axiom<NamedResource> nextAxiom() {
final NamedResource subject = NamedResource.create(currentNode.getURI());
final Assertion assertion = Assertion
.createObjectPropertyAssertion(URI.create(first() ? hasList.getURI() : hasNext.getURI()), false);
final NamedResource value = nextValue();
return new AxiomImpl<>(subject, assertion, new Value<>(value));
}
NamedResource nextValue() {
resolveNextListNode();
return NamedResource.create(currentNode.getURI());
}
/**
* Replaces the current node with the specified replacement, connecting it into the list. Original links to the current
* element are removed.
*
* @param replacement The replacement node
*/
void replace(Resource replacement) {
removeWithoutReconnect();
final List<Statement> toAdd = new ArrayList<>(2);
toAdd.add(createStatement(previousNode, index == 0 ? hasList : hasNext, replacement));
if (hasNext()) {
verifySuccessorCount();
final RDFNode nextNode = cursor.iterator().next().getObject();
remove(currentNode, hasNext, nextNode);
if (!nextNode.equals(replacement)) {
final Statement linkToNext = createStatement(replacement, hasNext, nextNode);
toAdd.add(linkToNext);
this.cursor = Collections.singleton(linkToNext);
} else {
moveCursor(replacement);
}
}
this.currentNode = replacement;
if (context != null) {
connector.add(toAdd, context);
} else {
connector.add(toAdd);
}
}
}