Search in sources :

Example 1 with AbstractColumnParser

use of tech.tablesaw.columns.AbstractColumnParser in project symja_android_library by axkr.

the class FileReader method addRows.

private void addRows(ReadOptions options, ColumnType[] types, AbstractParser<?> reader, Table table, int[] columnIndexes, int sampleSize) {
    String[] nextLine;
    Map<String, AbstractColumnParser<?>> parserMap = getParserMap(options, table);
    Random random = new Random(0);
    // Add the rows
    for (int rowNumber = options.header() ? 1 : 0; (nextLine = reader.parseNext()) != null; rowNumber++) {
        // validation
        if (options.skipRowsWithInvalidColumnCount() && options.header() && nextLine.length != types.length) {
            continue;
        }
        if (nextLine.length < types.length) {
            if (nextLine.length == 1 && Strings.isNullOrEmpty(nextLine[0])) {
                logger.error("Warning: Invalid file. Row " + rowNumber + " is empty. Continuing.");
                continue;
            } else {
                Exception e = new IndexOutOfBoundsException("Row number " + rowNumber + " contains " + nextLine.length + " columns. " + types.length + " expected.");
                throw new AddCellToColumnException(e, 0, rowNumber, table.columnNames(), nextLine);
            }
        } else if (nextLine.length > types.length) {
            throw new IllegalArgumentException("Row number " + rowNumber + " contains " + nextLine.length + " columns. " + types.length + " expected.");
        }
        int samplesCount = table.rowCount();
        if (sampleSize < 0 || samplesCount < sampleSize) {
            addValuesToColumns(table, columnIndexes, nextLine, parserMap, rowNumber, -1);
        } else {
            // find a row index to replace
            int randomIndex = random.nextInt(samplesCount + 1);
            // replace index if it is smaller than numSamples, otherwise ignore it.
            if (randomIndex < sampleSize) {
                addValuesToColumns(table, columnIndexes, nextLine, parserMap, rowNumber, randomIndex);
            }
        }
    }
}
Also used : AbstractColumnParser(tech.tablesaw.columns.AbstractColumnParser) Random(java.util.Random) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

NoSuchElementException (java.util.NoSuchElementException)1 Random (java.util.Random)1 AbstractColumnParser (tech.tablesaw.columns.AbstractColumnParser)1