Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tool.main;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- public class TTCSV2HashMap implements TTSheet2HashMap {
- private String path;
- private static final int HEADER_ROW = 0;
- private static final String SEPARATOR = ";";
- /**
- * Constructor
- *
- * @param path
- * to CSV file
- */
- public TTCSV2HashMap(String path) {
- this.path = path;
- }
- /**
- * AND Condition
- *
- * @param row
- * @param col2val
- * @return
- */
- private boolean rowContainsAllValues(Map<String, String> row, Map<String, String> col2val) {
- String searchValue;
- for (String searchCol : col2val.keySet()) {
- if (row.containsKey(searchCol)) {
- searchValue = col2val.get(searchCol);
- if (!searchValue.equalsIgnoreCase(row.get(searchCol))) {
- return false;
- }
- } else {
- throw new IllegalArgumentException("Row does not contain search column '" + searchCol + "'");
- }
- }
- return true;
- }
- private boolean isRowInRange(int rowNum, int start, int end) {
- // Only start limit
- if (start == -1 && end != -1) {
- return rowNum <= end;
- } else if (end == -1 && start != -1) {
- return rowNum >= start;
- } else if (start == -1 && end == -1) {
- return true;
- } else {
- return (rowNum >= start && rowNum <= end);
- }
- }
- private List<Map<String, String>> process(Map<String, String> columnName2SeachValue, int start, int end) {
- List<Map<String, String>> result = new ArrayList<Map<String, String>>();
- try {
- BufferedReader reader = new BufferedReader(new FileReader(this.path));
- // Read header first
- Map<String, String> columnHeaders = new LinkedHashMap<String, String>();
- String line;
- String[] split;
- Map<String, String> currentRowMap;
- int rowCount = 0;
- ROW_ITERATION: while ((line = reader.readLine()) != null) {
- split = line.split(SEPARATOR);
- if (rowCount == HEADER_ROW) {
- for (int i = 0; i < split.length; i++) {
- columnHeaders.put("" + i, split[i]);
- }
- result.add(columnHeaders);
- } else {
- if (isRowInRange(rowCount, start, end)) {
- currentRowMap = new LinkedHashMap<String, String>();
- for (String columnHeaderIndex : columnHeaders.keySet()) {
- int index = Integer.parseInt(columnHeaderIndex);
- String columnHeader = columnHeaders.get(columnHeaderIndex);
- currentRowMap.put(columnHeader, split[index]);
- }
- if ((columnName2SeachValue == null || columnName2SeachValue.isEmpty()) || (rowContainsAllValues(currentRowMap, columnName2SeachValue))) {
- result.add(currentRowMap);
- }
- } else if (rowCount > end) {
- break ROW_ITERATION;
- }
- }
- rowCount++;
- }
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return result;
- }
- /****************************************** PUBLIC ********************************************/
- /* (non-Javadoc)
- * @see com.tool.main.TTSheet2HashMap#load(java.util.Map, int, int)
- */
- @Override
- public List<Map<String, String>> load(Map<String, String> columnName2SeachValue, int start, int end) {
- return process(columnName2SeachValue, start, end);
- }
- /* (non-Javadoc)
- * @see com.tool.main.TTSheet2HashMap#load(java.util.Map)
- */
- @Override
- public List<Map<String, String>> load(Map<String, String> columnName2SeachValue) {
- return this.load(columnName2SeachValue, -1, -1);
- }
- /* (non-Javadoc)
- * @see com.tool.main.TTSheet2HashMap#load()
- */
- @Override
- public List<Map<String, String>> load() {
- return this.load(new HashMap<String, String>(), -1, -1);
- }
- /* (non-Javadoc)
- * @see com.tool.main.TTSheet2HashMap#load(int, int)
- */
- @Override
- public List<Map<String, String>> load(int start, int end) {
- return this.load(new HashMap<String, String>(), start, end);
- }
- /* (non-Javadoc)
- * @see com.tool.main.TTSheet2HashMap#numberOfRows()
- */
- @Override
- public int numberOfRows() {
- int rowCount = 0;
- BufferedReader reader;
- try {
- reader = new BufferedReader(new FileReader(this.path));
- try {
- while ((reader.readLine()) != null) {
- rowCount++;
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- return rowCount;
- }
- /****************************************** MAIN ********************************************/
- public static void main(String[] args) throws Exception {
- if (args.length < 1) {
- System.err.println("Use:");
- System.err.println("TTCSV2HashMap <csv file>");
- return;
- }
- File file = new File(args[0]);
- if (!file.exists()) {
- System.err.println("Not found or not a file: " + file.getPath());
- return;
- }
- TTSheet2HashMap csv2map = new TTCSV2HashMap(file.getPath());
- System.out.println(csv2map.numberOfRows());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement