package innovimax.mixthem.io;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;

public class MultiChannelCharReader implements IMultiChannelCharInput {
	
	private final List<ICharInput> readers = new ArrayList<ICharInput>();
	
	/**
	* Constructor
	* @param inputs The list of inputs as InputResource
	* @param selection The input index selection (maybe empty)
	* @see innovimax.mixthem.io.InputResource
	*/
	public MultiChannelCharReader(final List<InputResource> inputs, final Set<Integer> selection) {		
		/*if (selection.isEmpty()) {
			inputs.stream().forEach(input -> {
				try {
					this.readers.add(new DefaultCharReader(input));
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			});
		} else {*/
			IntStream.rangeClosed(1, inputs.size())
				.filter(index -> selection.isEmpty() || selection.contains(Integer.valueOf(index)))
				.mapToObj(index -> inputs.get(index-1))
				.forEach(input -> {
					try {
						this.readers.add(new DefaultCharReader(input));
					} catch (IOException e) {
						throw new RuntimeException(e);
					}
				});
		//}
	}
	
	@Override
	public boolean hasCharacter() throws IOException {
		final Iterator<ICharInput> iterator = this.readers.iterator();
		while (iterator.hasNext()) {
			final ICharInput reader = iterator.next();
			if (reader.hasCharacter()) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	public int[] nextCharacterRange() throws IOException {
		final int[] chars = new int[this.readers.size()];
		int channel = 0;
		final Iterator<ICharInput> iterator = this.readers.iterator();
		while (iterator.hasNext()) {
			final ICharInput reader = iterator.next();
			final int c = reader.nextCharacter();
			chars[channel++] = c;
		}
		return chars;
	}

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

@@ -618,233 +618,8 @@
 %7B%09%09%0A
-%09%09/*if (selection.isEmpty()) %7B%0A%09%09%09inputs.stream().forEach(input -%3E %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09this.readers.add(new DefaultCharReader(input));%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%09%7D);%0A%09%09%7D else %7B*/%0A%09
 %09%09In
@@ -659,17 +659,16 @@
 e())%0A%09%09%09
-%09
 .filter(
@@ -746,17 +746,16 @@
 x)))%0A%09%09%09
-%09
 .mapToOb
@@ -789,17 +789,16 @@
 -1))%0A%09%09%09
-%09
 .forEach
@@ -809,17 +809,16 @@
 ut -%3E %7B%0A
-%09
 %09%09%09%09try 
@@ -824,17 +824,16 @@
  %7B%0A%09%09%09%09%09
-%09
 this.rea
@@ -872,17 +872,16 @@
 nput));%0A
-%09
 %09%09%09%09%7D ca
@@ -907,17 +907,16 @@
  %7B%0A%09%09%09%09%09
-%09
 throw ne
@@ -938,17 +938,16 @@
 ion(e);%0A
-%09
 %09%09%09%09%7D%0A%09%09
@@ -951,19 +951,12 @@
 %0A%09%09%09
-%09
 %7D);%0A
-%09%09//%7D%0A
 %09%7D%0A%09
@@ -1011,32 +1011,37 @@
 s IOException %7B%0A
+%09%09/*%0A
 %09%09final Iterator
@@ -1240,16 +1240,93 @@
  false;%0A
+%09%09*/%0A%09%09return this.readers.stream.anyMatch(reader -%3E reader.hasCharacter());%0A
 %09%7D%0A%09%0A%09@O

Update MultiChannelCharReader.java