package com.cookpad.puree.outputs;

import com.cookpad.puree.PureeFilter;
import com.cookpad.puree.storage.PureeStorage;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public abstract class PureeOutput {
    protected OutputConfiguration conf;
    protected PureeStorage storage;
    protected List<PureeFilter> filters = new ArrayList<>();

    public void registerFilter(PureeFilter filter) {
        filters.add(filter);
    }

    public void initialize(PureeStorage storage) {
        this.storage = storage;
        this.conf = configure(new OutputConfiguration());
    }

    public void receive(JSONObject jsonLog) {
        try {
            final JSONObject filteredLog = applyFilters(jsonLog);
            if (filteredLog == null) {
                return;
            }

            emit(filteredLog);
        } catch (JSONException ignored) {
        }
    }

    protected JSONObject applyFilters(JSONObject jsonLog) throws JSONException {
        JSONObject filteredLog = jsonLog;
        for (PureeFilter filter : filters) {
            filteredLog = filter.apply(filteredLog);
            if (filteredLog == null) {
                return null;
            }
        }
        return filteredLog;
    }

    public void flush() {
        // do nothing because PureeOutput don't have any buffers.
    }

    public abstract String type();

    public abstract OutputConfiguration configure(OutputConfiguration conf);

    public abstract void emit(JSONObject jsonLog);
}


@@ -585,39 +585,185 @@
     
-this.conf = configure(new Outpu
+OutputConfiguration defaultConfiguration = new OutputConfiguration();%0A        this.conf = configure(defaultConfiguration);%0A%0A        if (conf == null) %7B%0A            conf = defaul
 tCon
@@ -776,12 +776,19 @@
 tion
-());
+;%0A        %7D
 %0A   

Check OutputConfiguration for avoiding NPE