All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fiolino.common.processing.sink.AggregatingSink Maven / Gradle / Ivy

Go to download

General structure to easily create dynamic logic via MethodHandles and others.

There is a newer version: 1.0.10
Show newest version
package org.fiolino.common.processing.sink;

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

import org.fiolino.common.container.Container;

/**
 * This sink aggregates elements in a list and sends it then to the target.
 * 

* The list is flushed when the given size is reached, or in case of a commit. *

* This class is not thread safe. *

* Created by kuli on 27.03.16. */ public final class AggregatingSink extends ChainedSink> implements CloneableSink> { private List list; private final int chunkSize; private Container metadata = Container.empty(); public AggregatingSink(Sink> target, int chunkSize) { super(target); this.chunkSize = chunkSize; initializeList(); } private void initializeList() { list = new ArrayList<>(chunkSize); } @Override public void accept(T value, Container metadata) throws Exception { list.add(value); if (list.size() >= chunkSize) { flush(); } else { this.metadata = metadata; } } @Override public void commit(Container metadata) throws Exception { flush(); super.commit(metadata); } @Override public void partialCommit(Container metadata) throws Exception { flush(); if (getTarget() instanceof CloneableSink) { ((CloneableSink) getTarget()).partialCommit(metadata); } } private void flush() throws Exception { if (!list.isEmpty()) { getTarget().accept(list, metadata); initializeList(); this.metadata = Container.empty(); } } @Override public AggregatingSink createClone() { return new AggregatingSink<>(targetForCloning(), chunkSize); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy