Search in sources :

Example 1 with AbstractParser

use of com.univocity.parsers.common.AbstractParser in project symja_android_library by axkr.

the class FileReader method parseRows.

protected Table parseRows(ReadOptions options, boolean headerOnly, Reader reader, ReadOptions.ColumnTypeReadOptions columnTypeReadOptions, AbstractParser<?> parser, int sampleSize) {
    parser.beginParsing(reader);
    Table table = Table.create(options.tableName());
    List<String> headerRow = Lists.newArrayList(getColumnNames(options, columnTypeReadOptions, parser));
    @SuppressWarnings({ "UnstableApiUsage", "OptionalGetWithoutIsPresent" }) ColumnType[] types = Streams.mapWithIndex(headerRow.stream(), (columnName, idx) -> columnTypeReadOptions.columnType((int) idx, columnName)).filter(Optional::isPresent).map(Optional::get).toArray(ColumnType[]::new);
    for (int x = 0; x < types.length; x++) {
        if (types[x] != SKIP) {
            String columnName = cleanName(headerRow.get(x));
            if (Strings.isNullOrEmpty(columnName)) {
                columnName = "Column " + table.columnCount();
            }
            Column<?> newColumn = types[x].create(columnName);
            table.addColumns(newColumn);
        }
    }
    if (!headerOnly) {
        String[] columnNames = selectColumnNames(headerRow, types);
        int[] columnIndexes = new int[columnNames.length];
        for (int i = 0; i < columnIndexes.length; i++) {
            // get the index in the original table, which includes skipped fields
            columnIndexes[i] = headerRow.indexOf(columnNames[i]);
        }
        addRows(options, types, parser, table, columnIndexes, sampleSize);
    }
    return table;
}
Also used : Logger(org.slf4j.Logger) ColumnType(tech.tablesaw.api.ColumnType) AbstractParser(com.univocity.parsers.common.AbstractParser) Iterator(java.util.Iterator) Table(tech.tablesaw.api.Table) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Random(java.util.Random) Reader(java.io.Reader) Streams(com.google.common.collect.Streams) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) List(java.util.List) Lists(com.google.common.collect.Lists) AbstractColumnParser(tech.tablesaw.columns.AbstractColumnParser) Map(java.util.Map) Column(tech.tablesaw.columns.Column) SKIP(tech.tablesaw.api.ColumnType.SKIP) Optional(java.util.Optional) NoSuchElementException(java.util.NoSuchElementException) Table(tech.tablesaw.api.Table) ColumnType(tech.tablesaw.api.ColumnType) Optional(java.util.Optional)

Aggregations

Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 Streams (com.google.common.collect.Streams)1 AbstractParser (com.univocity.parsers.common.AbstractParser)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Optional (java.util.Optional)1 Random (java.util.Random)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 ColumnType (tech.tablesaw.api.ColumnType)1 SKIP (tech.tablesaw.api.ColumnType.SKIP)1 Table (tech.tablesaw.api.Table)1 AbstractColumnParser (tech.tablesaw.columns.AbstractColumnParser)1 Column (tech.tablesaw.columns.Column)1