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 {@code collection}.
035     *
036     * <p>
037     * When this method returns, the {@code iterator} will be "empty": its {@code hasNext()} method returns {@code false}.
038     * </p>
039     *
040     * @param <T>        type of the elements contained inside the collection
041     * @param collection target collection
042     * @param iterator   source
043     * @return {@code true} if the target {@code collection} was modified as a result of this operation
044     */
045    public static <T> boolean addAll(final Collection<T> collection, final Iterator<? extends T> iterator) {
046        Objects.requireNonNull(collection);
047        Objects.requireNonNull(iterator);
048        boolean wasModified = false;
049        while (iterator.hasNext()) {
050            wasModified |= collection.add(iterator.next());
051        }
052        return wasModified;
053    }
054
055    private Iterators() {
056        // do not instantiate
057    }
058
059}