package innovimax.mixthem.io;

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

public class MultiChannelLineReader implements IMultiChannelLineInput {
	
	private final List<ILineInput> readers = new ArrayList<ILineInput>();
	
	public MultiChannelLineReader(final List<InputResource> inputs) {
		inputs.stream().forEach(input -> {
			try {
				this.readers.add(new DefaultLineReader(input));
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		});
	}
	
	@Override
	public boolean hasLine() throws IOException {
		final Iterator<ILineInput> iterator = this.readers.iterator();
		while (iterator.hasNext()) {
			final ILineInput reader = iterator.next();
			if (reader.hasLine()) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	public List<String> nextLineRange() throws IOException {
		final List<String> lines = new ArrayList<String>();		
		final Iterator<ILineInput> iterator = this.readers.iterator();
		while (iterator.hasNext()) {
			final ILineInput reader = iterator.next();
			final String line = reader.nextLine();
			lines.add(line);
		}
		return lines;	
	}
	
	@Override
	public List<String> nextLineRange(List<Boolean> readingRange) throws IOException {
		final List<String> lines = new ArrayList<String>();
		int index = 0;
		final Iterator<ILineInput> iterator = this.readers.iterator();
		while (iterator.hasNext()) {
			final ILineInput reader = iterator.next();
			if (readingRange.get(index).booleanValue()) {				
				lines.add(reader.nextLine());
			} else {
				lines.add(null);
			}
			index++;
		}
		return lines;		
	}

	@Override
	public void close() throws IOException {
		final Iterator<ILineInput> iterator = this.readers.iterator();
		while (iterator.hasNext()) {
			final ILineInput reader = iterator.next();
			reader.close();
		}		
	}
	
}

@@ -129,16 +129,73 @@
 il.List;
+%0Aimport java.util.Set;%0Aimport java.util.stream.IntStream;
 %0A%0Apublic
@@ -340,91 +340,459 @@
 %0A%09%0A%09
-public MultiChannelLineReader(final List%3CInputResource%3E inputs) %7B%0A%09%09inputs.stream()
+/**%0A%09* Constructor%0A%09* @param inputs The list of inputs as InputResource%0A%09* @param selection The input index selection (maybe empty)%0A%09* @see innovimax.mixthem.io.InputResource%0A%09*/%0A%09public MultiChannelLineReader(final List%3CInputResource%3E inputs, final Set%3CInteger%3E selection) %7B%0A%09%09IntStream.rangeClosed(1, inputs.size())%0A%09%09%09.filter(index -%3E selection.isEmpty() %7C%7C selection.contains(Integer.valueOf(index)))%0A%09%09%09.mapToObj(index -%3E inputs.get(index-1))%0A%09%09%09
 .for
@@ -814,14 +814,16 @@
 %0A%09%09%09
+%09
 try %7B%0A
+%09
 %09%09%09%09
@@ -866,24 +866,25 @@
 er(input));%0A
+%09
 %09%09%09%7D catch (
@@ -904,16 +904,17 @@
 ) %7B%0A%09%09%09%09
+%09
 throw ne
@@ -939,18 +939,20 @@
 (e);%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%7D);%0A%09%7D
@@ -1018,215 +1018,181 @@
 %7B%0A%09%09
-final Iterator%3CILineInput%3E iterator = this.readers.iterator();%0A%09%09while (iterator.hasNext()) %7B%0A%09%09%09final ILineInput reader = iterator.next();%0A%09%09%09if (reader.hasLine()) %7B%0A%09%09%09%09return true
+return this.readers.stream()%0A%09%09%09.anyMatch(reader -%3E %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09return reader.hasLine();%0A%09%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09%09throw new RuntimeException(e)
 ;%0A%09%09%09
+%09
 %7D
-%0A
 %09%09
-%7D
 %0A%09%09
-return false
+%09%7D)
 ;%0A%09%7D
@@ -2079,170 +2079,144 @@
 %7B%0A%09%09
-final Iterator%3CILineInput%3E iterator = this.readers.iterator();%0A%09%09while (iterator.hasNext()) %7B%0A%09%09%09final ILineInput reader = iterator.next();%0A%09%09%09reader.close();
+this.readers.forEach(reader -%3E %7B%0A%09%09%09try %7B%0A%09%09%09%09reader.close();%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09throw new RuntimeException(e);%0A%09%09%09%7D
 %0A%09%09%7D
+);
 %09%09%0A%09

Update MultiChannelLineReader.java