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