    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 * http://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019
    020package org.apache.commons.compress.utils;
    021
    022import java.util.Collection;
    023import java.util.Iterator;
    024import java.util.Objects;
    025
    026/**
    027 * Iterator utilities.
    028 *
    029 * @since 1.13.
    030 */
    031public class Iterators {
    032
    033    /**
    034     * Adds all the elements in the source {@code iterator} to the target
    035     * {@code collection}.
    036     *
    037     * <p>
    038     * When this method returns, the {@code iterator} will be "empty": its
    039     * {@code hasNext()} method returns {@code false}.
    040     * </p>
    041     *
    042     * @param <T> type of the elements contained inside the collection
    043     * @param collection target collection
    044     * @param iterator source
    045     * @return {@code true} if the target {@code collection} was modified as a
    046     *         result of this operation
    047     */
    048    public static <T> boolean addAll(final Collection<T> collection, final Iterator<? extends T> iterator) {
    049        Objects.requireNonNull(collection);
    050        Objects.requireNonNull(iterator);
    051        boolean wasModified = false;
    052        while (iterator.hasNext()) {
    053            wasModified |= collection.add(iterator.next());
    054        }
    055        return wasModified;
    056    }
    057
    058    private Iterators() {
    059        // do not instantiate
    060    }
    061
    062}