package com.github.blindpirate.gogradle.build;

import com.github.blindpirate.gogradle.util.ExceptionHandler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;

import static com.github.blindpirate.gogradle.GogradleGlobal.DEFAULT_CHARSET;

public class SubprocessReader extends Thread {
    private Supplier<InputStream> is;
    private CountDownLatch latch;
    private Consumer<String> consumer;
    private Predicate<String> lineFilter = line -> true;

    public SubprocessReader(Supplier<InputStream> is,
                            Consumer<String> consumer,
                            CountDownLatch latch) {
        this.is = is;
        this.latch = latch;
        this.consumer = consumer;
    }

    public SubprocessReader(Supplier<InputStream> is,
                            Consumer<String> consumer,
                            CountDownLatch latch,
                            Predicate<String> lineFilter) {
        this.is = is;
        this.latch = latch;
        this.consumer = consumer;
        this.lineFilter = lineFilter;
    }

    @Override
    public void run() {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(is.get(), DEFAULT_CHARSET))) {
            String line;
            while ((line = br.readLine()) != null) {
                if (lineFilter.test(line)) {
                    consumer.accept(line);
                }
            }
        } catch (IOException e) {
            consumer.accept(ExceptionHandler.getStackTrace(e));
        } finally {
            latch.countDown();
        }
    }
}

@@ -619,65 +619,8 @@
 mer;
-%0A    private Predicate%3CString%3E lineFilter = line -%3E true;
 %0A%0A  
@@ -873,356 +873,8 @@
  %7D%0A%0A
-    public SubprocessReader(Supplier%3CInputStream%3E is,%0A                            Consumer%3CString%3E consumer,%0A                            CountDownLatch latch,%0A                            Predicate%3CString%3E lineFilter) %7B%0A        this.is = is;%0A        this.latch = latch;%0A        this.consumer = consumer;%0A        this.lineFilter = lineFilter;%0A    %7D%0A%0A
     
@@ -1094,57 +1094,8 @@
 ) %7B%0A
-                if (lineFilter.test(line)) %7B%0A    
     
@@ -1133,26 +1133,8 @@
 e);%0A
-                %7D%0A
     

Remove predicate in SubprocessReader