@Test public void withCloseableIteratorShouldApplyGivenFunction() throws IOException { List<String> elems = Arrays.asList("elem 1", "elem 2", "elem 3"); Closeable closeable = mock(Closeable.class); CloseableIterator<String> iterator = new CloseableIteratorString(elems.iterator(), closeable); Function<Stream<String>, Stream<String>> f = Function.identity(); Stream<String> stream = StreamUtils.withCloseableIterator(iterator, f); assertEquals(elems, stream.collect(Collectors.toList())); verify(closeable, times(1)).close(); }
public static <X, R> R withCloseableIterator(CloseableIterator<X> iterator, Function<Stream<X>, R> f) throws IOException { try { return f.apply(asStream(iterator)); } finally { iterator.close(); } }
StreamUtils { public static <X, R> R withCloseableIterator(CloseableIterator<X> iterator, Function<Stream<X>, R> f) throws IOException { try { return f.apply(asStream(iterator)); } finally { iterator.close(); } } }
StreamUtils { public static <X, R> R withCloseableIterator(CloseableIterator<X> iterator, Function<Stream<X>, R> f) throws IOException { try { return f.apply(asStream(iterator)); } finally { iterator.close(); } } private  StreamUtils(); }
StreamUtils { public static <X, R> R withCloseableIterator(CloseableIterator<X> iterator, Function<Stream<X>, R> f) throws IOException { try { return f.apply(asStream(iterator)); } finally { iterator.close(); } } private  StreamUtils(); static Stream<X> asStream(Iterator<X> iterator); static R withCloseableIterator(CloseableIterator<X> iterator, Function<Stream<X>, R> f); }
StreamUtils { public static <X, R> R withCloseableIterator(CloseableIterator<X> iterator, Function<Stream<X>, R> f) throws IOException { try { return f.apply(asStream(iterator)); } finally { iterator.close(); } } private  StreamUtils(); static Stream<X> asStream(Iterator<X> iterator); static R withCloseableIterator(CloseableIterator<X> iterator, Function<Stream<X>, R> f);  }