@Override public ObjectStream<Entity> openStream() throws IOException { return ObjectStreams.wrap(entities); }
/** * Create an empty object stream. * * @param <T> The type of value in the object stream. * @return An empty object stream. */ public static <T> ObjectStream<T> empty() { return wrap(Collections.<T>emptyList()); }
/** * Create an object stream over a fixed set of elements. This is mostly useful for testing. * @param contents The contents. * @param <T> The data type. * @return The object stream. */ @SafeVarargs @SuppressWarnings("varargs") // method is safe MDE 2015-05-08 public static <T> ObjectStream<T> of(T... contents) { return wrap(Arrays.asList(contents)); }
@Override public ObjectStream<Entity> streamEntities(EntityType type) { Iterable<Entity> data = storage.get(type); if (data != null) { return ObjectStreams.wrap(data.iterator()); } else { return ObjectStreams.empty(); } }
public FirstLetterObjectStream(Iterable<String> vals) { this(ObjectStreams.wrap(vals.iterator())); }
/** * Sort an object stream. This reads the original object stream into a list, sorts it, and * returns a new object stream backed by the list (after closing the original object stream). * * @param <T> The type of value in the object stream. * @param objectStream The object stream to sort. * @param comp The comparator to use to sort the object stream. * @return An object stream iterating over the sorted results. * @deprecated Use {@link ObjectStream#sorted(Comparator)}, possibly with {@link #wrap(Stream, Closeable)}. */ @Deprecated public static <T> ObjectStream<T> sort(@WillClose ObjectStream<T> objectStream, Comparator<? super T> comp) { ArrayList<T> list; try { list = Lists.newArrayList(objectStream); } finally { objectStream.close(); } Collections.sort(list, comp); return wrap(list); }
@Test public void testWrapIterator() { ObjectStream<String> stream = ObjectStreams.wrap(Lists.newArrayList("foo", "bar").iterator()); try { // since collection wrapping tested general capabilities, these tests will be terser assertThat(stream, contains("foo", "bar")); } finally { stream.close(); } } //endregion
List<Attribute<?>> filters = query.getFilterFields(); if (filters.isEmpty()) { baseStream = ObjectStreams.wrap(data); } else { baseStream = ObjectStreams.wrap(data.find(f1)); if (filters.size() > 1) { baseStream = ObjectStreams.filter(baseStream, query); assert ord != null; try { return ObjectStreams.wrap(ord.immutableSortedCopy(stream)); } finally { stream.close();
@Test public void testMapToDouble() { List<Rating> ratings = makeRatings(); double correct = ratings.stream() .mapToDouble(Rating::getValue) .sum(); double result = ObjectStreams.wrap(ratings) .mapToDouble(Rating::getValue) .sum(); assertThat(result, equalTo(correct)); }
@Test public void testMapToLong() { List<Rating> ratings = makeRatings(); long correct = ratings.stream() .mapToLong(Rating::getUserId) .sum(); long result = ObjectStreams.wrap(ratings) .mapToLong(Rating::getUserId) .sum(); assertThat(result, equalTo(correct)); }
@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 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)); }
@Test public void testMakeListOfList() { // same test as previous, but with a stream with a known length String[] strings = { "READ ME", "ZELGO NER", "HACKEM MUCHE" }; List<String> lst = ObjectStreams.makeList(ObjectStreams.wrap(Arrays.asList(strings))); assertThat(lst, hasSize(3)); assertThat(lst, contains(strings)); } //endregion
@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(); } }
@Test public void testMapToInt() { List<Rating> ratings = makeRatings(); int correct = ratings.stream() .mapToInt(r -> (int) r.getUserId()) .sum(); int result = ObjectStreams.wrap(ratings) .mapToInt(r -> (int) r.getUserId()) .sum(); assertThat(result, equalTo(correct)); }
@Test public void testMakeListOfIterator() { String[] strings = { "READ ME", "ZELGO NER", "HACKEM MUCHE" }; // go through an iteator so the stream doesn't know its length List<String> lst = ObjectStreams.makeList(ObjectStreams.wrap(Iterators.forArray(strings))); assertThat(lst, hasSize(3)); assertThat(lst, contains(strings)); }
@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 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(); } }
/** * Create an empty object stream. * * @param <T> The type of value in the object stream. * @return An empty object stream. */ public static <T> ObjectStream<T> empty() { return wrap(Collections.<T>emptyList()); }
@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); } }