diff options
author | Gary Gregory <gardgregory@gmail.com> | 2022-09-12 17:10:56 -0700 |
---|---|---|
committer | Gary Gregory <gardgregory@gmail.com> | 2022-09-12 17:10:56 -0700 |
commit | 93228aa10a06b6cb3f38c18aecd0c5eea867a16c (patch) | |
tree | 0be5ee0c4e04cab2b17960862e6d07026c65d434 /src/main/java/org | |
parent | 63af628e71da85841b48cddbc2ae83d180a040d6 (diff) | |
download | apache-commons-io-93228aa10a06b6cb3f38c18aecd0c5eea867a16c.tar.gz |
Add IOSpliterator
Diffstat (limited to 'src/main/java/org')
-rw-r--r-- | src/main/java/org/apache/commons/io/function/IOSpliterator.java | 146 | ||||
-rw-r--r-- | src/main/java/org/apache/commons/io/function/IOSpliteratorAdapter.java | 45 |
2 files changed, 191 insertions, 0 deletions
diff --git a/src/main/java/org/apache/commons/io/function/IOSpliterator.java b/src/main/java/org/apache/commons/io/function/IOSpliterator.java new file mode 100644 index 00000000..550f13a4 --- /dev/null +++ b/src/main/java/org/apache/commons/io/function/IOSpliterator.java @@ -0,0 +1,146 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.io.function; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.Objects; +import java.util.Spliterator; +import java.util.function.Consumer; + +/** + * Like {@link Spliterator} but throws {@link IOException}. + * + * @param <T> the type of elements returned by this IOSpliterator. + * @since 2.12.0 + */ +public interface IOSpliterator<T> { + + /** + * Adapts the given Spliterator as an IOSpliterator. + * + * @param <E> the type of the stream elements. + * @param iterator The iterator to adapt + * @return A new IOSpliterator + */ + static <E> IOSpliterator<E> adapt(final Spliterator<E> iterator) { + return IOSpliteratorAdapter.adapt(iterator); + } + + /** + * Creates a {@link Spliterator} for this instance that throws {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @return an {@link UncheckedIOException} {@link Spliterator}. + */ + default Spliterator<T> asSpliterator() { + return new UncheckedIOSpliterator<>(this); + } + + /** + * Like {@link Spliterator#characteristics()}. + * + * @return a representation of characteristics + */ + default int characteristics() { + return unwrap().characteristics(); + } + + /** + * Like {@link Spliterator#estimateSize()}. + * + * + * @return the estimated size, or {@code Long.MAX_VALUE} if infinite, unknown, or too expensive to compute. + */ + default long estimateSize() { + return unwrap().estimateSize(); + } + + /** + * Like {@link Spliterator#forEachRemaining(Consumer)}. + * + * @param action The action + * @throws NullPointerException if the specified action is null + */ + default void forEachRemaining(final IOConsumer<? super T> action) { + while (tryAdvance(action)) { // NOPMD + } + } + + /** + * Like {@link Spliterator#getComparator()}. + * + * @return a Comparator, or {@code null} if the elements are sorted in the natural order. + * @throws IllegalStateException if the spliterator does not report a characteristic of {@code SORTED}. + */ + @SuppressWarnings("unchecked") + default IOComparator<? super T> getComparator() { + return (IOComparator<T>) unwrap().getComparator(); + } + + /** + * Like {@link Spliterator#getExactSizeIfKnown()}. + * + * @return the exact size, if known, else {@code -1}. + */ + default long getExactSizeIfKnown() { + return unwrap().getExactSizeIfKnown(); + } + + /** + * Like {@link Spliterator#hasCharacteristics(int)}. + * + * @param characteristics the characteristics to check for + * @return {@code true} if all the specified characteristics are present, else {@code false} + */ + default boolean hasCharacteristics(final int characteristics) { + return unwrap().hasCharacteristics(characteristics); + } + + /** + * Like {@link Spliterator#tryAdvance(Consumer)}. + * + * @param action The action + * @return {@code false} if no remaining elements existed upon entry to this method, else {@code true}. + * @throws NullPointerException if the specified action is null + */ + default boolean tryAdvance(IOConsumer<? super T> action) { + return unwrap().tryAdvance(Objects.requireNonNull(action, "action").asConsumer()); + } + + /** + * Like {@link Spliterator#trySplit()}. + * + * @return a {@code Spliterator} covering some portion of the elements, or {@code null} if this spliterator cannot be + * split + */ + default IOSpliterator<T> trySplit() { + return adapt(unwrap().trySplit()); + } + + /** + * Unwraps this instance and returns the underlying {@link Spliterator}. + * <p> + * Implementations may not have anything to unwrap and that behavior is undefined for now. + * </p> + * + * @return the underlying Spliterator. + */ + Spliterator<T> unwrap(); + +} diff --git a/src/main/java/org/apache/commons/io/function/IOSpliteratorAdapter.java b/src/main/java/org/apache/commons/io/function/IOSpliteratorAdapter.java new file mode 100644 index 00000000..8c3101c1 --- /dev/null +++ b/src/main/java/org/apache/commons/io/function/IOSpliteratorAdapter.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.io.function; + +import java.util.Objects; +import java.util.Spliterator; + +/** + * Adapts an {@link Spliterator} as an {@link IOSpliterator}. + * + * @param <T> the type of the stream elements. + */ +final class IOSpliteratorAdapter<T> implements IOSpliterator<T> { + + static <E> IOSpliteratorAdapter<E> adapt(final Spliterator<E> delegate) { + return new IOSpliteratorAdapter<>(delegate); + } + + private final Spliterator<T> delegate; + + IOSpliteratorAdapter(final Spliterator<T> delegate) { + this.delegate = Objects.requireNonNull(delegate, "delegate"); + } + + @Override + public Spliterator<T> unwrap() { + return delegate; + } + +} |