/** * Wrap an iterator in an object stream. * * The iterator may not contain `null`. This property is checked lazily; the object stream will not fail * until the `null` would be returned. * * @param <T> The type of data to return. * @param iterator An iterator to wrap * @return An object stream returning the elements of the iterator. */ public static <T> ObjectStream<T> wrap(Iterator<? extends T> iterator) { return new IteratorObjectStream<>(iterator); }
/** * Read an object stream into a list, closing when it is finished. * * @param <T> The type of item in the object stream. * @param objectStream The object stream. * @return A new list containing the elements of the object stream. * @deprecated Use {@link ObjectStream#collect(Collector)}. */ @SuppressWarnings("PMD.LooseCoupling") @Deprecated public static <T> List<T> makeList(@WillClose ObjectStream<? extends T> objectStream) { List<T> result = null; try { if (objectStream instanceof IteratorObjectStream) { result = ((IteratorObjectStream) objectStream).getList(); } if (result == null) { ImmutableList.Builder<T> builder = ImmutableList.builder(); builder.addAll(objectStream); result = builder.build(); } } finally { objectStream.close(); } return result; }
/** * 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(); } }
/** * Wrap a collection in an object stream. * * The iterator may not contain `null`. This property is checked lazily; the object stream will not fail * until the `null` would be returned. * * @param <T> The type of data to return. * @param collection A collection to wrap * @return An object stream returning the elements of the collection. */ public static <T> ObjectStream<T> wrap(Collection<? extends T> collection) { return new IteratorObjectStream<>(collection); }
/** * Read an object stream into a list, closing when it is finished. * * @param <T> The type of item in the object stream. * @param objectStream The object stream. * @return A new list containing the elements of the object stream. * @deprecated Use {@link ObjectStream#collect(Collector)}. */ @SuppressWarnings("PMD.LooseCoupling") @Deprecated public static <T> List<T> makeList(@WillClose ObjectStream<? extends T> objectStream) { List<T> result = null; try { if (objectStream instanceof IteratorObjectStream) { result = ((IteratorObjectStream) objectStream).getList(); } if (result == null) { ImmutableList.Builder<T> builder = ImmutableList.builder(); builder.addAll(objectStream); result = builder.build(); } } finally { objectStream.close(); } return result; }
/** * Wrap a Java stream in an object stream. * * The stream may not contain `null`. This property is checked lazily; the object stream will not fail * until the `null` would be returned. * * @param <T> The type of data to return. * @param stream A stream to wrap * @return An object stream returning the elements of the stream. */ public static <T> ObjectStream<T> wrap(Stream<? extends T> stream) { return new IteratorObjectStream<>(stream.iterator()); }
/** * 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(); } }
/** * Wrap a Java stream in an object stream. * * The stream may not contain `null`. This property is checked lazily; the object stream will not fail * until the `null` would be returned. * * @param <T> The type of data to return. * @param stream A stream to wrap * @param root The 'root stream', which will be closed when the resulting stream is closed. * @return An object stream returning the elements of the stream. */ public static <T> ObjectStream<T> wrap(Stream<? extends T> stream, @WillCloseWhenClosed Closeable root) { return new IteratorObjectStream<>(stream.iterator(), root); }
/** * Wrap an iterator in an object stream. * * The iterator may not contain `null`. This property is checked lazily; the object stream will not fail * until the `null` would be returned. * * @param <T> The type of data to return. * @param iterator An iterator to wrap * @return An object stream returning the elements of the iterator. */ public static <T> ObjectStream<T> wrap(Iterator<? extends T> iterator) { return new IteratorObjectStream<>(iterator); }
/** * Wrap a collection in an object stream. * * The iterator may not contain `null`. This property is checked lazily; the object stream will not fail * until the `null` would be returned. * * @param <T> The type of data to return. * @param collection A collection to wrap * @return An object stream returning the elements of the collection. */ public static <T> ObjectStream<T> wrap(Collection<? extends T> collection) { return new IteratorObjectStream<>(collection); }
/** * Wrap a Java stream in an object stream. * * The stream may not contain `null`. This property is checked lazily; the object stream will not fail * until the `null` would be returned. * * @param <T> The type of data to return. * @param stream A stream to wrap * @return An object stream returning the elements of the stream. */ public static <T> ObjectStream<T> wrap(Stream<? extends T> stream) { return new IteratorObjectStream<>(stream.iterator()); }
/** * Wrap a Java stream in an object stream. * * The stream may not contain `null`. This property is checked lazily; the object stream will not fail * until the `null` would be returned. * * @param <T> The type of data to return. * @param stream A stream to wrap * @param root The 'root stream', which will be closed when the resulting stream is closed. * @return An object stream returning the elements of the stream. */ public static <T> ObjectStream<T> wrap(Stream<? extends T> stream, @WillCloseWhenClosed Closeable root) { return new IteratorObjectStream<>(stream.iterator(), root); }