@Override public void close() { source.close(); } };
@Override default Spliterator<T> spliterator() { return Spliterators.spliteratorUnknownSize(iterator(), 0); } }
@Override public boolean hasNext() { if (!hasNextCalled) { next = stream.readObject(); hasNextCalled = true; } return next != null; }
/** * 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(); } }
@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 testWrapCollectionIterator() { ObjectStream<String> objectStream = ObjectStreams.wrap(Lists.newArrayList("foo", "bar")); try { List<String> strs = Lists.newArrayList(objectStream.iterator()); assertThat(strs, hasSize(2)); assertThat(strs, contains("foo", "bar")); } finally { objectStream.close(); } }
@Override public ObjectStream<IdBox<Long2DoubleMap>> streamUsers() { ObjectStream<IdBox<List<Rating>>> stream = dao.query(Rating.class) .groupBy(CommonAttributes.USER_ID) .stream(); return ObjectStreams.wrap(stream.map(u -> u.mapValue(Ratings::userRatingVector)), stream); } }
@Override public List<E> get() { return stream().collect(Collectors.toList()); }
@Override public int count() { return (int) stream().count(); }
@Test public void testFilter() { List<Rating> ratings = makeRatings(); List<Rating> correct = ratings.stream() .filter(r -> r.getUserId() % 2 == 1) .collect(Collectors.toList()); List<Rating> result = ObjectStreams.wrap(ratings) .filter(r -> r.getUserId() % 2 == 1) .collect(Collectors.toList()); assertThat(result, equalTo(correct)); }
/** * 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 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(); } }
@Override public ObjectStream<IdBox<Long2DoubleMap>> streamUsers() { ObjectStream<IdBox<List<Rating>>> stream = dao.query(Rating.class) .groupBy(CommonAttributes.USER_ID) .stream(); return ObjectStreams.wrap(stream.map(u -> u.mapValue(Ratings::userRatingVector)), stream); } }
@Test public void testAddEntityLayout() { EntityCollectionDAOBuilder b = EntityCollectionDAO.newBuilder(); b.addEntityLayout(Rating.ENTITY_TYPE, Rating.ATTRIBUTES); Rating r = Rating.newBuilder() .setId(42) .setUserId(100) .setItemId(50) .setRating(3.5) .setTimestamp(1034801) .build(); b.addEntity(r); EntityCollectionDAO dao = b.build(); assertThat(dao.getEntityIds(CommonTypes.RATING), contains(42L)); assertThat(dao.streamEntities(CommonTypes.RATING) .collect(Collectors.toList()), contains(r)); assertThat(dao.query(Rating.class) .get(), contains(r)); }
@Override public void close() { delegate.close(); } }
@Override default void forEach(Consumer<? super T> action) { T obj = readObject(); while (obj != null) { action.accept(obj); obj = readObject(); } }
@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(); } }
/** * Extract a user rating vector from a rating stream. * * @param ratings The rating stream. * @return The user rating vector. * @see #userRatingVector(Collection) */ public static Long2DoubleMap userRatingVector(@WillClose ObjectStream<? extends Rating> ratings) { return extractVector(ratings.iterator(), IdExtractor.ITEM, -1); }
@Override public void close() { delegate.close(); }
@Override public T readObject() { T obj = delegate.readObject(); while (obj != null && !filter.apply(obj)) { obj = delegate.readObject(); } return obj; } }