@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)); }
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; } }
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; } } }
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; } }  }
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(); }
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();  }