| 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 | * https://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 | |
| 18 | package org.apache.commons.io.function; |
| 19 | |
| 20 | import java.io.Closeable; |
| 21 | import java.io.IOException; |
| 22 | import java.io.UncheckedIOException; |
| 23 | import java.util.stream.BaseStream; |
| 24 | import java.util.stream.Stream; |
| 25 | |
| 26 | /** |
| 27 | * Like {@link BaseStream} but throws {@link IOException}. |
| 28 | * |
| 29 | * @param <T> the type of the stream elements. |
| 30 | * @param <S> the type of the IO stream extending {@code IOBaseStream}. |
| 31 | * @param <B> the type of the stream extending {@code BaseStream}. |
| 32 | * @since 2.12.0 |
| 33 | */ |
| 34 | public interface IOBaseStream<T, S extends IOBaseStream<T, S, B>, B extends BaseStream<T, B>> extends Closeable { |
| 35 | |
| 36 | /** |
| 37 | * Constructs a {@link BaseStream} for this instance that throws {@link UncheckedIOException} instead of |
| 38 | * {@link IOException}. |
| 39 | * |
| 40 | * @return an {@link UncheckedIOException} {@link BaseStream}. |
| 41 | */ |
| 42 | @SuppressWarnings("unchecked") |
| 43 | default BaseStream<T, B> asBaseStream() { |
| 44 | return new UncheckedIOBaseStream<>((S) this); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Like {@link BaseStream#close()}. |
| 49 | * |
| 50 | * @see BaseStream#close() |
| 51 | */ |
| 52 | @Override |
| 53 | default void close() { |
| 54 | unwrap().close(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Like {@link BaseStream#isParallel()}. |
| 59 | * |
| 60 | * @return See {@link BaseStream#isParallel() delegate}. |
| 61 | * @see BaseStream#isParallel() |
| 62 | */ |
| 63 | @SuppressWarnings("resource") // for unwrap() |
| 64 | default boolean isParallel() { |
| 65 | return unwrap().isParallel(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Like {@link BaseStream#iterator()}. |
| 70 | * |
| 71 | * @return See {@link BaseStream#iterator() delegate}. |
| 72 | * @see BaseStream#iterator() |
| 73 | */ |
| 74 | @SuppressWarnings("resource") // for unwrap() |
| 75 | default IOIterator<T> iterator() { |
| 76 | return IOIteratorAdapter.adapt(unwrap().iterator()); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Like {@link BaseStream#onClose(Runnable)}. |
| 81 | * |
| 82 | * @param closeHandler See {@link BaseStream#onClose(Runnable) delegate}. |
| 83 | * @return See {@link BaseStream#onClose(Runnable) delegate}. |
| 84 | * @throws IOException if an I/O error occurs. |
| 85 | * @see BaseStream#onClose(Runnable) |
| 86 | */ |
| 87 | @SuppressWarnings({"unused", "resource"}) // throws IOException, unwrap() |
| 88 | default S onClose(final IORunnable closeHandler) throws IOException { |
| 89 | return wrap(unwrap().onClose(() -> Erase.run(closeHandler))); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Like {@link BaseStream#parallel()}. |
| 94 | * |
| 95 | * @return See {@link BaseStream#parallel() delegate}. |
| 96 | * @see BaseStream#parallel() |
| 97 | */ |
| 98 | @SuppressWarnings({"resource", "unchecked"}) // for unwrap(), this |
| 99 | default S parallel() { |
| 100 | return isParallel() ? (S) this : wrap(unwrap().parallel()); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Like {@link BaseStream#sequential()}. |
| 105 | * |
| 106 | * @return See {@link BaseStream#sequential() delegate}. |
| 107 | * @see BaseStream#sequential() |
| 108 | */ |
| 109 | @SuppressWarnings({"resource", "unchecked"}) // for unwrap(), this |
| 110 | default S sequential() { |
| 111 | return isParallel() ? wrap(unwrap().sequential()) : (S) this; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Like {@link BaseStream#spliterator()}. |
| 116 | * |
| 117 | * @return See {@link BaseStream#spliterator() delegate}. |
| 118 | * @see BaseStream#spliterator() |
| 119 | */ |
| 120 | @SuppressWarnings("resource") // for unwrap() |
| 121 | default IOSpliterator<T> spliterator() { |
| 122 | return IOSpliteratorAdapter.adapt(unwrap().spliterator()); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Like {@link BaseStream#unordered()}. |
| 127 | * |
| 128 | * @return See {@link BaseStream#unordered() delegate}. |
| 129 | * @see java.util.stream.BaseStream#unordered() |
| 130 | */ |
| 131 | @SuppressWarnings("resource") // for unwrap() |
| 132 | default S unordered() { |
| 133 | return wrap(unwrap().unordered()); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Unwraps this instance and returns the underlying {@link Stream}. |
| 138 | * <p> |
| 139 | * Implementations may not have anything to unwrap and that behavior is undefined for now. |
| 140 | * </p> |
| 141 | * |
| 142 | * @return the underlying stream. |
| 143 | */ |
| 144 | B unwrap(); |
| 145 | |
| 146 | /** |
| 147 | * Wraps a {@link Stream}. |
| 148 | * |
| 149 | * @param delegate The delegate. |
| 150 | * @return An IO stream. |
| 151 | */ |
| 152 | S wrap(B delegate); |
| 153 | |
| 154 | } |