The add() does not need to call contains(), a O(n) operation, to check
if the element was already present. It can simply re-use the return
value of TreeMultimap.put().
Change-Id: Iab1971f6310163099906df0a1702d106e3640316
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/56904
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Hudson CI
@Test
public void testNoDuplicateElements() {
for (ISegment segment : SEGMENTS) {
- fSegmentStore.add(new BasicSegment(segment.getStart(), segment.getEnd()));
+ boolean ret = fSegmentStore.add(new BasicSegment(segment.getStart(), segment.getEnd()));
+ assertFalse(ret);
}
assertEquals(SEGMENTS.size(), fSegmentStore.size());
}
fLock.writeLock().lock();
try {
- /* We can take a read lock while holding the write lock. */
- if (contains(val)) {
- return false;
- }
-
if (fStartTimesIndex.put(Long.valueOf(val.getStart()), val)) {
fEndTimesIndex.put(Long.valueOf(val.getEnd()), val);
fSize++;
fLastSnapshot = null;
+ return true;
}
+ return false;
} finally {
fLock.writeLock().unlock();
}
- return true;
}
@Override