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

fun.mike.flapjack.pipeline.lab.FlatFileOutputChannel Maven / Gradle / Ivy

package fun.mike.flapjack.pipeline.lab;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

import fun.mike.flapjack.alpha.DelimitedFormat;
import fun.mike.flapjack.alpha.Format;
import fun.mike.flapjack.alpha.SerializationResult;
import fun.mike.io.alpha.Spitter;
import fun.mike.record.alpha.Record;

public class FlatFileOutputChannel implements OutputChannel {
    private final Spitter spitter;
    private final Format format;

    private final List errors;

    public FlatFileOutputChannel(String path, Format format, Boolean includeHeader) {
        this.format = format;

        this.spitter = new Spitter(path);

        if(includeHeader && format instanceof DelimitedFormat) {
            spitter.spit(HeaderBuilder.build((DelimitedFormat) format));
        }

        this.errors = new LinkedList<>();
    }

    @Override
    public Optional put(int number, String line, Record value) {
        SerializationResult serializationResult = format.serialize(value);

        if (serializationResult.isOk()) {
            String outputLine = serializationResult.getValue();
            spitter.spit(outputLine);
            return Optional.empty();
        }

        return Optional.of(SerializationPipelineError.fromResult(number, line, serializationResult));
    }

    public List getErrors() {
        return errors;
    }

    @Override
    public Nothing getValue() {
        return Nothing.value();
    }

    @Override
    public void close() {
        spitter.close();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy