All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.way.io.TableReaderCsv Maven / Gradle / Ivy

The newest version!
/*
 * 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 [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();

    Iterable wrappers;
    wrappers = Iterables.transform(lines, new ToWrapper());

    Iterable parsed;
    parsed = Iterables.transform(wrappers, new Parser());

    List protos;
    protos = ImmutableList.copyOf(parsed);

    List entities;
    entities = transform(protos, new ToEntity());

    Iterable valids;
    valids = Iterables.filter(entities, new Valid());

    List> elistzes;
    elistzes = transform(protos, new ToErrors());

    Iterable errors;
    errors = Iterables.concat(elistzes);

    return new TablePojo(valids, errors);
  }

  private class ToWrapper implements Function {

    @Override
    public Wrapper apply(Line line) {
      return new Wrapper(line);
    }

  }

  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;
    }

  }

  private class Parser implements Function {
    @Override
    public Wrapper apply(Wrapper input) {
      return input.parse();
    }
  }

  private class ToEntity implements Function {
    @Override
    public T apply(Wrapper input) {
      return input.entity;
    }
  }

  private class Valid implements Predicate {
    @Override
    public boolean apply(T input) {
      return input != null && filter.apply(input);
    }
  }

  private class ToErrors implements Function> {
    @Override
    public List apply(Wrapper input) {
      return input.errors;
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy