@Override
public void add(int index, Node element) {
if (index < 0 || index >= size())
throw new IndexOutOfBoundsException();
Relationship currentEntryRelation = underlyingNode
.getSingleRelationship(RelTypes.FIRST_ELEMENT,
Direction.OUTGOING);
Node currentEntry = currentEntryRelation.getEndNode();
if (index == 0) {
currentEntryRelation.delete();
Node newEntry = graphDb.createNode();
underlyingNode.createRelationshipTo(newEntry,
RelTypes.FIRST_ELEMENT);
newEntry.createRelationshipTo(currentEntry, RelTypes.NEXT_ELEMENT);
newEntry.createRelationshipTo(element, RelTypes.LIST_ENTRY);
underlyingNode.setProperty(INDEX_SIZE, 1);
} else {
for (int i = 0; i < index; i++) {
currentEntryRelation = currentEntry.getSingleRelationship(
RelTypes.NEXT_ELEMENT, Direction.OUTGOING);
currentEntry = currentEntryRelation.getEndNode();
}
Node prevEntry = currentEntryRelation.getStartNode();
currentEntryRelation.delete();
Node newEntry = graphDb.createNode();
prevEntry.createRelationshipTo(newEntry, RelTypes.NEXT_ELEMENT);
newEntry.createRelationshipTo(currentEntry, RelTypes.NEXT_ELEMENT);
newEntry.createRelationshipTo(element, RelTypes.LIST_ENTRY);
int i = (Integer) underlyingNode.getProperty(INDEX_SIZE);