Commit 1f03fadb authored by Gorazd's avatar Gorazd
Browse files

I really don't like this HW
parent 2d84e2aa
Pipeline #94108 failed with stage
in 23 seconds
package cz.muni.fi.pb162.hw03.csv.impl;
import cz.muni.fi.pb162.hw03.csv.CsvParser;
import cz.muni.fi.pb162.hw03.csv.CsvReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import static cz.muni.fi.pb162.hw03.csv.CsvParser.DEFAULT_CHARSET;
import static cz.muni.fi.pb162.hw03.csv.CsvParser.DEFAULT_DELIMITER;
/**
* Factory class for CSV processing
*/
public final class CsvToolkit {
public final class CsvToolkit{
private CsvToolkit() {
// intentionally private to prevent instantiation
}
/**
* Creates instance of {@link CsvParser} with default delimiter and charset
*
* @return parser
*/
public static CsvParser parser() {
throw new UnsupportedOperationException("Implement this method!");
return parser(DEFAULT_DELIMITER, DEFAULT_CHARSET);
}
/**
......@@ -30,6 +45,7 @@ public final class CsvToolkit {
* @return parser
*/
public static CsvParser parser(String delimiter, Charset charset) {
throw new UnsupportedOperationException("Implement this method!");
return new Parser(delimiter, charset);
}
}
package cz.muni.fi.pb162.hw03.csv.impl;
import cz.muni.fi.pb162.hw03.csv.CsvParser;
import cz.muni.fi.pb162.hw03.csv.CsvReader;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Parser implements CsvParser {
private final String delimiter;
private final Charset charset;
public Parser(String delimiter, Charset charset) {
this.delimiter = delimiter;
this.charset = charset;
}
@Override
public CsvReader<List<String>> open(Path path) throws IOException {
return open(new FileInputStream(path.toString()));
}
@Override
public CsvReader<List<String>> open(InputStream is) throws IOException {
return new Reader(is,charset,delimiter);
}
@Override
public CsvReader<Map<String, String>> openWithHeader(Path path) throws IOException {
return openWithHeader(new FileInputStream(path.toString()));
}
@Override
public CsvReader<Map<String, String>> openWithHeader(InputStream is) throws IOException {
return new ReaderWithHeader(is,charset, delimiter);
}
@Override
public List<List<String>> readAll(Path path) throws IOException {
List res = new ArrayList();
open(path).forEach(x -> res.add(x));
return res;
}
@Override
public List<Map<String, String>> readAllWithHeader(Path path) throws IOException {
List res = new ArrayList();
openWithHeader(path).forEach(x -> res.add(x));
return res;
}
}
package cz.muni.fi.pb162.hw03.csv.impl;
import cz.muni.fi.pb162.hw03.csv.CsvReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import static cz.muni.fi.pb162.hw03.csv.Messages.INVALID_FORMAT;
public class Reader implements CsvReader<List<String>> {
private final BufferedReader reader;
private final String delimiter;
public Reader(InputStream is, Charset charset, String delimiter){
reader = new BufferedReader(new InputStreamReader(is,charset));
this.delimiter = delimiter;
}
@Override
public void forEach(Consumer consumer) throws IOException {
while (reader.ready()){
consumer.accept(read());
}
}
@Override
public void close() throws IOException {
reader.close();
}
@Override
public List<String> read() throws IOException {
return reader.ready() ? Arrays.asList(reader.readLine().split(delimiter))
.stream()
.map(String::trim)
.collect(Collectors.toList()) :null;
}
}
package cz.muni.fi.pb162.hw03.csv.impl;
import cz.muni.fi.pb162.hw03.csv.CsvReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static cz.muni.fi.pb162.hw03.csv.Messages.INVALID_FORMAT;
public class ReaderWithHeader implements CsvReader<Map<String, String>> {
private final Reader reader;
private final List<String> labels;
public ReaderWithHeader(InputStream is, Charset charset, String delimiter) throws IOException {
reader = new Reader(is,charset,delimiter);
labels = reader.read();
}
@Override
public Map<String, String> read() throws IOException {
List<String> s = reader.read();
if (s == null){
return null;
}
if (s.size() != labels.size()){
throw new IOException(INVALID_FORMAT);
}
return IntStream.range(0, labels.size()).boxed()
.collect(Collectors.toMap(labels::get, s::get));
}
@Override
public void forEach(Consumer<Map<String, String>> consumer) throws IOException {
Map<String, String> line;
while ((line = read()) != null){
consumer.accept(line);
}
}
@Override
public void close() throws IOException {
reader.close();
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment