@Test public void iteratedCorrectly() throws IOException { MatcherAssert.assertThat( "Each rows are iterated correctly", new IteratorIterable<>( new ExcelIterator( new XSSFWorkbook( new ResourceAsStream("excel/test.xlsx").stream() ) ) ), Matchers.contains( Matchers.is("jed,24.0"), Matchers.is("aisyl,20.0"), Matchers.is("linux,23.0"), Matchers.is("juan,29.0") ) ); }
@Test public void iteratedCorrectlyWithBlankSheet() throws IOException { MatcherAssert.assertThat( "Each rows are iterated correctly, even with blank sheet", new IteratorIterable<>( new ExcelIterator( new XSSFWorkbook( new ResourceAsStream("excel/test-trail-blank-sheet.xlsx").stream() ) ) ), Matchers.contains( Matchers.is("jed,24.0"), Matchers.is("aisyl,20.0"), Matchers.is("linux,23.0"), Matchers.is("juan,29.0") ) ); }
@Ignore @Test public void iteratedCorrectlyBetweenBlankSheet() throws IOException { MatcherAssert.assertThat( "Each rows are iterated correctly, between blank sheets", new IteratorIterable<>( new ExcelIterator( new XSSFWorkbook( new ResourceAsStream("excel/test-between-blank-sheet.xlsx").stream() ) ) ), Matchers.contains( Matchers.is("jed,24.0"), Matchers.is("aisyl,20.0"), Matchers.is("linux,23.0"), Matchers.is("juan,29.0") ) ); }
@Test public void iteratedCorrectlyScatteredSheet() throws IOException { MatcherAssert.assertThat( "Each rows are iterated correctly when scattered on multiple sheets", new IteratorIterable<>( new ExcelIterator( new XSSFWorkbook( new ResourceAsStream("excel/test-scattered-sheets.xlsx").stream() ) ) ), Matchers.contains( Matchers.is("jed,24.0"), Matchers.is("aisyl,20.0"), Matchers.is("linux,23.0"), Matchers.is("juan,29.0") ) ); }
@Ignore @Test public void iteratedCorrectlyBlankMiddle() throws IOException { MatcherAssert.assertThat( "Each rows are iterated correctly while middle sheet is blank", new IteratorIterable<>( new ExcelIterator( new XSSFWorkbook( new ResourceAsStream("excel/test-blank-middle-sheet.xlsx").stream() ) ) ), Matchers.contains( Matchers.is("jed,24.0"), Matchers.is("aisyl,20.0"), Matchers.is("linux,23.0"), Matchers.is("juan,29.0") ) ); }
@Override public List<ProcessTask> handle(EntityManager manager) throws Exception { if (!file.exists()) { return todo; } try (ZipFile zip = new ZipFile(file)) { for (ZipArchiveEntry entry : new IteratorIterable<>(new EnumerationIterator<>(zip.getEntries()))) { try { handleSingleZipEntry(zip, entry, manager); } catch (IOException ex) { System.out.println(ex.getLocalizedMessage()); } } } return todo; }
private Iterable<Metric> _getMetrics( E environment, String name, Interval interval ) { log.debug( "Performing fetch for metric " + name + " for " + interval.toString() ); Optional<List<Metric>> metricsFromCache = environment.metricCache().getValues( name, interval ); if( metricsFromCache.isPresent() ) { if( !metricsFromCache.get().isEmpty() && metricsFromCache.get().get( 0 ).timestamp() <= interval.start() ) { log.debug( "Got all results we want from cache." ); return metricsFromCache.get(); } else { log.debug( "Got some results from cache, but need to get the rest from the database" ); long refetchEnd = metricsFromCache.get().isEmpty() ? interval.end() : metricsFromCache.get().get( 0 ).timestamp(); return new IteratorIterable<>( new IteratorChain<>( environment.cassandraAccessLayer().loadMetrics( name, new Interval( interval.start(), refetchEnd ) ).iterator(), metricsFromCache.get().iterator() ) ); } } else { log.debug( "Got no results from cache, need to hit the database." ); return environment.cassandraAccessLayer().loadMetrics( name, interval ); } }
/** * Gets an {@link Iterable} that wraps an iterator. The returned {@link Iterable} can be * used for a single iteration. * * @param <E> the element type * @param iterator the iterator to use, may not be null * @return a new, single use {@link Iterable} * @throws NullPointerException if iterator is null */ public static <E> Iterable<E> asIterable(final Iterator<? extends E> iterator) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } return new IteratorIterable<E>(iterator, false); }
/** * Gets an iterable that wraps an iterator. The returned iterable can be * used for multiple iterations. * * @param <E> the element type * @param iterator the iterator to use, may not be null * @return a new, multiple use iterable * @throws NullPointerException if iterator is null */ public static <E> Iterable<E> asMultipleUseIterable(final Iterator<? extends E> iterator) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } return new IteratorIterable<E>(iterator, true); }