View Javadoc
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  
18  package org.apache.commons.io.function;
19  
20  import java.io.IOException;
21  import java.io.UncheckedIOException;
22  import java.util.Iterator;
23  import java.util.Spliterator;
24  import java.util.stream.BaseStream;
25  
26  /**
27   * An {@link BaseStream} for a {@link IOBaseStream} that throws {@link UncheckedIOException} instead of
28   * {@link IOException}.
29   *
30   * Keep package-private for now.
31   *
32   * @param <T> the type of the stream elements.
33   * @param <S> the type of the IO stream extending {@code IOBaseStream}.
34   * @param <B> the type of the stream extending {@code BaseStream}.
35   */
36  final class UncheckedIOBaseStream<T, S extends IOBaseStream<T, S, B>, B extends BaseStream<T, B>> implements BaseStream<T, B> {
37  
38      private final S delegate;
39  
40      UncheckedIOBaseStream(final S delegate) {
41          this.delegate = delegate;
42      }
43  
44      @Override
45      public void close() {
46          delegate.close();
47      }
48  
49      @Override
50      public boolean isParallel() {
51          return delegate.isParallel();
52      }
53  
54      @Override
55      public Iterator<T> iterator() {
56          return delegate.iterator().asIterator();
57      }
58  
59      @SuppressWarnings("resource")
60      @Override
61      public B onClose(final Runnable closeHandler) {
62          return Uncheck.apply(delegate::onClose, () -> closeHandler.run()).unwrap();
63      }
64  
65      @SuppressWarnings("resource")
66      @Override
67      public B parallel() {
68          return delegate.parallel().unwrap();
69      }
70  
71      @SuppressWarnings("resource")
72      @Override
73      public B sequential() {
74          return delegate.sequential().unwrap();
75      }
76  
77      @Override
78      public Spliterator<T> spliterator() {
79          return delegate.spliterator().unwrap();
80      }
81  
82      @SuppressWarnings("resource")
83      @Override
84      public B unordered() {
85          return delegate.unordered().unwrap();
86      }
87  
88  }