Package: AbstractListIterator
AbstractListIterator
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
AbstractListIterator(ListDescriptor, StorageConnector) |
|
|
|
|
|
||||||||||||||||||||
first() |
|
|
|
|
|
||||||||||||||||||||
getCurrentNode() |
|
|
|
|
|
||||||||||||||||||||
hasNext() |
|
|
|
|
|
||||||||||||||||||||
moveCursor(Resource) |
|
|
|
|
|
||||||||||||||||||||
remove(Resource, Property, RDFNode) |
|
|
|
|
|
||||||||||||||||||||
removeWithoutReconnect() |
|
|
|
|
|
||||||||||||||||||||
resolveNextListNode() |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
verifySuccessorCount() |
|
|
|
|
|
Coverage
1: package cz.cvut.kbss.ontodriver.jena.list;
2:
3: import cz.cvut.kbss.ontodriver.descriptor.ListDescriptor;
4: import cz.cvut.kbss.ontodriver.exception.IntegrityConstraintViolatedException;
5: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
6: import cz.cvut.kbss.ontodriver.jena.exception.ListProcessingException;
7: import cz.cvut.kbss.ontodriver.model.Axiom;
8: import cz.cvut.kbss.ontodriver.model.NamedResource;
9: import org.apache.jena.rdf.model.Property;
10: import org.apache.jena.rdf.model.RDFNode;
11: import org.apache.jena.rdf.model.Resource;
12: import org.apache.jena.rdf.model.Statement;
13:
14: import java.util.Collection;
15: import java.util.NoSuchElementException;
16:
17: import static org.apache.jena.rdf.model.ResourceFactory.createProperty;
18: import static org.apache.jena.rdf.model.ResourceFactory.createResource;
19:
20: abstract class AbstractListIterator {
21:
22: final StorageConnector connector;
23:
24: final Property hasList;
25: final Property hasNext;
26:
27: final String context;
28:
29: int index;
30: private boolean removed = false;
31:
32: Resource previousNode;
33: Resource currentNode;
34: Collection<Statement> cursor;
35:
36: AbstractListIterator(ListDescriptor descriptor, StorageConnector connector) {
37: this.hasList = createProperty(descriptor.getListProperty().getIdentifier().toString());
38: this.hasNext = createProperty(descriptor.getNextNode().getIdentifier().toString());
39:• this.context = descriptor.getContext() != null ? descriptor.getContext().toString() : null;
40: this.connector = connector;
41: this.index = -1;
42: this.currentNode = createResource(descriptor.getListOwner().getIdentifier().toString());
43: moveCursor(currentNode);
44: }
45:
46: void moveCursor(Resource from) {
47:• this.cursor = connector.find(from, first() ? hasList : hasNext, null, context);
48: }
49:
50: void resolveNextListNode() {
51: verifySuccessorCount();
52: final RDFNode node = cursor.iterator().next().getObject();
53:• if (!node.isURIResource()) {
54: throw new ListProcessingException("Expected successor of node " + currentNode + " to be a named resource.");
55: }
56: final Resource item = node.asResource();
57: index++;
58: this.removed = false;
59: this.previousNode = currentNode;
60: this.currentNode = item;
61: moveCursor(currentNode);
62: }
63:
64: boolean first() {
65:• return index == -1;
66: }
67:
68: boolean hasNext() {
69:• return !cursor.isEmpty();
70: }
71:
72: void verifySuccessorCount() {
73:• if (!hasNext()) {
74: throw new NoSuchElementException("No more elements available.");
75: }
76:• if (cursor.size() > 1) {
77: throw new IntegrityConstraintViolatedException(
78: "Encountered multiple successors of list node " + currentNode.getURI());
79: }
80: }
81:
82: void remove(Resource subject, Property property, RDFNode object) {
83: connector.remove(subject, property, object, context);
84: }
85:
86: abstract Axiom<NamedResource> nextAxiom();
87:
88: abstract NamedResource nextValue();
89:
90: /**
91: * Only returns current node.
92: * <p>
93: * Does not advance the iterator like {@link #nextAxiom()} and {@link #nextValue()} do.
94: *
95: * @return Current list node
96: */
97: Resource getCurrentNode() {
98: return currentNode;
99: }
100:
101: /**
102: * Removes the current node without reconnecting the subsequent nodes to the previous one.
103: */
104: void removeWithoutReconnect() {
105:• if (first()) {
106: throw new IllegalStateException("Cannot call remove before calling next.");
107: }
108:• if (removed) {
109: throw new IllegalStateException("Cannot call remove multiple times on one element.");
110: }
111:• assert previousNode != null;
112:• assert currentNode != null;
113:• remove(previousNode, index == 0 ? hasList : hasNext, currentNode);
114: this.removed = true;
115: }
116:
117: abstract void replace(Resource replacement);
118: }