ImmutableRecordNumbers implements RecordNumbers { @Override public int getOffset(int recordNumber) { if (recordNumber < offsets.length) { return offsets[recordNumber]; } else { return -1; } } ImmutableRecordNumbers(@NotNull int[] offsets, @NotNull byte[] type); @Override int getOffset(int recordNumber); @NotNull @Override Iterator<Entry> iterator();  }
@Test public void tableShouldBeCorrectlyInitialized() { Map<Integer, Integer> entries = new HashMap<>(); entries.put(1, 2); entries.put(3, 4); entries.put(5, 6); ImmutableRecordNumbers table = new ImmutableRecordNumbers(offsets(entries), types(entries)); assertEquals(2, table.getOffset(1)); assertEquals(4, table.getOffset(3)); assertEquals(6, table.getOffset(5)); assertEquals(-1, table.getOffset(2)); assertEquals(-1, table.getOffset(42)); }
@Test public void changingInitializationMapShouldBeSafe() { Map<Integer, Integer> entries = new HashMap<>(); entries.put(1, 2); entries.put(3, 4); entries.put(5, 6); ImmutableRecordNumbers table = new ImmutableRecordNumbers(offsets(entries), types(entries)); entries.put(1, 3); entries.put(7, 8); entries.remove(3); assertEquals(2, table.getOffset(1)); assertEquals(4, table.getOffset(3)); assertEquals(6, table.getOffset(5)); assertEquals(-1, table.getOffset(2)); assertEquals(-1, table.getOffset(42)); }
@Test public void iteratingShouldBeCorrect() { Map<Integer, Integer> entries = new HashMap<>(); entries.put(1, 2); entries.put(3, 4); entries.put(5, 6); ImmutableRecordNumbers table = new ImmutableRecordNumbers(offsets(entries), types(entries)); Map<Integer, Integer> iterated = new HashMap<>(); for (Entry entry : table) { iterated.put(entry.getRecordNumber(), entry.getOffset()); } assertEquals(entries, iterated); }