RangeSet extends AbstractSet<Long> implements Set<Long> { @Override public Iterator<Long> iterator() { return new Iterator<Long>() { private final Iterator<LongSegment> segmentIterator = map.values().iterator(); private Iterator<Long> longIterator = segmentIterator.hasNext() ? segmentIterator.next().iterator() : null; @Override public boolean hasNext() { return longIterator != null && longIterator.hasNext(); } @Override public Long next() { if (!hasNext()) { throw new NoSuchElementException(); } Long ret = longIterator.next(); if (!longIterator.hasNext()) { longIterator = segmentIterator.hasNext() ? segmentIterator.next().iterator() : null; } return ret; } @Override public void remove() { throw new UnsupportedOperationException("Iterator does not support remove"); } }; }  @Override int size(); @Override boolean contains(Object o); @Override Iterator<Long> iterator(); @Override boolean add(Long aLong); @Override boolean remove(Object o); @Override boolean removeAll(Collection<?> c); @Override void clear();  }
@Test(groups = "fast") public void testIterator() throws Exception { Iterator<Long> iter; iter = set.iterator(); Assert.assertFalse(iter.hasNext()); set.add(1L); iter = set.iterator(); Assert.assertTrue(iter.hasNext()); Assert.assertTrue(iter.next() == 1); Assert.assertFalse(iter.hasNext()); set.add(2L); iter = set.iterator(); Assert.assertTrue(iter.hasNext()); Assert.assertTrue(iter.next() == 1); Assert.assertTrue(iter.hasNext()); Assert.assertTrue(iter.next() == 2); Assert.assertFalse(iter.hasNext()); set.add(-1L); iter = set.iterator(); Assert.assertTrue(iter.hasNext()); Assert.assertTrue(iter.next() == -1); Assert.assertTrue(iter.hasNext()); Assert.assertTrue(iter.next() == 1); Assert.assertTrue(iter.hasNext()); Assert.assertTrue(iter.next() == 2); Assert.assertFalse(iter.hasNext()); }
@Test(groups = "fast", expectedExceptions = NoSuchElementException.class) public void testIllegalIterator() throws Exception { Iterator<Long> iter; iter = set.iterator(); Assert.assertFalse(iter.hasNext()); iter.next(); }