37 lines
1.1 KiB
Java
37 lines
1.1 KiB
Java
package com.fabiosalvini.permutations;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.util.Arrays;
|
|
|
|
/**
|
|
* Class to read the permutation elements from the given input stream.
|
|
* Elements must be parsable as long and be separated by a comma.
|
|
*/
|
|
public class ElementsInputReader {
|
|
|
|
private static final String SEPARATOR = ",";
|
|
|
|
private final InputStream inputStream;
|
|
|
|
public ElementsInputReader(InputStream inputStream) {
|
|
this.inputStream = inputStream;
|
|
}
|
|
|
|
/**
|
|
* Read the elements from the input stream.
|
|
*
|
|
* @return the array of elements.
|
|
* @throws IOException if elements cannot be read or they have the wrong format.
|
|
*/
|
|
public long[] readElements() throws IOException {
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
String line = reader.readLine();
|
|
return Arrays.stream(line.split(SEPARATOR))
|
|
.mapToLong(Long::parseLong)
|
|
.toArray();
|
|
}
|
|
}
|