View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.base;
33  
34  import edu.umd.cs.findbugs.annotations.CleanupObligation;
35  import edu.umd.cs.findbugs.annotations.DischargesObligation;
36  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
37  import java.io.Closeable;
38  import java.util.Iterator;
39  import java.util.Spliterators;
40  import java.util.stream.Stream;
41  import java.util.stream.StreamSupport;
42  
43  /**
44   * need to extend java.io.Closeable so that jaxrs does not interfere.
45   * @author Zoltan Farkas
46   */
47  @CleanupObligation
48  public interface CloseableIterator<T> extends Iterator<T>, Closeable {
49  
50    @DischargesObligation
51    void close();
52  
53    static <T> CloseableIterator<T> from(final Iterator<T> it) {
54      return from(it, () -> { });
55    }
56  
57    static <T> CloseableIterator<T> from(final Iterator<T> it, final AutoCloseable close) {
58      return new CloseableIterator<T>() {
59        @Override
60        @SuppressFBWarnings("EXS_EXCEPTION_SOFTENING_NO_CHECKED")
61        public void close() {
62          try {
63            close.close();
64          } catch (RuntimeException | Error e) {
65            throw e;
66          } catch (Exception ex) {
67            throw new RuntimeException(ex);
68          }
69        }
70  
71        @Override
72        public boolean hasNext() {
73          return it.hasNext();
74        }
75  
76        @Override
77        public T next() {
78          return it.next();
79        }
80  
81        @Override
82        public void remove() {
83          it.remove();
84        }
85  
86      };
87    }
88  
89    default Stream<T> toStreamRemaining() {
90      return toStreamRemaining(false);
91    }
92  
93    default Stream<T> toStreamRemaining(final boolean parallel) {
94      return StreamSupport.stream(Spliterators.spliteratorUnknownSize(this, 0), parallel).onClose(() -> this.close());
95    }
96  
97  }