Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
package com.presidentio.testdatagenerator.output;
import java.util.*;
public abstract class AbstractBufferedSink implements Sink {
    private Map<TemplateList<Map<StringObject>>> buffer = new HashMap<>();
    private Formatter formatter;
    private int bufferSize = 1000;
    private boolean bufferingEnabled = true;
    protected void init(Formatter formatter) {
        this. = formatter;
    }
    @Override
    public void process(Template templateMap<StringObjectmap) {
        if ( && .supportMultiInsert()) {
            addToBuffer(templatemap);
        } else {
            String data = .format(maptemplate);
            write(data);
        }
    }
    public abstract void write(String bytes);
    private synchronized void addToBuffer(Template templateMap<StringObjectmap) {
        List<Map<StringObject>> items = .get(template);
        if (items == null) {
            items = new ArrayList<>();
            .put(templateitems);
        }
        items.add(map);
        if (items.size() >= ) {
            flush(templateitems);
            items.clear();
        }
    }
    private synchronized void flush(Template templateList<Map<StringObject>> items) {
        if (items != null && !items.isEmpty()) {
            String data = .format(itemstemplate);
            write(data);
        }
    }
    @Override
    public synchronized void close() {
        for (Template template : .keySet()) {
            List<Map<StringObject>> items = .get(template);
            flush(templateitems);
            items.clear();
        }
    }
    public void setBufferSize(int bufferSize) {
        this. = bufferSize;
    }
    public void setBufferingEnabled(boolean bufferingEnabled) {
        this. = bufferingEnabled;
    }
New to GrepCode? Check out our FAQ X