001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.internal;
014
015import static org.apache.juneau.internal.ThrowableUtils.*;
016
017import java.util.*;
018
019/**
020 * Utility class for defining an iterator over one or more iterables.
021 *
022 * @param <E> The element class type.
023 */
024public class MultiIterable<E> implements Iterable<E> {
025
026   final List<Iterator<E>> iterators = new LinkedList<>();
027
028   /**
029    * Constructor.
030    *
031    * @param iterators The list of iterators to iterate over.
032    */
033   @SuppressWarnings("unchecked")
034   public MultiIterable(Iterator<E>...iterators) {
035      for (Iterator<E> i : iterators)
036         append(i);
037   }
038
039   /**
040    * Appends the specified iterator to this list of iterators.
041    *
042    * @param iterator The iterator to append.
043    * @return This object (for method chaining).
044    */
045   public MultiIterable<E> append(Iterator<E> iterator) {
046      assertFieldNotNull(iterator, "iterator");
047      this.iterators.add(iterator);
048      return this;
049   }
050
051   @Override /* Iterable */
052   public Iterator<E> iterator() {
053      return new Iterator<E>() {
054         Iterator<Iterator<E>> i1 = iterators.iterator();
055         Iterator<E> i2 = i1.hasNext() ? i1.next() : null;
056
057         @Override /* Iterator */
058         public boolean hasNext() {
059            while (i2 != null && ! i2.hasNext())
060               i2 = (i1.hasNext() ? i1.next() : null);
061            return (i2 != null);
062         }
063
064         @Override /* Iterator */
065         public E next() {
066            hasNext();
067            if (i2 == null)
068               throw new NoSuchElementException();
069            return i2.next();
070         }
071
072         @Override /* Iterator */
073         public void remove() {
074            if (i2 == null)
075               throw new NoSuchElementException();
076            i2.remove();
077         }
078      };
079   }
080}