|
| 1 | +/* |
| 2 | + * Copyright 2017 National Bank of Belgium |
| 3 | + * |
| 4 | + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved |
| 5 | + * by the European Commission - subsequent versions of the EUPL (the "Licence"); |
| 6 | + * You may not use this work except in compliance with the Licence. |
| 7 | + * You may obtain a copy of the Licence at: |
| 8 | + * |
| 9 | + * http://ec.europa.eu/idabc/eupl |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the Licence is distributed on an "AS IS" basis, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the Licence for the specific language governing permissions and |
| 15 | + * limitations under the Licence. |
| 16 | + */ |
| 17 | +package internal.io; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.UncheckedIOException; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Iterator; |
| 23 | +import java.util.NoSuchElementException; |
| 24 | +import java.util.Spliterators; |
| 25 | +import java.util.function.Consumer; |
| 26 | +import java.util.stream.Stream; |
| 27 | +import java.util.stream.StreamSupport; |
| 28 | +import nbbrd.io.IOIterator; |
| 29 | +import nbbrd.io.function.IOConsumer; |
| 30 | +import nbbrd.io.function.IOPredicate; |
| 31 | +import nbbrd.io.function.IOSupplier; |
| 32 | +import nbbrd.io.function.IOUnaryOperator; |
| 33 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 34 | + |
| 35 | +/** |
| 36 | + * |
| 37 | + * @author Philippe Charles |
| 38 | + */ |
| 39 | +@lombok.experimental.UtilityClass |
| 40 | +public class IOIterators { |
| 41 | + |
| 42 | + public enum Empty implements IOIterator<Object> { |
| 43 | + |
| 44 | + INSTANCE; |
| 45 | + |
| 46 | + @Override |
| 47 | + public boolean hasNextWithIO() throws IOException { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Object nextWithIO() throws IOException, NoSuchElementException { |
| 53 | + throw new NoSuchElementException(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Stream<Object> asStream() { |
| 58 | + return Stream.empty(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Iterator<Object> asUnchecked() { |
| 63 | + return Collections.emptyIterator(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @lombok.RequiredArgsConstructor |
| 68 | + public static final class Singleton<E> implements IOIterator<E> { |
| 69 | + |
| 70 | + @Nullable |
| 71 | + private final E element; |
| 72 | + |
| 73 | + private boolean first = true; |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean hasNextWithIO() throws IOException { |
| 77 | + return first; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public E nextWithIO() throws IOException, NoSuchElementException { |
| 82 | + if (!hasNextWithIO()) { |
| 83 | + throw new NoSuchElementException(); |
| 84 | + } |
| 85 | + first = false; |
| 86 | + return element; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @lombok.RequiredArgsConstructor |
| 91 | + public static final class Checked<E> implements IOIterator<E> { |
| 92 | + |
| 93 | + @lombok.Getter |
| 94 | + @lombok.NonNull |
| 95 | + private final Iterator<E> delegate; |
| 96 | + |
| 97 | + @Override |
| 98 | + public boolean hasNextWithIO() throws IOException { |
| 99 | + try { |
| 100 | + return delegate.hasNext(); |
| 101 | + } catch (UncheckedIOException ex) { |
| 102 | + throw ex.getCause(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public E nextWithIO() throws IOException, NoSuchElementException { |
| 108 | + try { |
| 109 | + return delegate.next(); |
| 110 | + } catch (UncheckedIOException ex) { |
| 111 | + throw ex.getCause(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void removeWithIO() throws IOException { |
| 117 | + try { |
| 118 | + delegate.remove(); |
| 119 | + } catch (UncheckedIOException ex) { |
| 120 | + throw ex.getCause(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void forEachRemainingWithIO(IOConsumer<? super E> action) throws IOException { |
| 126 | + try { |
| 127 | + delegate.forEachRemaining(action.asUnchecked()); |
| 128 | + } catch (UncheckedIOException ex) { |
| 129 | + throw ex.getCause(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public Stream<E> asStream() { |
| 135 | + return StreamSupport.stream(Spliterators.spliteratorUnknownSize(delegate, 0), false); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public Iterator<E> asUnchecked() { |
| 140 | + return delegate; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @lombok.RequiredArgsConstructor |
| 145 | + public static final class Unchecked<E> implements Iterator<E> { |
| 146 | + |
| 147 | + @lombok.Getter |
| 148 | + @lombok.NonNull |
| 149 | + private final IOIterator<E> delegate; |
| 150 | + |
| 151 | + @Override |
| 152 | + public boolean hasNext() { |
| 153 | + try { |
| 154 | + return delegate.hasNextWithIO(); |
| 155 | + } catch (IOException ex) { |
| 156 | + throw new UncheckedIOException(ex); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public E next() { |
| 162 | + try { |
| 163 | + return delegate.nextWithIO(); |
| 164 | + } catch (IOException ex) { |
| 165 | + throw new UncheckedIOException(ex); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void remove() { |
| 171 | + try { |
| 172 | + delegate.removeWithIO(); |
| 173 | + } catch (IOException ex) { |
| 174 | + throw new UncheckedIOException(ex); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public void forEachRemaining(Consumer<? super E> action) { |
| 180 | + try { |
| 181 | + delegate.forEachRemainingWithIO(IOConsumer.checked(action)); |
| 182 | + } catch (IOException ex) { |
| 183 | + throw new UncheckedIOException(ex); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + @lombok.RequiredArgsConstructor |
| 189 | + public static final class Functional<E> implements IOIterator<E> { |
| 190 | + |
| 191 | + @lombok.NonNull |
| 192 | + private final IOSupplier<E> seed; |
| 193 | + |
| 194 | + @lombok.NonNull |
| 195 | + private final IOPredicate<? super E> hasNext; |
| 196 | + |
| 197 | + @lombok.NonNull |
| 198 | + private final IOUnaryOperator<E> next; |
| 199 | + |
| 200 | + private boolean seeded = false; |
| 201 | + private E nextValue = null; |
| 202 | + |
| 203 | + @Override |
| 204 | + public boolean hasNextWithIO() throws IOException { |
| 205 | + if (!seeded) { |
| 206 | + seeded = true; |
| 207 | + nextValue = seed.getWithIO(); |
| 208 | + } |
| 209 | + return hasNext.testWithIO(nextValue); |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public E nextWithIO() throws IOException, NoSuchElementException { |
| 214 | + if (!hasNextWithIO()) { |
| 215 | + throw new NoSuchElementException(); |
| 216 | + } |
| 217 | + E result = nextValue; |
| 218 | + nextValue = next.applyWithIO(nextValue); |
| 219 | + return result; |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments