Java Iterator Join joinIterators(final Iterator... iterators)

Here you can find the source of joinIterators(final Iterator... iterators)

Description

join Iterators

License

Open Source License

Declaration

public static <T> Iterator<T> joinIterators(final Iterator<T>... iterators) 

Method Source Code

//package com.java2s;
/*//from  ww  w.j a  va 2s  .c  o m
Cosmic Drift is a computer game about building simulated space stations.
Copyright (C) 2014-2015 Cel Skeggs and Christopher Quisling.
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.Iterator;

import java.util.NoSuchElementException;

public class Main {
    public static <T> Iterator<T> joinIterators(final Iterator<T>... iterators) {
        return new Iterator<T>() {
            private int next = 0;

            @Override
            public boolean hasNext() {
                while (next < iterators.length && !iterators[next].hasNext()) {
                    next++;
                }
                return next < iterators.length;
            }

            @Override
            public T next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                return iterators[next].next();
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Remove not implemented for joined iterators.");
            }
        };
    }
}

Related

  1. join(Iterator iterator, String separator)
  2. join(Iterator iterator)
  3. join(String delimiter, Iterator iterator)
  4. join(String seperator, Iterator objects)
  5. join(String seperator, Iterator objects)