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

sdmxdl.util.ext.FileFormat Maven / Gradle / Ivy

There is a newer version: 3.0.0-beta.8
Show newest version
package sdmxdl.util.ext;

import nbbrd.design.MightBePromoted;
import nbbrd.io.FileFormatter;
import nbbrd.io.FileParser;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

@lombok.Value
public class FileFormat {

    @lombok.NonNull
    FileParser parser;

    @lombok.NonNull
    FileFormatter formatter;

    @lombok.NonNull
    String fileExtension;

    public static  & FileFormatter> @NonNull FileFormat of(@NonNull F fileFormat, @NonNull String fileExtension) {
        return new FileFormat<>(fileFormat, fileFormat, fileExtension);
    }

    public static  @NonNull FileFormat noOp() {
        return new FileFormat<>(
                noOpParser(),
                noOpFormatter(),
                ""
        );
    }

    public static  @NonNull FileFormat gzip(@NonNull FileFormat delegate) {
        return new FileFormat<>(
                gzipParser(delegate.getParser()),
                gzipFormatter(delegate.getFormatter()),
                delegate.getFileExtension() + ".gz"
        );
    }

    @MightBePromoted
    private static  @NonNull FileParser noOpParser() {
        return (resource) -> {
            throw new IOException("Cannot parse stream");
        };
    }

    @MightBePromoted
    private static  @NonNull FileFormatter noOpFormatter() {
        return (value, resource) -> {
            throw new IOException("Cannot format stream");
        };
    }

    @MightBePromoted
    private static  @NonNull FileParser gzipParser(@NonNull FileParser delegate) {
        return (resource) -> {
            try (InputStream gzip = new GZIPInputStream(resource)) {
                return delegate.parseStream(gzip);
            }
        };
    }

    @MightBePromoted
    private static  @NonNull FileFormatter gzipFormatter(@NonNull FileFormatter delegate) {
        return (value, resource) -> {
            try (OutputStream gzip = new GZIPOutputStream(resource)) {
                delegate.formatStream(value, gzip);
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy