MutableSegmentReferences implements SegmentReferences { int addOrReference(SegmentId id) { Integer number = numbers.get(id); if (number != null) { return number; } synchronized (lock) { number = numbers.get(id); if (number != null) { return number; } ids.add(id); number = ids.size(); numbers.put(id, number); return number; } }  @Override SegmentId getSegmentId(int reference); @NotNull @Override Iterator<SegmentId> iterator();  }
@Test public void referencesShouldBeGreaterThanZero() throws Exception { MemoryStore store = new MemoryStore(); SegmentId id = store.getSegmentIdProvider().newDataSegmentId(); MutableSegmentReferences table = new MutableSegmentReferences(); int reference = table.addOrReference(id); assertEquals(1, reference); }
@Test public void referencesShouldBeIncrementing() throws Exception { MemoryStore store = new MemoryStore(); SegmentId first = store.getSegmentIdProvider().newDataSegmentId(); SegmentId second = store.getSegmentIdProvider().newDataSegmentId(); MutableSegmentReferences table = new MutableSegmentReferences(); int firstReference = table.addOrReference(first); int secondReference = table.addOrReference(second); assertEquals(firstReference + 1, secondReference); }
@Test public void shouldNotAddSameSegmentIdTwice() throws Exception { MemoryStore store = new MemoryStore(); SegmentId id = store.getSegmentIdProvider().newDataSegmentId(); MutableSegmentReferences table = new MutableSegmentReferences(); int first = table.addOrReference(id); int second = table.addOrReference(id); assertEquals(first, second); }
@Test public void shouldIterateInInsertionOrder() throws Exception { MemoryStore store = new MemoryStore(); SegmentId first = store.getSegmentIdProvider().newDataSegmentId(); SegmentId second = store.getSegmentIdProvider().newDataSegmentId(); List<SegmentId> ids = newArrayList(first, second); MutableSegmentReferences table = new MutableSegmentReferences(); table.addOrReference(first); table.addOrReference(second); assertTrue(elementsEqual(ids, table)); }