Skip to content
Permalink
Browse files Browse the repository at this point in the history
XWIKI-19223: Improve xobject memory storage in XWikidocument
* fix List#remove implementation
* override List#clear implementation for performances
  • Loading branch information
tmortagne committed Jan 11, 2022
1 parent 6b10ebe commit fdfce06
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
Expand Up @@ -102,7 +102,7 @@ public void add(int index, BaseObject element)
// Check if the index is valid
rangeCheckForAdd(index);

// Move right values
// Shifts right values to the right
if (index < this.size) {
for (int i = this.size - 1; i >= index; --i) {
put(i + 1, get(i));
Expand All @@ -128,7 +128,26 @@ public BaseObject remove(int index)
{
rangeCheck(index);

return this.map.remove(index);
BaseObject previous = this.map.remove(index);

// Shifts right values to the left
if (index < this.size - 1) {
for (int i = index; i < this.size - 1; ++i) {
put(i, get(i + 1));
}
}

// The list is one element shorter
--this.size;

return previous;
}

@Override
public void clear()
{
this.map.clear();
this.size = 0;
}

private void rangeCheck(int index)
Expand Down
Expand Up @@ -120,12 +120,26 @@ void remove()
assertEquals(3, objects.size());

objects.remove(0);
objects.remove(2);

assertNull(objects.get(0));
assertSame(XOBJ2, objects.get(1));
assertNull(objects.get(2));
assertEquals(2, objects.size());
assertSame(XOBJ2, objects.get(0));
assertSame(XOBJ3, objects.get(1));

objects.remove(1);

assertEquals(1, objects.size());
assertSame(XOBJ2, objects.get(0));
}

@Test
void clear()
{
BaseObjects objects = new BaseObjects(Arrays.asList(XOBJ1, XOBJ2, XOBJ3));

assertEquals(3, objects.size());

objects.clear();

assertEquals(0, objects.size());
}
}

0 comments on commit fdfce06

Please sign in to comment.