FixedSizeStream extends Stream<T> { @Override public Iterator<T> iterator() { return new ReadOnlyIterator<T>() { int index; @Override public boolean hasNext() { return index < length; } @Override public T next() { return get.call(index++); } }; } FixedSizeStream(int length, Func1<Integer, T> get); @Override Iterator<T> iterator();  }
@Test public void testIterator() throws Exception { assertIterableEquals(asList("0", "1", "2"), create3()); assertIterableEquals(emptyList(), new FixedSizeStream<>(0, null)); }
@Test(expected = UnsupportedOperationException.class) public void testReadOnly() throws Exception { Iterator<String> iterator = create3().iterator(); iterator.next(); iterator.remove(); }