Commit f5c3e7f6 authored by Gorazd's avatar Gorazd
Browse files

Revert "I really don't like this HW"

This reverts commit 1f03fadb.
parent 1f03fadb
Loading
Loading
Loading
Loading
Loading
+4 −20
Original line number Diff line number Diff line
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
@@ -27,14 +12,14 @@ 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() {

        return parser(DEFAULT_DELIMITER, DEFAULT_CHARSET);
        throw new UnsupportedOperationException("Implement this method!");
    }

    /**
@@ -45,7 +30,6 @@ public final class CsvToolkit{
     * @return parser
     */
    public static CsvParser parser(String delimiter, Charset charset) {
        return new Parser(delimiter, charset);
        throw new UnsupportedOperationException("Implement this method!");
    }

}
+0 −62
Original line number Diff line number Diff line
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;
    }
}
+0 −45
Original line number Diff line number Diff line
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;
    }
}
+0 −50
Original line number Diff line number Diff line
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();
    }
}