GenomeRegions { public void addPosition(final long position) { GenomeRegion prev = null; for (int i = 0; i < regions.size(); i++) { GenomeRegion current = regions.get(i); if (position >= current.start() && position <= current.end()) { return; } if (position < current.start()) { if (prev != null && prev.end() + minGap >= position) { if (position + minGap >= current.start()) { prev = create(prev.chromosome(), prev.start(), current.end()); regions.set(i - 1, prev); regions.remove(i); return; } else { prev = create(prev.chromosome(), prev.start(), position); regions.set(i - 1, prev); return; } } if (position + minGap >= current.start()) { current = create(current.chromosome(), position, current.end()); regions.set(i, current); return; } regions.add(i, create(chromosome, position, position)); return; } prev = current; } if (prev != null && prev.end() + minGap >= position) { prev = GenomeRegions.create(prev.chromosome(), prev.start(), position); regions.set(regions.size() - 1, prev); } else { regions.add(create(chromosome, position, position)); } } GenomeRegions(@NotNull final String chromosome);  GenomeRegions(@NotNull final String chromosome, final int minGap); @NotNull static GenomeRegion create(@NotNull final String chromosome, final long start, final long end); @NotNull List<GenomeRegion> build(); void addRegion(final long start, final long end); void addPosition(final long position);  }
@Test public void testAddPosition() { final GenomeRegions victim = new GenomeRegions(CHROM, 1); final List<Long> positions = Lists.newArrayList(1L, 1L, 2L, 3L, 5L, 6L, 7L, 9L, 10L, 10L); Collections.shuffle(positions); for (Long position : positions) { victim.addPosition(position); } final List<GenomeRegion> regions = victim.build(); Assert.assertEquals(3, regions.size()); assertRegion(regions.get(0), 1, 3); assertRegion(regions.get(1), 5, 7); assertRegion(regions.get(2), 9, 10); }