IOBaseStream.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.io.function;

  18. import java.io.Closeable;
  19. import java.io.IOException;
  20. import java.io.UncheckedIOException;
  21. import java.util.stream.BaseStream;
  22. import java.util.stream.Stream;

  23. /**
  24.  * Like {@link BaseStream} but throws {@link IOException}.
  25.  *
  26.  * @param <T> the type of the stream elements.
  27.  * @param <S> the type of the IO stream extending {@code IOBaseStream}.
  28.  * @param <B> the type of the stream extending {@code BaseStream}.
  29.  * @since 2.12.0
  30.  */
  31. public interface IOBaseStream<T, S extends IOBaseStream<T, S, B>, B extends BaseStream<T, B>> extends Closeable {

  32.     /**
  33.      * Constructs a {@link BaseStream} for this instance that throws {@link UncheckedIOException} instead of
  34.      * {@link IOException}.
  35.      *
  36.      * @return an {@link UncheckedIOException} {@link BaseStream}.
  37.      */
  38.     @SuppressWarnings("unchecked")
  39.     default BaseStream<T, B> asBaseStream() {
  40.         return new UncheckedIOBaseStream<>((S) this);
  41.     }

  42.     /**
  43.      * Like {@link BaseStream#close()}.
  44.      *
  45.      * @see BaseStream#close()
  46.      */
  47.     @Override
  48.     default void close() {
  49.         unwrap().close();
  50.     }

  51.     /**
  52.      * Like {@link BaseStream#isParallel()}.
  53.      *
  54.      * @return See {@link BaseStream#isParallel() delegate}.
  55.      * @see BaseStream#isParallel()
  56.      */
  57.     @SuppressWarnings("resource") // for unwrap()
  58.     default boolean isParallel() {
  59.         return unwrap().isParallel();
  60.     }

  61.     /**
  62.      * Like {@link BaseStream#iterator()}.
  63.      *
  64.      * @return See {@link BaseStream#iterator() delegate}.
  65.      * @see BaseStream#iterator()
  66.      */
  67.     @SuppressWarnings("resource") // for unwrap()
  68.     default IOIterator<T> iterator() {
  69.         return IOIteratorAdapter.adapt(unwrap().iterator());
  70.     }

  71.     /**
  72.      * Like {@link BaseStream#onClose(Runnable)}.
  73.      *
  74.      * @param closeHandler See {@link BaseStream#onClose(Runnable) delegate}.
  75.      * @return See {@link BaseStream#onClose(Runnable) delegate}.
  76.      * @throws IOException if an I/O error occurs.
  77.      * @see BaseStream#onClose(Runnable)
  78.      */
  79.     @SuppressWarnings({"unused", "resource"}) // throws IOException, unwrap()
  80.     default S onClose(final IORunnable closeHandler) throws IOException {
  81.         return wrap(unwrap().onClose(() -> Erase.run(closeHandler)));
  82.     }

  83.     /**
  84.      * Like {@link BaseStream#parallel()}.
  85.      *
  86.      * @return See {@link BaseStream#parallel() delegate}.
  87.      * @see BaseStream#parallel()
  88.      */
  89.     @SuppressWarnings({"resource", "unchecked"}) // for unwrap(), this
  90.     default S parallel() {
  91.         return isParallel() ? (S) this : wrap(unwrap().parallel());
  92.     }

  93.     /**
  94.      * Like {@link BaseStream#sequential()}.
  95.      *
  96.      * @return See {@link BaseStream#sequential() delegate}.
  97.      * @see BaseStream#sequential()
  98.      */
  99.     @SuppressWarnings({"resource", "unchecked"}) // for unwrap(), this
  100.     default S sequential() {
  101.         return isParallel() ? wrap(unwrap().sequential()) : (S) this;
  102.     }

  103.     /**
  104.      * Like {@link BaseStream#spliterator()}.
  105.      *
  106.      * @return See {@link BaseStream#spliterator() delegate}.
  107.      * @see BaseStream#spliterator()
  108.      */
  109.     @SuppressWarnings("resource") // for unwrap()
  110.     default IOSpliterator<T> spliterator() {
  111.         return IOSpliteratorAdapter.adapt(unwrap().spliterator());
  112.     }

  113.     /**
  114.      * Like {@link BaseStream#unordered()}.
  115.      *
  116.      * @return See {@link BaseStream#unordered() delegate}.
  117.      * @see java.util.stream.BaseStream#unordered()
  118.      */
  119.     @SuppressWarnings("resource") // for unwrap()
  120.     default S unordered() {
  121.         return wrap(unwrap().unordered());
  122.     }

  123.     /**
  124.      * Unwraps this instance and returns the underlying {@link Stream}.
  125.      * <p>
  126.      * Implementations may not have anything to unwrap and that behavior is undefined for now.
  127.      * </p>
  128.      *
  129.      * @return the underlying stream.
  130.      */
  131.     B unwrap();

  132.     /**
  133.      * Wraps a {@link Stream}.
  134.      *
  135.      * @param delegate The delegate.
  136.      * @return An IO stream.
  137.      */
  138.     S wrap(B delegate);

  139. }