package com.smartg.java.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Iterator items from multiple Iterators 
 * @author User
 *
 * @param <E>
 */
public class MIterable<E> implements Iterable<E> {

	private final List<Iterator<E>> list = new ArrayList<>();

	public void add(Iterable<E> iterable) {
		list.add(iterable.iterator());
	}

	public void add(Iterator<E> iterator) {
		list.add(iterator);
	}

	static class MIterator<E> implements Iterator<E> {
		private final List<Iterator<E>> list;
		private Iterator<Iterator<E>> iterator;
		private Iterator<E> current;

		public MIterator(List<Iterator<E>> list) {
			this.list = list;
		}

		@Override
		public boolean hasNext() {
			Iterator<E> iter = getIterator();
			return iter != null && iter.hasNext();
		}

		private Iterator<E> getIterator() {
			if (current != null && current.hasNext()) {
				return current;
			}
			if (iterator == null) {
				iterator = list.iterator();
			}
			if (iterator.hasNext()) {
				current = iterator.next();
			}
			return current;
		}

		@Override
		public E next() {
			Iterator<E> iter = getIterator();
			if (iter == null) {
				return null;
			}
			return iter.next();
		}
	}

	@Override
	public Iterator<E> iterator() {
		return new MIterator<>(list);
	}
}

@@ -148,17 +148,19 @@
 terators
- 
+%0A *
 %0A * @aut
@@ -241,17 +241,20 @@
 e%3CE%3E %7B%0A%0A
-%09
+    
 private 
@@ -304,17 +304,20 @@
 t%3C%3E();%0A%0A
-%09
+    
 public v
@@ -344,26 +344,32 @@
 iterable) %7B%0A
-%09%09
+        
 list.add(ite
@@ -387,21 +387,28 @@
 tor());%0A
-%09%7D%0A%0A%09
+    %7D%0A %0A    
 public v
@@ -439,18 +439,24 @@
 ator) %7B%0A
-%09%09
+        
 list.add
@@ -467,21 +467,27 @@
 rator);%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 static c
@@ -529,18 +529,25 @@
 or%3CE%3E %7B%0A
-%09%09
+%0A        
 private 
@@ -576,18 +576,24 @@
 %3E list;%0A
-%09%09
+        
 private 
@@ -624,18 +624,24 @@
 erator;%0A
-%09%09
+        
 private 
@@ -662,18 +662,24 @@
 rrent;%0A%0A
-%09%09
+        
 public M
@@ -713,19 +713,28 @@
 list) %7B%0A
-%09%09%09
+            
 this.lis
@@ -743,23 +743,35 @@
 = list;%0A
-%09%09%7D%0A%0A%09%09
+        %7D%0A%0A        
 @Overrid
@@ -768,26 +768,32 @@
   @Override%0A
-%09%09
+        
 public boole
@@ -799,35 +799,44 @@
 ean hasNext() %7B%0A
-%09%09%09
+            
 Iterator%3CE%3E iter
@@ -853,19 +853,28 @@
 ator();%0A
-%09%09%09
+            
 return i
@@ -904,23 +904,35 @@
 Next();%0A
-%09%09%7D%0A%0A%09%09
+        %7D%0A%0A        
 private 
@@ -959,19 +959,28 @@
 tor() %7B%0A
-%09%09%09
+            
 if (curr
@@ -1019,36 +1019,66 @@
 ) %7B%0A
-%09%09%09%09return current;%0A%09%09%09%7D%0A%09%09%09
+                return current;%0A            %7D%0A            
 if (
@@ -1097,20 +1097,32 @@
 null) %7B%0A
-%09%09%09%09
+                
 iterator
@@ -1141,24 +1141,106 @@
 ator();%0A
-%09%09%09%7D%0A%09%09%09
+            %7D%0A            while (current == null %7C%7C !current.hasNext()) %7B%0A                
 if (iter
@@ -1257,20 +1257,36 @@
 xt()) %7B%0A
-%09%09%09%09
+                    
 current 
@@ -1308,39 +1308,146 @@
 ();%0A
-%09%09%09%7D%0A%09%09%09return current;%0A%09%09%7D%0A%0A%09%09
+                %7D else %7B%0A                    return null;%0A                %7D%0A            %7D%0A%0A            return current;%0A        %7D%0A%0A        
 @Ove
@@ -1452,18 +1452,24 @@
 verride%0A
-%09%09
+        
 public E
@@ -1478,19 +1478,28 @@
 ext() %7B%0A
-%09%09%09
+            
 Iterator
@@ -1524,19 +1524,28 @@
 ator();%0A
-%09%09%09
+            
 if (iter
@@ -1560,62 +1560,104 @@
 ) %7B%0A
-%09%09%09%09return null;%0A%09%09%09%7D%0A%09%09%09return iter.next();%0A%09%09%7D%0A%09%7D%0A%0A%09
+                return null;%0A            %7D%0A            return iter.next();%0A        %7D%0A    %7D%0A%0A    
 @Ove
@@ -1662,17 +1662,20 @@
 verride%0A
-%09
+    
 public I
@@ -1702,10 +1702,16 @@
 ) %7B%0A
-%09%09
+        
 retu
@@ -1740,9 +1740,12 @@
 t);%0A
-%09
+    
 %7D%0A%7D%0A

Update MIterable.java