// Copyright (c) Cognitect, Inc.
// All rights reserved.

package com.cognitect.transit;


import com.cognitect.transit.impl.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

public class TransitFactory {
    public static enum Format { JSON, MSGPACK }

    public static Writer writer(Format type, OutputStream out)  throws IOException, IllegalArgumentException {
        return writer(type, out, null);
    }

    public static Writer writer(Format type, OutputStream out, Map<Class, Handler> customHandlers) throws IOException, IllegalArgumentException {
        switch (type) {
            case JSON:    return WriterImpl.getJsonInstance(out, customHandlers);
            case MSGPACK: return WriterImpl.getMsgpackInstance(out, customHandlers);
            default: throw new IllegalArgumentException("Unknown Writer type: " + type.toString());
        }
    }

    public static Reader reader(Format type, InputStream in) throws IOException, IllegalArgumentException {
        return reader(type, in, null);
    }

    public static Reader reader(Format type, InputStream in, Map<String, Decoder> customDecoders) throws IOException, IllegalArgumentException {
        return reader(type, in, customDecoders, new MapBuilderImpl(), new ListBuilderImpl(), new ArrayBuilderImpl(), new SetBuilderImpl());
    }

    public static Reader reader(Format type, InputStream in,
                                Map<String, Decoder> customDecoders,
                                MapBuilder mapBuilder, ListBuilder listBuilder,
                                ArrayBuilder arrayBuilder, SetBuilder setBuilder) throws IOException, IllegalArgumentException {
        switch (type) {
            case JSON:    return ReaderImpl.getJsonInstance(in, customDecoders, mapBuilder, listBuilder, arrayBuilder, setBuilder);
            case MSGPACK: return ReaderImpl.getMsgpackInstance(in, customDecoders, mapBuilder, listBuilder, arrayBuilder, setBuilder);
            default: throw new IllegalArgumentException("Unknown Reader type: " + type.toString());
        }
    }

    public static Keyword keyword(Object o) {
        if (o instanceof Keyword)
            return (Keyword) o;
        else if (o instanceof String)
            return new KeywordImpl((String) o);
        else throw new IllegalArgumentException("Cannot make keyword from " + o.getClass().getSimpleName());
    }

    public static Symbol symbol(Object o) {
        if (o instanceof Symbol)
            return (Symbol) o;
        else if (o instanceof String)
            return new SymbolImpl((String) o);
        else throw new IllegalArgumentException("Cannot make keyword from " + o.getClass().getSimpleName());
    }

    public static TaggedValue taggedValue(String tag, Object rep) {
        return new TaggedValueImpl(tag, rep);
    }

    public static TaggedValue taggedValue(String tag, Object rep, String stringRep) {
        return new TaggedValueImpl(tag, rep, stringRep);
    }

    public static Map defaultDecoders = ReaderImpl.defaultDecoders();
    public static Map defaultHandlers = WriterImpl.defaultHandlers();
    public static MapBuilder defaultMapBuilder = new MapBuilderImpl();
    public static ArrayBuilder defaultArrayBuilder = new ArrayBuilderImpl();
    public static ListBuilder defaultListBuilder = new ListBuilderImpl();
    public static SetBuilder defaultSetBuilder = new SetBuilderImpl();
}

@@ -3045,18 +3045,27 @@
 Decoders
- =
+() %7B return
  ReaderI
@@ -3082,24 +3082,26 @@
 tDecoders();
+ %7D
 %0A    public 
@@ -3126,18 +3126,27 @@
 Handlers
- =
+() %7B return
  WriterI
@@ -3167,16 +3167,18 @@
 dlers();
+ %7D
 %0A    pub
@@ -3212,26 +3212,35 @@
 ltMapBuilder
- =
+() %7B return
  new MapBuil
@@ -3241,32 +3241,34 @@
 apBuilderImpl();
+ %7D
 %0A    public stat
@@ -3298,26 +3298,35 @@
 ArrayBuilder
- =
+() %7B return
  new ArrayBu
@@ -3329,32 +3329,34 @@
 ayBuilderImpl();
+ %7D
 %0A    public stat
@@ -3384,26 +3384,35 @@
 tListBuilder
- =
+() %7B return
  new ListBui
@@ -3422,16 +3422,18 @@
 rImpl();
+ %7D
 %0A    pub
@@ -3475,10 +3475,19 @@
 lder
- =
+() %7B return
  new
@@ -3504,11 +3504,13 @@
 rImpl();
+ %7D
 %0A%7D%0A

Change default decoders/handlers/builders to return new instances each time