Java Iterator shallowUnionColIter(Iterator> values)

Here you can find the source of shallowUnionColIter(Iterator> values)

Description

shallow Union Col Iter

License

Open Source License

Declaration

public static <X> Iterable<X> shallowUnionColIter(Iterator<? extends Iterator<? extends X>> values) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

import java.util.Iterator;

import java.util.NoSuchElementException;

public class Main {
    public static <X> Iterable<X> shallowUnionColIter(Iterator<? extends Iterator<? extends X>> values) {
        return () -> multiIterator(values);
    }//from ww  w. jav a2s .c  o  m

    public static <X> Iterator<X> multiIterator(Iterable<? extends Iterator<? extends X>> iterable) {
        return multiIterator(iterable.iterator());
    }

    public static <X> Iterator<X> multiIterator(Iterator<? extends Iterator<? extends X>> iterable) {
        return new Iterator<X>() {

            Iterator<? extends X> curIter = null;

            @Override
            public boolean hasNext() {
                while ((this.curIter == null || !this.curIter.hasNext()) && iterable.hasNext()) {
                    while (iterable.hasNext()) {
                        this.curIter = iterable.next();
                        break;
                    }

                }
                return this.curIter != null && this.curIter.hasNext();
            }

            @Override
            public X next() {
                if (this.curIter == null) {
                    throw new NoSuchElementException();
                }
                return this.curIter.next();
            }
        };
    }

    public static <X> Iterator<X> multiIterator(Iterator<? extends X>... iterable) {
        return multiIterator(Arrays.asList(iterable));
    }
}

Related

  1. removeOnNext(final Iterator iterator)
  2. removeValue(List args, int idx, Iterator it)
  3. retrieveFirstAndExhaustIterator(Iterator iterator)
  4. safeAdvance(Iterator iterator)
  5. searchIndexInIterator(Iterator iterator, T value)
  6. single(Iterator i)
  7. singleElementIterator(T element)
  8. singleOrDefault(Iterator iterator)
  9. singletonIterator(final Object item)