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

com.cognitect.transit.impl.WriterFactory Maven / Gradle / Ivy

Go to download

Transit is a data format and a set of libraries for conveying values between applications written in different languages. This library provides support for marshalling Transit data to/from Java.

There is a newer version: 1.0.371
Show newest version
// Copyright (c) Cognitect, Inc.
// All rights reserved.

package com.cognitect.transit.impl;

import com.cognitect.transit.WriteHandler;
import com.cognitect.transit.Writer;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import org.msgpack.MessagePack;
import org.msgpack.packer.Packer;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.function.Function;

public class WriterFactory {

    private static final Map>, WriteHandlerMap> handlerCache = new Cache>, WriteHandlerMap>();

    private static WriteHandlerMap handlerMap(Map> customHandlers) {
        if (customHandlers instanceof WriteHandlerMap)
            return new WriteHandlerMap(customHandlers);

        WriteHandlerMap writeHandlerMap;
        synchronized (handlerCache) {
            writeHandlerMap = handlerCache.get(customHandlers);
            if (writeHandlerMap == null) {
                writeHandlerMap = new WriteHandlerMap(customHandlers);
                handlerCache.put(customHandlers, writeHandlerMap);
            }
        }
        return new WriteHandlerMap(writeHandlerMap);
    }

    private static WriteHandlerMap verboseHandlerMap(Map> customHandlers) {
        return handlerMap(customHandlers).verboseWriteHandlerMap();
    }

    public static  Writer getJsonInstance(final OutputStream out, Map> customHandlers,  WriteHandler defaultWriteHandler, boolean verboseMode) throws IOException {
        return getJsonInstance(out, customHandlers, defaultWriteHandler, verboseMode, null);
    }

    public static  Writer getJsonInstance(final OutputStream out, Map> customHandlers,  WriteHandler defaultWriteHandler, boolean verboseMode, Function transform) throws IOException {

        JsonGenerator gen = new JsonFactory().createGenerator(out);
        final JsonEmitter emitter;

        if (verboseMode) {
            emitter = new JsonVerboseEmitter(gen, verboseHandlerMap(customHandlers), defaultWriteHandler, transform);
        } else {
            emitter = new JsonEmitter(gen, handlerMap(customHandlers), defaultWriteHandler, transform);
        }

        final WriteCache writeCache = new WriteCache(!verboseMode);

        return new Writer() {
            @Override
            public void write(T o) {
                try {
                    emitter.emit(o, false, writeCache.init());
                    out.flush();
                } catch (Throwable e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }

    public static  Writer getMsgpackInstance(final OutputStream out, Map> customHandlers, WriteHandler defaultWriteHandler) throws IOException {
        return getMsgpackInstance(out, customHandlers, defaultWriteHandler, null);
    }

    public static  Writer getMsgpackInstance(final OutputStream out, Map> customHandlers, WriteHandler defaultWriteHandler, Function transform) throws IOException {

        Packer packer = new MessagePack().createPacker(out);

        final MsgpackEmitter emitter = new MsgpackEmitter(packer, handlerMap(customHandlers), defaultWriteHandler, transform);

        final WriteCache writeCache = new WriteCache(true);

        return new Writer() {
            @Override
            public void write(T o) {
                try {
                    emitter.emit(o, false, writeCache.init());
                    out.flush();
                } catch (Throwable e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }

    public static WriteHandler defaultDefaultHandler() {
        return new WriteHandler() {
            private String throwException(Object o) {
                throw new RuntimeException("Not supported " + o);
            }

            @Override
            public String tag(Object o) {
                return throwException(o);
            }

            @Override
            public Object rep(Object o) {
                return throwException(o);
            }

            @Override
            public String stringRep(Object o) {
                return throwException(o);
            }

            @Override
            public WriteHandler getVerboseHandler() {
                return null;
            }
        };
    }

    @Deprecated
    public static  Writer getMsgpackInstance(final OutputStream out, Map> customHandlers) throws IOException {
        return getMsgpackInstance(out, customHandlers, null);
    }

    @Deprecated
    public static  Writer getJsonInstance(final OutputStream out, Map> customHandlers, boolean verboseMode) throws IOException {
        return getJsonInstance(out, customHandlers, null, verboseMode);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy