Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. package com.tool.main;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.LinkedHashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13.  
  14. public class TTCSV2HashMap implements TTSheet2HashMap {
  15. private String path;
  16. private static final int HEADER_ROW = 0;
  17. private static final String SEPARATOR = ";";
  18.  
  19. /**
  20. * Constructor
  21. *
  22. * @param path
  23. * to CSV file
  24. */
  25. public TTCSV2HashMap(String path) {
  26. this.path = path;
  27. }
  28.  
  29. /**
  30. * AND Condition
  31. *
  32. * @param row
  33. * @param col2val
  34. * @return
  35. */
  36. private boolean rowContainsAllValues(Map<String, String> row, Map<String, String> col2val) {
  37. String searchValue;
  38. for (String searchCol : col2val.keySet()) {
  39. if (row.containsKey(searchCol)) {
  40. searchValue = col2val.get(searchCol);
  41. if (!searchValue.equalsIgnoreCase(row.get(searchCol))) {
  42. return false;
  43. }
  44. } else {
  45. throw new IllegalArgumentException("Row does not contain search column '" + searchCol + "'");
  46. }
  47. }
  48. return true;
  49. }
  50.  
  51. private boolean isRowInRange(int rowNum, int start, int end) {
  52. // Only start limit
  53. if (start == -1 && end != -1) {
  54. return rowNum <= end;
  55. } else if (end == -1 && start != -1) {
  56. return rowNum >= start;
  57. } else if (start == -1 && end == -1) {
  58. return true;
  59. } else {
  60. return (rowNum >= start && rowNum <= end);
  61. }
  62. }
  63.  
  64. private List<Map<String, String>> process(Map<String, String> columnName2SeachValue, int start, int end) {
  65. List<Map<String, String>> result = new ArrayList<Map<String, String>>();
  66.  
  67. try {
  68. BufferedReader reader = new BufferedReader(new FileReader(this.path));
  69.  
  70. // Read header first
  71. Map<String, String> columnHeaders = new LinkedHashMap<String, String>();
  72. String line;
  73. String[] split;
  74. Map<String, String> currentRowMap;
  75. int rowCount = 0;
  76. ROW_ITERATION: while ((line = reader.readLine()) != null) {
  77. split = line.split(SEPARATOR);
  78.  
  79. if (rowCount == HEADER_ROW) {
  80. for (int i = 0; i < split.length; i++) {
  81. columnHeaders.put("" + i, split[i]);
  82. }
  83. result.add(columnHeaders);
  84. } else {
  85. if (isRowInRange(rowCount, start, end)) {
  86. currentRowMap = new LinkedHashMap<String, String>();
  87. for (String columnHeaderIndex : columnHeaders.keySet()) {
  88. int index = Integer.parseInt(columnHeaderIndex);
  89. String columnHeader = columnHeaders.get(columnHeaderIndex);
  90. currentRowMap.put(columnHeader, split[index]);
  91. }
  92.  
  93. if ((columnName2SeachValue == null || columnName2SeachValue.isEmpty()) || (rowContainsAllValues(currentRowMap, columnName2SeachValue))) {
  94. result.add(currentRowMap);
  95. }
  96. } else if (rowCount > end) {
  97. break ROW_ITERATION;
  98. }
  99. }
  100.  
  101. rowCount++;
  102. }
  103.  
  104. reader.close();
  105. } catch (IOException e) {
  106. e.printStackTrace();
  107. }
  108.  
  109. return result;
  110. }
  111.  
  112. /****************************************** PUBLIC ********************************************/
  113. /* (non-Javadoc)
  114. * @see com.tool.main.TTSheet2HashMap#load(java.util.Map, int, int)
  115. */
  116. @Override
  117. public List<Map<String, String>> load(Map<String, String> columnName2SeachValue, int start, int end) {
  118. return process(columnName2SeachValue, start, end);
  119. }
  120.  
  121. /* (non-Javadoc)
  122. * @see com.tool.main.TTSheet2HashMap#load(java.util.Map)
  123. */
  124. @Override
  125. public List<Map<String, String>> load(Map<String, String> columnName2SeachValue) {
  126. return this.load(columnName2SeachValue, -1, -1);
  127. }
  128.  
  129. /* (non-Javadoc)
  130. * @see com.tool.main.TTSheet2HashMap#load()
  131. */
  132. @Override
  133. public List<Map<String, String>> load() {
  134. return this.load(new HashMap<String, String>(), -1, -1);
  135. }
  136.  
  137. /* (non-Javadoc)
  138. * @see com.tool.main.TTSheet2HashMap#load(int, int)
  139. */
  140. @Override
  141. public List<Map<String, String>> load(int start, int end) {
  142. return this.load(new HashMap<String, String>(), start, end);
  143. }
  144.  
  145. /* (non-Javadoc)
  146. * @see com.tool.main.TTSheet2HashMap#numberOfRows()
  147. */
  148. @Override
  149. public int numberOfRows() {
  150. int rowCount = 0;
  151.  
  152. BufferedReader reader;
  153. try {
  154. reader = new BufferedReader(new FileReader(this.path));
  155. try {
  156. while ((reader.readLine()) != null) {
  157. rowCount++;
  158. }
  159. } catch (IOException e) {
  160. e.printStackTrace();
  161. } finally {
  162. try {
  163. reader.close();
  164. } catch (IOException e) {
  165. e.printStackTrace();
  166. }
  167. }
  168. } catch (FileNotFoundException e1) {
  169. e1.printStackTrace();
  170. }
  171.  
  172. return rowCount;
  173. }
  174.  
  175. /****************************************** MAIN ********************************************/
  176.  
  177. public static void main(String[] args) throws Exception {
  178. if (args.length < 1) {
  179. System.err.println("Use:");
  180. System.err.println("TTCSV2HashMap <csv file>");
  181. return;
  182. }
  183.  
  184. File file = new File(args[0]);
  185. if (!file.exists()) {
  186. System.err.println("Not found or not a file: " + file.getPath());
  187. return;
  188. }
  189.  
  190. TTSheet2HashMap csv2map = new TTCSV2HashMap(file.getPath());
  191. System.out.println(csv2map.numberOfRows());
  192. }
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement