package org.hexpresso.elm327.commands.filters;

import org.hexpresso.elm327.commands.Response;
import org.hexpresso.elm327.commands.ResponseFilter;

import java.util.ArrayList;
import java.util.ListIterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Pierre-Etienne Messier <pierre.etienne.messier@gmail.com> on 2015-10-25.
 */
public class RegularExpressionResponseFilter implements ResponseFilter {
    private Pattern mPattern = null;

    public RegularExpressionResponseFilter(String regularExpression) {
        mPattern = Pattern.compile(regularExpression);
    }

    public RegularExpressionResponseFilter(Pattern pattern) {
        mPattern = pattern;
    }

    @Override
    public void onResponseReceived(Response response) {
        ArrayList<String> lines = response.getLines();

        ListIterator<String> it = lines.listIterator();
        while(it.hasNext()) {
            String line = it.next();

            Matcher m = mPattern.matcher(line);
            if(m.matches()) {
                final int groupCount = m.groupCount();
                if( groupCount == 1) {
                    it.set(m.group(1));
                }
                else if( groupCount > 1) {
                    throw new RuntimeException("Unsupported pattern match with more than one group");
                }
            }
            else {
                it.remove();
            }
        }
    }
}
@@ -176,16 +176,40 @@
 yList;%0D%0A
+import java.util.List;%0D%0A
 import j
@@ -830,21 +830,16 @@
         
-Array
 List%3CStr

Fix build error (my fault)