IteratorOperations.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.collections4.iterators;

  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.HashSet;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Set;
  24. import java.util.function.Supplier;

  25. /**
  26.  * Extends {@link Iterator} with additional default methods.
  27.  *
  28.  * @param <E> the type of elements returned by this iterator.
  29.  * @since 4.5.0-M3
  30.  */
  31. public interface IteratorOperations<E> extends Iterator<E> {

  32.     /**
  33.      * Adds the remaining elements in the iterator to an arbitrary {@link Collection}. This method consumes the iterator.
  34.      *
  35.      * @param collection The target collection to add elements to.
  36.      * @return the given {@code collection}.
  37.      * @param <C> A collection of objects of type {@code <E>}.
  38.      */
  39.     default <C extends Collection<E>> C addTo(final C collection) {
  40.         forEachRemaining(collection::add);
  41.         return collection;
  42.     }

  43.     /**
  44.      * Returns the next item and removes it from the iterator.
  45.      *
  46.      * @return the next item from the iterator.
  47.      */
  48.     default E removeNext() {
  49.         final E result = next();
  50.         remove();
  51.         return result;
  52.     }

  53.     /**
  54.      * Adds the remaining elements in the iterator to a new {@link Collection} provided by the supplier. This method consumes the iterator.
  55.      *
  56.      * @param collectionSupplier supplies a collection target.
  57.      * @param <C> the collection type.
  58.      * @return a new Collection containing the remaining elements of this instance.
  59.      */
  60.     default <C extends Collection<E>> C toCollection(final Supplier<C> collectionSupplier) {
  61.         return addTo(collectionSupplier.get());
  62.     }

  63.     /**
  64.      * Adds the remaining elements in the iterator to a new {@link List}. This method consumes the iterator.
  65.      *
  66.      * @return a new List containing the remaining elements of this instance.
  67.      */
  68.     default List<E> toList() {
  69.         return toCollection(ArrayList::new);
  70.     }

  71.     /**
  72.      * Adds the remaining elements in the iterator to a new {@link Set}. This method consumes the iterator.
  73.      *
  74.      * @return a new Set containing the remaining elements of this instance.
  75.      */
  76.     default Set<E> toSet() {
  77.         return toCollection(HashSet::new);
  78.     }

  79. }