package innovimax.mixthem.io;

import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class MultiChannelCharReader implements IMultiChannelCharInput {
	
	private final List<ICharInput> readers;
	
	/**
	* 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) {		
		this.readers = IntStream.rangeClosed(1, inputs.size())
			.filter(index -> selection.isEmpty() || selection.contains(Integer.valueOf(index)))
			.mapToObj(index -> inputs.get(index-1))
			.map(input -> {
				try {
					return new DefaultCharReader(input);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			})
			.collect(Collectors.toList());
	}
	
	@Override
	public boolean hasCharacter() throws IOException {
		return this.readers.stream()
			.anyMatch(reader -> {
				try {
					return reader.hasCharacter();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}		
			});
	}
	
	@Override
	public int[] nextCharacterRange() throws IOException {
		return readers.stream()
			.mapToInt(reader -> {
				try {
					return reader.nextCharacter();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}	
			})
			.toArray();
	}

	@Override
	public void close() throws IOException {
		this.readers.forEach(reader -> {
			try {
				reader.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		});
	}
	
}

@@ -894,23 +894,19 @@
 );%0A%09%09%09%09%7D
-%0A%09%09%09
 %7D)%0A
+
 %09%09%09.coll
@@ -1175,14 +1175,8 @@
 %09%09%09%7D
-%09%09%0A%09%09%09
 %7D);%0A
@@ -1419,13 +1419,8 @@
 %09%09%09%7D
-%09%0A%09%09%09
 %7D)%0A%09
@@ -1504,16 +1504,20 @@
 .readers
+%0A%09%09%09
 .forEach
@@ -1532,22 +1532,24 @@
 -%3E %7B%0A%09%09%09
+%09
 try %7B%0A
+%09
 %09%09%09%09read
@@ -1563,16 +1563,17 @@
 e();%0A%09%09%09
+%09
 %7D catch 
@@ -1586,24 +1586,25 @@
 eption e) %7B%0A
+%09
 %09%09%09%09throw ne
@@ -1633,12 +1633,10 @@
 %0A%09%09%09
-%7D%0A%09
 %09
+%7D
 %7D);%0A

Update MultiChannelCharReader.java