1 /** 2 * 3 */ 4 package net.sf.flatpack.examples; 5 6 import java.lang.reflect.Method; 7 8 import net.sf.flatpack.util.FPConstants; 9 import net.sf.flatpack.util.ParserUtils; 10 import net.sf.flatpack.examples.createsamplecsv.CSVTestFileCreator; 11 import net.sf.flatpack.examples.csvheaderandtrailer.CSVHeaderAndTrailer; 12 import net.sf.flatpack.examples.csvperformancetest.CSVPerformanceTest; 13 import net.sf.flatpack.examples.delimiteddynamiccolumns.DelimitedWithPZMap; 14 import net.sf.flatpack.examples.delimiteddynamiccolumnswitherrors.DelimitedWithPZMapErrors; 15 import net.sf.flatpack.examples.exporttoexcel.DelimitedFileExportToExcel; 16 import net.sf.flatpack.examples.fixedlengthdynamiccolumns.FixedLengthWithPZMap; 17 import net.sf.flatpack.examples.fixedlengthheaderandtrailer.FixedLengthHeaderAndTrailer; 18 import net.sf.flatpack.examples.largedataset.delimiteddynamiccolumns.LargeDelimitedWithPZMap; 19 import net.sf.flatpack.examples.largedataset.fixedlengthdynamiccolumns.LargeFixedLengthWithPZMap; 20 import net.sf.flatpack.examples.largedataset.largecsvperformancetest.CSVLarge; 21 import net.sf.flatpack.examples.lowlevelparse.LowLevelParse; 22 import net.sf.flatpack.examples.multilinedelimitedrecord.DelimitedMultiLine; 23 import net.sf.flatpack.examples.numericsanddates.NumericsAndDates; 24 25 /** 26 * @author Benoit Xhenseval 27 */ 28 public class Examples implements Repeater { 29 30 public void tearDown() { 31 } 32 33 /** 34 * @param args 35 */ 36 public static void main(final String[] args) { 37 final Examples examples = new Examples(); 38 examples.run(); 39 } 40 41 public void repeat(final Method target) { 42 } 43 44 public void doCall() { 45 System.out.println(""); 46 System.out.println(" ___ _ _ _ _ _"); 47 System.out.println(" / _ \\| |__ (_) ___ ___| |_| | __ _| |__"); 48 System.out.println(" | | | | '_ \\| |/ _ \\/ __| __| | / _` | '_ \\"); 49 System.out.println(" | |_| | |_) | | __/ (__| |_| |__| (_| | |_) |"); 50 System.out.println(" \\___/|_.__// |\\___|\\___|\\__|_____\\__,_|_.__/"); 51 System.out.println(" |__/"); 52 System.out.println(""); 53 System.out.println(" w w w . O b j e c t L a b . c o . u k"); 54 } 55 56 private void run() { 57 final ConsoleMenu menu = new ConsoleMenu(this); 58 menu.addMenuItem("CSVTestFileCreator", "doCSVTestFileCreator", false); 59 menu.addMenuItem("CSVHeaderAndTrailer", "doCSVHeaderAndTrailer", false); 60 menu.addMenuItem("CSVPerformanceTest", "doCSVPerformanceTest", false); 61 menu.addMenuItem("DelimitedWithPZMap", "doDelimitedWithPZMap", false); 62 menu.addMenuItem("DelimitedWithPZMapErrors", "doDelimitedWithPZMapErrors", false); 63 menu.addMenuItem("DelimitedFileExportToExcel", "doDelimitedFileExportToExcel", false); 64 menu.addMenuItem("FixedLengthWithPZMap", "doFixedLengthWithPZMap", false); 65 menu.addMenuItem("FixedLengthHeaderAndTrailer", "doFixedLengthHeaderAndTrailer", false); 66 menu.addMenuItem("LargeDelimitedWithPZMap", "doLargeDelimitedWithPZMap", false); 67 menu.addMenuItem("LargeFixedLengthWithPZMap", "doLargeFixedLengthWithPZMap", false); 68 menu.addMenuItem("CSVLarge", "doCSVLarge", false); 69 menu.addMenuItem("LowLevelParse", "doLowLevelParse", false); 70 menu.addMenuItem("DelimitedMultiLine", "doDelimitedMultiLine", false); 71 menu.addMenuItem("NumericsAndDates", "doNumericsAndDates", false); 72 menu.addMenuItem("Ask for GC", "doGC", false); 73 menu.addMenuItem("Test StringBuffer", "doStringBuffer", false); 74 menu.addMenuItem("Test Delim Parser Perform", "doTestParsers", false); 75 76 menu.addMenuItem("Who you gonna call?", "doCall", false); 77 menu.displayMenu(); 78 } 79 80 public void doCSVTestFileCreator() { 81 final int cols = ConsoleMenu.getInt("Number of cols", 10); 82 final int rows = ConsoleMenu.getInt("Number of rows", 100); 83 CSVTestFileCreator.createFile(cols, rows); 84 } 85 86 public void doCSVHeaderAndTrailer() { 87 try { 88 final String mapping = ConsoleMenu.getString("Mapping ", CSVHeaderAndTrailer.getDefaultMapping()); 89 final String data = ConsoleMenu.getString("Data ", CSVHeaderAndTrailer.getDefaultDataFile()); 90 CSVHeaderAndTrailer.call(mapping, data); 91 } catch (final Exception e) { 92 e.printStackTrace(); 93 } 94 } 95 96 public void doCSVPerformanceTest() { 97 try { 98 final String mapping = ConsoleMenu.getString("CSV File ", "SampleCSV.csv"); 99 final boolean data = ConsoleMenu.getBoolean("Traverse the entire parsed file", true); 100 final boolean verbose = ConsoleMenu.getBoolean("Verbose", false); 101 CSVPerformanceTest.call(mapping, verbose, data); 102 } catch (final Exception e) { 103 e.printStackTrace(); 104 } 105 } 106 107 public void doDelimitedWithPZMap() { 108 try { 109 final String mapping = ConsoleMenu.getString("Mapping ", DelimitedWithPZMap.getDefaultMapping()); 110 final String data = ConsoleMenu.getString("Data ", DelimitedWithPZMap.getDefaultDataFile()); 111 DelimitedWithPZMap.call(mapping, data); 112 } catch (final Exception e) { 113 e.printStackTrace(); 114 } 115 } 116 117 public void doDelimitedWithPZMapErrors() { 118 try { 119 final String mapping = ConsoleMenu.getString("Mapping ", DelimitedWithPZMapErrors.getDefaultMapping()); 120 final String data = ConsoleMenu.getString("Data ", DelimitedWithPZMapErrors.getDefaultDataFile()); 121 DelimitedWithPZMapErrors.call(mapping, data); 122 } catch (final Exception e) { 123 e.printStackTrace(); 124 } 125 } 126 127 public void doDelimitedFileExportToExcel() { 128 try { 129 final String mapping = ConsoleMenu.getString("Mapping ", DelimitedFileExportToExcel.getDefaultMapping()); 130 final String data = ConsoleMenu.getString("Data ", DelimitedFileExportToExcel.getDefaultDataFile()); 131 DelimitedFileExportToExcel.call(mapping, data); 132 } catch (final Exception e) { 133 e.printStackTrace(); 134 } 135 } 136 137 public void doFixedLengthWithPZMap() { 138 try { 139 final String mapping = ConsoleMenu.getString("Mapping ", FixedLengthWithPZMap.getDefaultMapping()); 140 final String data = ConsoleMenu.getString("Data ", FixedLengthWithPZMap.getDefaultDataFile()); 141 FixedLengthWithPZMap.call(mapping, data); 142 } catch (final Exception e) { 143 e.printStackTrace(); 144 } 145 } 146 147 public void doFixedLengthHeaderAndTrailer() { 148 try { 149 final String mapping = ConsoleMenu.getString("Mapping ", FixedLengthHeaderAndTrailer.getDefaultMapping()); 150 final String data = ConsoleMenu.getString("Data ", FixedLengthHeaderAndTrailer.getDefaultDataFile()); 151 FixedLengthHeaderAndTrailer.call(mapping, data); 152 } catch (final Exception e) { 153 e.printStackTrace(); 154 } 155 } 156 157 public void doLargeDelimitedWithPZMap() { 158 try { 159 final String mapping = ConsoleMenu.getString("Mapping ", LargeDelimitedWithPZMap.getDefaultMapping()); 160 final String data = ConsoleMenu.getString("Data ", LargeDelimitedWithPZMap.getDefaultDataFile()); 161 LargeDelimitedWithPZMap.call(mapping, data); 162 } catch (final Exception e) { 163 e.printStackTrace(); 164 } 165 } 166 167 public void doLargeFixedLengthWithPZMap() { 168 try { 169 final String mapping = ConsoleMenu.getString("Mapping ", LargeFixedLengthWithPZMap.getDefaultMapping()); 170 final String data = ConsoleMenu.getString("Data ", LargeFixedLengthWithPZMap.getDefaultDataFile()); 171 LargeFixedLengthWithPZMap.call(mapping, data); 172 } catch (final Exception e) { 173 e.printStackTrace(); 174 } 175 } 176 177 public void doCSVLarge() { 178 try { 179 final int cols = ConsoleMenu.getInt("Number of cols", 10); 180 final int rows = ConsoleMenu.getInt("Number of rows", 2000000); 181 final String filename = "LargeSampleCSV.csv"; 182 CSVTestFileCreator.createFile(cols, rows, filename); 183 184 System.out.println("Large file created"); 185 186 CSVLarge.call(filename); 187 } catch (final Exception e) { 188 e.printStackTrace(); 189 } 190 } 191 192 public void doLowLevelParse() { 193 try { 194 final String data = ConsoleMenu.getString("Data ", LowLevelParse.getDefaultDataFile()); 195 LowLevelParse.call(data); 196 } catch (final Exception e) { 197 e.printStackTrace(); 198 } 199 } 200 201 public void doDelimitedMultiLine() { 202 try { 203 final String data = ConsoleMenu.getString("Data ", DelimitedMultiLine.getDefaultDataFile()); 204 DelimitedMultiLine.call(data); 205 } catch (final Exception e) { 206 e.printStackTrace(); 207 } 208 } 209 210 public void doNumericsAndDates() { 211 try { 212 final String mapping = ConsoleMenu.getString("Mapping ", NumericsAndDates.getDefaultMapping()); 213 final String data = ConsoleMenu.getString("Data ", NumericsAndDates.getDefaultDataFile()); 214 NumericsAndDates.call(mapping, data); 215 } catch (final Exception e) { 216 e.printStackTrace(); 217 } 218 } 219 220 public void doGC() { 221 System.gc(); 222 } 223 224 public void doStringBuffer() { 225 final int repeat = ConsoleMenu.getInt("How many times?", 100000); 226 final int characters = ConsoleMenu.getInt("How many char?", 20); 227 228 long start = System.currentTimeMillis(); 229 for (int i = 0; i < repeat; i++) { 230 final StringBuffer sb = new StringBuffer(); 231 for (int u = 0; u < 1000; u++) { 232 sb.append("h"); 233 } 234 } 235 long stop = System.currentTimeMillis(); 236 237 System.out.println("Creating new SB " + (stop - start) + " ms."); 238 239 start = System.currentTimeMillis(); 240 final StringBuffer sb = new StringBuffer(); 241 for (int i = 0; i < repeat; i++) { 242 for (int u = 0; u < characters; u++) { 243 sb.append("h"); 244 } 245 sb.delete(0, sb.length()); 246 } 247 stop = System.currentTimeMillis(); 248 249 System.out.println("Deleting existing SB " + (stop - start) + " ms."); 250 251 } 252 253 public void doTestParsers() { 254 final int repeat = ConsoleMenu.getInt("How many Rows?", 1000); 255 final int numberOfCols = ConsoleMenu.getInt("How many columns?", 100); 256 final boolean qualif = ConsoleMenu.getBoolean("With qualifier?", true); 257 258 final StringBuffer aRow = new StringBuffer(); 259 for (int i = 0; i < numberOfCols; i++) { 260 if (qualif) { 261 aRow.append("\""); 262 } 263 aRow.append("Column ").append(i); 264 if (qualif) { 265 aRow.append("\""); 266 } 267 } 268 269 final String line = aRow.toString(); 270 271 final long start = System.currentTimeMillis(); 272 for (int i = 0; i < repeat; i++) { 273 ParserUtils.splitLine(line, ',', '\"', FPConstants.SPLITLINE_SIZE_INIT); 274 } 275 final long stop = System.currentTimeMillis(); 276 277 System.out.println("ParserUtil " + (stop - start) + " ms."); 278 279 } 280 281 }