package com.github.fabienrenaud.jjb;

import javax.json.spi.JsonProvider;
import java.io.ByteArrayOutputStream;

/**
 *
 * @author Fabien Renaud
 */
public final class JsonUtils {

    private JsonUtils() {
    }

    public static void printJavaxJsonProvider() {
        System.out.println("Jsonp Provider:" + JsonProvider.provider().getClass().getCanonicalName());
        System.out.println();
    }

    public static ByteArrayOutputStream byteArrayOutputStream() {
        ByteArrayOutputStream baos = THREAD_BYTE_ARRAY_OUTPUT_STREAM.get();
        baos.reset();
        return baos;
    }

    public static StringBuilder stringBuilder() {
        StringBuilder b = THREAD_STRING_BUILDER.get();
        b.setLength(0);
        return b;
    }

    public static com.dslplatform.json.JsonWriter dslJsonWriter() {
        com.dslplatform.json.JsonWriter writer = THREAD_DSL_WRITER.get();
        writer.reset();
        return writer;
    }

    private static final ThreadLocal<ByteArrayOutputStream> THREAD_BYTE_ARRAY_OUTPUT_STREAM = new ThreadLocal<ByteArrayOutputStream>() {
        @Override
        protected ByteArrayOutputStream initialValue() {
            return new ByteArrayOutputStream();
        }
    };

    private static final ThreadLocal<StringBuilder> THREAD_STRING_BUILDER = new ThreadLocal<StringBuilder>() {
        @Override
        protected StringBuilder initialValue() {
            return new StringBuilder();
        }
    };

    private static final ThreadLocal<com.dslplatform.json.JsonWriter> THREAD_DSL_WRITER = new ThreadLocal<com.dslplatform.json.JsonWriter>() {
        @Override
        protected com.dslplatform.json.JsonWriter initialValue() {
            return new com.dslplatform.json.JsonWriter();
        }
    };

}

@@ -31,16 +31,57 @@
 d.jjb;%0A%0A
+import com.dslplatform.json.JsonWriter;%0A%0A
 import j
@@ -1073,20 +1073,16 @@
 STREAM =
- new
  ThreadL
@@ -1089,174 +1089,48 @@
 ocal
-%3CByteArrayOutputStream%3E() %7B%0A        @Override%0A        protected ByteArrayOutputStream initialValue() %7B%0A            return new ByteArrayOutputStream();%0A        %7D%0A    %7D
+.withInitial(ByteArrayOutputStream::new)
 ;%0A%0A 
@@ -1199,28 +1199,24 @@
 NG_BUILDER =
- new
  ThreadLocal
@@ -1219,150 +1219,40 @@
 ocal
-%3CStringBuilder%3E() %7B%0A        @Override%0A        protected StringBuilder initialValue() %7B%0A            return new StringBuilder();%0A        %7D%0A    %7D
+.withInitial(StringBuilder::new)
 ;%0A%0A 
@@ -1339,20 +1339,16 @@
 WRITER =
- new
  ThreadL
@@ -1355,204 +1355,37 @@
 ocal
-%3Ccom.dslplatform.json.JsonWriter%3E() %7B%0A        @Override%0A        protected com.dslplatform.json.JsonWriter initialValue() %7B%0A            return new com.dslplatform.json.JsonWriter();%0A        %7D%0A    %7D
+.withInitial(JsonWriter::new)
 ;%0A%0A%7D

Use ThreadLocal.withInitial in JsonUtils