package com.metamx.common.parsers;

import com.google.common.collect.Lists;
import com.metamx.common.collect.Utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

public class CSVParser implements Parser<String, String>
{
  protected ArrayList<String> fieldnames;
  protected au.com.bytecode.opencsv.CSVParser parser = new au.com.bytecode.opencsv.CSVParser();

  public CSVParser(Iterable<String> fieldnames) {
    this.fieldnames = Lists.newArrayList(fieldnames);
  }

  public CSVParser(String header) throws IOException {
    this.fieldnames = Lists.newArrayList(parser.parseLine(header));
  }
  
  @Override
  public Map<String, String> parse(String input) throws IOException
  {
    final String[] values = parser.parseLine(input);

    try {
      return Utils.zipMap(fieldnames.toArray(new String[]{}), values, true);
    }
    catch(IllegalArgumentException e) {
      throw new IOException(e.getMessage());
    }
  }
}

@@ -25,24 +25,68 @@
 n.parsers;%0A%0A
+import com.google.common.collect.Iterables;%0A
 import com.g
@@ -152,16 +152,62 @@
 t.Utils;
+%0Aimport sun.security.pkcs11.wrapper.Functions;
 %0A%0Aimport
@@ -256,16 +256,41 @@
 ayList;%0A
+import java.util.Arrays;%0A
 import j
@@ -833,14 +833,8 @@
     
-final 
 Stri
@@ -871,16 +871,102 @@
 (input);
+%0A    for(int i = 0; i %3C values.length; ++i) if(values%5Bi%5D.equals(%22%22)) values%5Bi%5D = null;
 %0A%0A    tr

make CSV parser consistent with other delimited string parsers and return null for empty string