@Override public boolean hasNext() { if (!hasNextCalled) { next = stream.readObject(); hasNextCalled = true; } return next != null; }
@Override public T readObject() { T obj = delegate.readObject(); while (obj != null && !filter.apply(obj)) { obj = delegate.readObject(); } return obj; } }
@Override default void forEach(Consumer<? super T> action) { T obj = readObject(); while (obj != null) { action.accept(obj); obj = readObject(); } }
@SuppressWarnings("unchecked") @Override public T readObject() { Object obj = source.readObject(); while (obj != null && !type.isInstance(obj)) { obj = source.readObject(); } return type.cast(obj); }
@Override public T readObject() { S obj = delegate.readObject(); if (obj != null) { T res = function.apply(obj); if (res == null) { throw new NullPointerException("stream transformer mapped " + obj + " to null"); } return res; } else { return null; } }
/** * Consume and discard the first {@code n} elements from an object stream. * @param n The number of elements to drop. * @param stream The stream. * @param <T> The stream's element type. * @return The passed-in stream, for convenience and functional-looking code. This method immediately consumes the * elements, however, so {@code stream} is modified. */ public static <T> ObjectStream<T> consume(int n, ObjectStream<T> stream) { Preconditions.checkArgument(n >= 0, "number to skip must be non-negative"); boolean wasNull = false; for (int i = 0; i < n && !wasNull; i++) { T obj = stream.readObject(); wasNull = obj == null; } return stream; }
@Nonnull @Override public T next() { if (!hasNextCalled) { next = stream.readObject(); } if (next == null) { throw new NoSuchElementException(); } final T n = next; next = null; hasNextCalled = false; return n; }
protected GroupingObjectStream(@WillCloseWhenClosed ObjectStream<? extends E> base) { baseStream = base; // prime the stream; nextItem = baseStream.readObject(); }
@Override public T readObject() { T obj = null; while (current != null) { obj = current.readObject(); if (obj == null) { current = Iterators.getNext(streamIter, null); } else { break; } } return obj; }
/** * Count the items in a stream. * * @param objectStream The object stream. * @return The number of items in the stream. * @deprecated Use {@link ObjectStream#collect(Collector)}. */ @SuppressWarnings("PMD.LooseCoupling") @Deprecated public static int count(@WillClose ObjectStream<?> objectStream) { try { if (objectStream instanceof IteratorObjectStream) { List<?> list = ((IteratorObjectStream) objectStream).getList(); if (list != null) { return list.size(); } } int n = 0; Object obj = objectStream.readObject(); while (obj != null) { n++; obj = objectStream.readObject(); } return n; } finally { objectStream.close(); } }
@Nullable @Override public C readObject() { if (nextItem == null) { return null; } C group = null; while (group == null && nextItem != null) { if (handleItem(nextItem)) { // item accepted, advance nextItem = baseStream.readObject(); } else { group = finishGroup(); } } if (group == null) { group = finishGroup(); } return group; }
/** * Open a stream to read entities from this source. * @return A stream of entities. */ @Override public ObjectStream<Entity> openStream() throws IOException { BufferedReader reader = source.openBufferedStream(); ObjectStream<String> lines = new LineStream(reader); int headerLines = format.getHeaderLines(); List<String> header = new ArrayList<>(); while (header.size() < headerLines) { String line = lines.readObject(); if (line == null) { IOException ex = new IOException(String.format("%s: expected %d header lines, found %d", sourceURL, headerLines, header.size())); try { lines.close(); } catch (Throwable th) { ex.addSuppressed(th); } throw ex; } header.add(line); } LineEntityParser parser = format.makeParser(header); return ObjectStreams.transform(lines, parser); }
@Test public void testWrapCollection() { ObjectStream<String> objectStream = ObjectStreams.wrap(Lists.newArrayList("foo", "bar")); try { assertThat(objectStream.readObject(), equalTo("foo")); assertThat(objectStream.readObject(), equalTo("bar")); assertThat(objectStream.readObject(), nullValue()); } finally { objectStream.close(); } }
@Test public void testEmptyConcat() { @SuppressWarnings("unchecked") ObjectStream<String> objectStream = ObjectStreams.concat(); assertThat(objectStream.readObject(), nullValue()); } @SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") @Test public void testConcatEmpty() { ObjectStream<String> objectStream = ObjectStreams.concat(ObjectStreams.<String>empty()); assertThat(objectStream.readObject(), nullValue()); }
@Test public void testConsumeTooMany() { ObjectStream<String> cur = ObjectStreams.of("foo", "bar"); cur = ObjectStreams.consume(3, cur); assertThat(cur.readObject(), nullValue()); }
@Test public void testWrapEmptyCollection() { ObjectStream<?> objectStream = ObjectStreams.wrap(Collections.emptyList()); try { assertThat(objectStream.readObject(), nullValue()); try { objectStream.iterator().next(); fail("next should fail on empty stream"); } catch (NoSuchElementException e) { /* expected */ } } finally { objectStream.close(); } }
@Test public void testEmptyStream() { ObjectStream<String> cur = ObjectStreams.empty(); try { assertThat(cur.readObject(), nullValue()); try { cur.iterator().next(); fail("next() on empty stream should fail"); } catch (NoSuchElementException e) { /* expected */ } } finally { cur.close(); } }
@Test public void testConcatWithEmpty() { @SuppressWarnings("unchecked") ObjectStream<String> objectStream = ObjectStreams.concat(ObjectStreams.of("foo"), ObjectStreams.<String>empty(), ObjectStreams.of("bar")); assertThat(objectStream, contains("foo", "bar")); assertThat(objectStream.readObject(), nullValue()); } //endregion
@Test public void testConfigureItemJSON() throws IOException, URISyntaxException { URI baseURI = TextEntitySourceTest.class.getResource("header-ratings.csv").toURI(); JsonNode node = reader.readTree("{\"file\": \"items.json\", \"format\": \"json\", \"entity_type\": \"item\"}"); TextEntitySource fr = TextEntitySource.fromJSON("test", node, baseURI); try (ObjectStream<Entity> stream = fr.openStream()) { Entity first = stream.readObject(); assertThat(first.getType(), equalTo(CommonTypes.ITEM)); assertThat(first.getId(), equalTo(42L)); assertThat(first.get(CommonAttributes.ENTITY_ID), equalTo(42L)); assertThat(first.get(CommonAttributes.NAME), equalTo("woozle")); Entity second = stream.readObject(); assertThat(second.getType(), equalTo(CommonTypes.ITEM)); assertThat(second.getId(), equalTo(37L)); assertThat(second.get(CommonAttributes.ENTITY_ID), equalTo(37L)); assertThat(second.get(CommonAttributes.NAME), equalTo("heffalump")); assertThat(stream.readObject(), nullValue()); } }