IOSpliterator.java

  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. package org.apache.commons.io.function;

  18. import java.io.IOException;
  19. import java.io.UncheckedIOException;
  20. import java.util.Objects;
  21. import java.util.Spliterator;
  22. import java.util.function.Consumer;

  23. /**
  24.  * Like {@link Spliterator} but throws {@link IOException}.
  25.  *
  26.  * @param <T> the type of elements returned by this IOSpliterator.
  27.  * @since 2.12.0
  28.  */
  29. public interface IOSpliterator<T> {

  30.     /**
  31.      * Adapts the given Spliterator as an IOSpliterator.
  32.      *
  33.      * @param <E> the type of the stream elements.
  34.      * @param iterator The iterator to adapt
  35.      * @return A new IOSpliterator
  36.      */
  37.     static <E> IOSpliterator<E> adapt(final Spliterator<E> iterator) {
  38.         return IOSpliteratorAdapter.adapt(iterator);
  39.     }

  40.     /**
  41.      * Constructs a {@link Spliterator} for this instance that throws {@link UncheckedIOException} instead of
  42.      * {@link IOException}.
  43.      *
  44.      * @return an {@link UncheckedIOException} {@link Spliterator}.
  45.      */
  46.     default Spliterator<T> asSpliterator() {
  47.         return new UncheckedIOSpliterator<>(this);
  48.     }

  49.     /**
  50.      * Like {@link Spliterator#characteristics()}.
  51.      *
  52.      * @return a representation of characteristics
  53.      */
  54.     default int characteristics() {
  55.         return unwrap().characteristics();
  56.     }

  57.     /**
  58.      * Like {@link Spliterator#estimateSize()}.
  59.      *
  60.      *
  61.      * @return the estimated size, or {@code Long.MAX_VALUE} if infinite, unknown, or too expensive to compute.
  62.      */
  63.     default long estimateSize() {
  64.         return unwrap().estimateSize();
  65.     }

  66.     /**
  67.      * Like {@link Spliterator#forEachRemaining(Consumer)}.
  68.      *
  69.      * @param action The action
  70.      * @throws NullPointerException if the specified action is null
  71.      */
  72.     default void forEachRemaining(final IOConsumer<? super T> action) {
  73.         while (tryAdvance(action)) { // NOPMD
  74.         }
  75.     }

  76.     /**
  77.      * Like {@link Spliterator#getComparator()}.
  78.      *
  79.      * @return a Comparator, or {@code null} if the elements are sorted in the natural order.
  80.      * @throws IllegalStateException if the spliterator does not report a characteristic of {@code SORTED}.
  81.      */
  82.     @SuppressWarnings("unchecked")
  83.     default IOComparator<? super T> getComparator() {
  84.         return (IOComparator<T>) unwrap().getComparator();
  85.     }

  86.     /**
  87.      * Like {@link Spliterator#getExactSizeIfKnown()}.
  88.      *
  89.      * @return the exact size, if known, else {@code -1}.
  90.      */
  91.     default long getExactSizeIfKnown() {
  92.         return unwrap().getExactSizeIfKnown();
  93.     }

  94.     /**
  95.      * Like {@link Spliterator#hasCharacteristics(int)}.
  96.      *
  97.      * @param characteristics the characteristics to check for
  98.      * @return {@code true} if all the specified characteristics are present, else {@code false}
  99.      */
  100.     default boolean hasCharacteristics(final int characteristics) {
  101.         return unwrap().hasCharacteristics(characteristics);
  102.     }

  103.     /**
  104.      * Like {@link Spliterator#tryAdvance(Consumer)}.
  105.      *
  106.      * @param action The action
  107.      * @return {@code false} if no remaining elements existed upon entry to this method, else {@code true}.
  108.      * @throws NullPointerException if the specified action is null
  109.      */
  110.     default boolean tryAdvance(final IOConsumer<? super T> action) {
  111.         return unwrap().tryAdvance(Objects.requireNonNull(action, "action").asConsumer());
  112.     }

  113.     /**
  114.      * Like {@link Spliterator#trySplit()}.
  115.      *
  116.      * @return a {@code Spliterator} covering some portion of the elements, or {@code null} if this spliterator cannot be
  117.      *         split
  118.      */
  119.     default IOSpliterator<T> trySplit() {
  120.         return adapt(unwrap().trySplit());
  121.     }

  122.     /**
  123.      * Unwraps this instance and returns the underlying {@link Spliterator}.
  124.      * <p>
  125.      * Implementations may not have anything to unwrap and that behavior is undefined for now.
  126.      * </p>
  127.      *
  128.      * @return the underlying Spliterator.
  129.      */
  130.     Spliterator<T> unwrap();

  131. }