Java Iterable Join joinIterables(final Iterable... iterables)

Here you can find the source of joinIterables(final Iterable... iterables)

Description

join Iterables

License

Open Source License

Declaration

public static <T> Iterable<T> joinIterables(final Iterable<T>... iterables) 

Method Source Code

//package com.java2s;
/*/*from  ww  w.  j  a  v  a2s  . co  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> Iterable<T> joinIterables(final Iterable<T>... iterables) {
        return new Iterable<T>() {
            @Override
            public Iterator<T> iterator() {
                Iterator<T>[] iterators = new Iterator[iterables.length];
                for (int i = 0; i < iterators.length; i++) {
                    iterators[i] = iterables[i].iterator();
                }
                return joinIterators(iterators);
            }
        };
    }

    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(String separator, Iterable args)
  2. join(String seperator, Iterator objects)
  3. join(String seprator, Iterable coll)
  4. join(StringBuilder buf, Iterable values, String separator)
  5. joinIterableOnComma(Iterable iterable)
  6. joinStr(CharSequence glue, Iterable parts)
  7. joinStrings(Iterable arr, String glue)
  8. joinStrings(Iterable strs, String sep)
  9. joinWith(Iterable elements, String join)