
br.com.objectos.way.io.TableReaderCsv Maven / Gradle / Ivy
/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.way.io;
import java.util.List;
import java.util.stream.Stream;
import br.com.objectos.comuns.io.Line;
import br.com.objectos.comuns.io.ParsedLines;
import br.com.objectos.comuns.io.csv.CsvFile;
import br.com.objectos.core.collections.ImmutableList;
import br.com.objectos.core.collections.MoreCollectors;
import br.com.objectos.way.io.Table.Error;
/**
* @author [email protected] (Mario Marques Junior)
*/
class TableReaderCsv implements TableSet {
private final CsvFile csv;
private final AbstractTableParser parser;
private final AbstractTableFilter filter;
public TableReaderCsv(CsvFile csv, AbstractTableParser parser, AbstractTableFilter filter) {
this.csv = csv;
this.parser = parser;
this.filter = filter;
}
@Override
public Table sheetNamed(String name) {
throw new UnsupportedOperationException();
}
@Override
public Table sheetNamed(String template, Object... args) {
throw new UnsupportedOperationException();
}
@Override
public Table get() {
ParsedLines lines = csv.getLines();
List protos = lines.stream()
.map(Wrapper::new)
.map(Wrapper::parse)
.collect(MoreCollectors.toImmutableList());
List valids = protos.stream()
.map(o -> o.entity)
.filter(input -> input != null && filter.apply(input))
.collect(MoreCollectors.toImmutableList());
List errors = protos.stream()
.flatMap(Wrapper::toErrors)
.collect(MoreCollectors.toImmutableList());
return new TablePojo(valids, errors);
}
private class Wrapper {
final Line line;
List errors = ImmutableList.of();
T entity;
public Wrapper(Line line) {
this.line = line;
}
public Wrapper parse() {
try {
entity = parser.parseLine(line);
} catch (Exception e) {
Error error = TableRowError.fromException(line, e);
errors = ImmutableList.of(error);
}
return this;
}
public Stream toErrors() {
return errors.stream();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy