/*
* 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 static com.google.common.collect.Lists.transform;
import java.util.List;
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.way.io.Table.Error;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
/**
* @author mario.marques@objectos.com.br (Mario Marques Junior)
*/
class TableReaderCsv<T> implements TableSet<T> {
private final CsvFile csv;
private final AbstractTableParser<T> parser;
private final AbstractTableFilter<T> filter;
public TableReaderCsv(CsvFile csv, AbstractTableParser<T> parser, AbstractTableFilter<T> filter) {
this.csv = csv;
this.parser = parser;
this.filter = filter;
}
@Override
public Table<T> sheetNamed(String name) {
throw new UnsupportedOperationException();
}
@Override
public Table<T> sheetNamed(String template, Object... args) {
throw new UnsupportedOperationException();
}
@Override
public Table<T> get() {
ParsedLines lines = csv.getLines();
Iterable<Wrapper> wrappers;
wrappers = Iterables.transform(lines, new ToWrapper());
Iterable<Wrapper> parsed;
parsed = Iterables.transform(wrappers, new Parser());
List<Wrapper> protos;
protos = ImmutableList.copyOf(parsed);
List<T> entities;
entities = transform(protos, new ToEntity());
Iterable<T> valids;
valids = Iterables.filter(entities, new Valid());
List<List<Error>> elistzes;
elistzes = transform(protos, new ToErrors());
Iterable<Error> errors;
errors = Iterables.concat(elistzes);
return new TablePojo<T>(valids, errors);
}
private class ToWrapper implements Function<Line, Wrapper> {
@Override
public Wrapper apply(Line line) {
return new Wrapper(line);
}
}
private class Wrapper {
final Line line;
List<Table.Error> 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;
}
}
private class Parser implements Function<Wrapper, Wrapper> {
@Override
public Wrapper apply(Wrapper input) {
return input.parse();
}
}
private class ToEntity implements Function<Wrapper, T> {
@Override
public T apply(Wrapper input) {
return input.entity;
}
}
private class Valid implements Predicate<T> {
@Override
public boolean apply(T input) {
return input != null && filter.apply(input);
}
}
private class ToErrors implements Function<Wrapper, List<Table.Error>> {
@Override
public List<Error> apply(Wrapper input) {
return input.errors;
}
}
}