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

org.nuiton.csv.ImportToMap Maven / Gradle / Ivy

There is a newer version: 3.1
Show newest version
/*
 * #%L
 * Nuiton CSV
 * %%
 * Copyright (C) 2011 CodeLutin
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */
package org.nuiton.csv;

import java.io.InputStream;
import java.io.Reader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

/**
 * A extended {@link Import} to read csv lines into a single map.
 * 

* Warning: The map used to push values for a csv line is the * same for all lines, it means you have to copy to your own object. * * @author Tony Chemit - [email protected] * @since 2.4 */ public class ImportToMap extends Import> { public static ImportToMap newImportToMap(ImportModel> model, InputStream inputStream) { return new ImportToMap(model, inputStream); } public static ImportToMap newImportToMap(ImportModel> model, Reader reader) { return new ImportToMap(model, reader); } public static ImportToMap newImportToMap(ImportModel> model, InputStream inputStream, boolean safetySwitch) { return new ImportToMap(model, inputStream, safetySwitch); } public static ImportToMap newImportToMap(ImportModel> model, Reader reader, boolean safetySwitch) { return new ImportToMap(model, reader, safetySwitch); } @Override public Iterator> iterator() { // obtain headers from csv input and validate the model prepareAndValidate(); return new Iterator>() { // read first line since first line is header boolean hasNext = readRow(); // get once for all columns to import List, Object>> columns = getNonIgnoredHeaders(); // to stock the current line number int lineNumber; // the map where to object of a row final Map element = new HashMap(); @Override public boolean hasNext() { return hasNext; } @Override public Map next() throws NoSuchElementException, ImportRuntimeException { if (!hasNext) { throw new NoSuchElementException(); } lineNumber += 1; // clean all values from the element element.clear(); for (ImportableColumn, Object> field : columns) { // read value from csv cell String value = readValue(field, lineNumber); // contravariance ftw Object parsedValue = parseValue(field, lineNumber, value); // set value to element setValue(field, lineNumber, element, parsedValue); } // check if there is a next row to read hasNext = readRow(); return element; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } protected ImportToMap(ImportModel> mapImportModel, InputStream inputStream) { this(mapImportModel, inputStream, true); } protected ImportToMap(ImportModel> mapImportModel, Reader reader) { this(mapImportModel, reader, true); } protected ImportToMap(ImportModel> mapImportModel, InputStream inputStream, boolean safetySwitch) { super(mapImportModel, inputStream); this.reader.setSafetySwitch(safetySwitch); } protected ImportToMap(ImportModel> mapImportModel, Reader reader, boolean safetySwitch) { super(mapImportModel, reader); this.reader.setSafetySwitch(safetySwitch); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy