<commit_before>/*
 * The MIT License (MIT)
 * <p/>
 * Copyright (c) 2016 Vimeo
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.vimeo.stag.processor.generators;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;
import com.vimeo.stag.GsonAdapterKey;
import com.vimeo.stag.processor.generators.model.AnnotatedClass;
import com.vimeo.stag.processor.generators.model.ClassInfo;
import com.vimeo.stag.processor.generators.model.SupportedTypesModel;
import com.vimeo.stag.processor.utils.FileGenUtils;
import com.vimeo.stag.processor.utils.TypeUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.processing.Filer;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;

@SuppressWarnings("StringConcatenationMissingWhitespace")
public class ParseGenerator {

    private static final String CLASS_PARSE_UTILS = "ParseUtils";

    @NotNull
    private final Set<String> mSupportedTypes;

    @NotNull
    private final Filer mFiler;

    public ParseGenerator(@NotNull Set<String> supportedTypes, @NotNull Filer filer) {
        mSupportedTypes = new HashSet<>(supportedTypes);
        mFiler = filer;
    }

    @NotNull
    private static MethodSpec generateParseArraySpec() {

        TypeVariableName genericTypeName = TypeVariableName.get("T");

        return MethodSpec.methodBuilder("parseArray")
                .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
                .addTypeVariable(genericTypeName)
                .returns(ParameterizedTypeName.get(ClassName.get(ArrayList.class), genericTypeName))
                .addParameter(Gson.class, "gson")
                .addParameter(JsonReader.class, "reader")
                .addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), genericTypeName), "clazz")
                .addException(IOException.class)
                .addCode("if(reader.peek() != com.google.gson.stream.JsonToken.BEGIN_ARRAY) {\n" +
                         "\treader.skipValue();\n" +
                         "\treturn null;\n}\n" +
                         "reader.beginArray();\n" +
                         '\n' +
                         "ArrayList<" + genericTypeName.name + "> list = " + StagGenerator.CLASS_STAG +
                         ".readListFromAdapter(gson, clazz, reader);\n" +
                         '\n' +
                         "reader.endArray();\n" +
                         "return list;\n")
                .build();
    }

    @NotNull
    private static MethodSpec generateWriteArraySpec() {
        TypeVariableName genericType = TypeVariableName.get("T");
        return MethodSpec.methodBuilder("write")
                .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
                .returns(void.class)
                .addTypeVariable(genericType)
                .addException(IOException.class)
                .addParameter(Gson.class, "gson")
                .addParameter(JsonWriter.class, "writer")
                .addParameter(Class.class, "clazz")
                .addParameter(ParameterizedTypeName.get(ClassName.get(ArrayList.class), genericType), "list")
                .addCode("if (list == null) {\n" +
                         "\treturn;\n" +
                         "}\n" +
                         "writer.beginArray();\n" +
                         '\n' +
                         "Stag.writeListToAdapter(gson, clazz, writer, list);\n" +
                         '\n' +
                         "writer.endArray();\n")
                .build();
    }

    /**
     * Generates the ParseUtils class. This class includes
     * parsing/write methods for JsonArray -> ArrayList, and
     * the parsing/write methods for all objects supported by
     * the Stag library.
     *
     * @throws IOException thrown if we are unable to write
     *                     to the source file for ParseUtils. Most likely the
     *                     file is being held by another process, barring us from
     *                     modifying it.
     */
    public void generateParsingCode() throws IOException {
        TypeSpec.Builder typeSpecBuilder =
                TypeSpec.classBuilder(CLASS_PARSE_UTILS).addModifiers(Modifier.FINAL);

        typeSpecBuilder.addMethod(ParseGenerator.generateParseArraySpec());
        typeSpecBuilder.addMethod(ParseGenerator.generateWriteArraySpec());
<commit_msg>Correct rawtypes lint issue in write array spec<commit_after>/*
 * The MIT License (MIT)
 * <p/>
 * Copyright (c) 2016 Vimeo
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.vimeo.stag.processor.generators;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;
import com.vimeo.stag.GsonAdapterKey;
import com.vimeo.stag.processor.generators.model.AnnotatedClass;
import com.vimeo.stag.processor.generators.model.ClassInfo;
import com.vimeo.stag.processor.generators.model.SupportedTypesModel;
import com.vimeo.stag.processor.utils.FileGenUtils;
import com.vimeo.stag.processor.utils.TypeUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.processing.Filer;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;

@SuppressWarnings("StringConcatenationMissingWhitespace")
public class ParseGenerator {

    private static final String CLASS_PARSE_UTILS = "ParseUtils";

    @NotNull
    private final Set<String> mSupportedTypes;

    @NotNull
    private final Filer mFiler;

    public ParseGenerator(@NotNull Set<String> supportedTypes, @NotNull Filer filer) {
        mSupportedTypes = new HashSet<>(supportedTypes);
        mFiler = filer;
    }

    @NotNull
    private static MethodSpec generateParseArraySpec() {

        TypeVariableName genericTypeName = TypeVariableName.get("T");

        return MethodSpec.methodBuilder("parseArray")
                .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
                .addTypeVariable(genericTypeName)
                .returns(ParameterizedTypeName.get(ClassName.get(ArrayList.class), genericTypeName))
                .addParameter(Gson.class, "gson")
                .addParameter(JsonReader.class, "reader")
                .addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), genericTypeName), "clazz")
                .addException(IOException.class)
                .addCode("if(reader.peek() != com.google.gson.stream.JsonToken.BEGIN_ARRAY) {\n" +
                         "\treader.skipValue();\n" +
                         "\treturn null;\n}\n" +
                         "reader.beginArray();\n" +
                         '\n' +
                         "ArrayList<" + genericTypeName.name + "> list = " + StagGenerator.CLASS_STAG +
                         ".readListFromAdapter(gson, clazz, reader);\n" +
                         '\n' +
                         "reader.endArray();\n" +
                         "return list;\n")
                .build();
    }

    @NotNull
    private static MethodSpec generateWriteArraySpec() {
        TypeVariableName genericType = TypeVariableName.get("T");
        return MethodSpec.methodBuilder("write")
                .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
                .returns(void.class)
                .addTypeVariable(genericType)
                .addException(IOException.class)
                .addParameter(Gson.class, "gson")
                .addParameter(JsonWriter.class, "writer")
                .addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), genericType), "clazz")
                .addParameter(ParameterizedTypeName.get(ClassName.get(ArrayList.class), genericType), "list")
                .addCode("if (list == null) {\n" +
                         "\treturn;\n" +
                         "}\n" +
                         "writer.beginArray();\n" +
                         '\n' +
                         "Stag.writeListToAdapter(gson, clazz, writer, list);\n" +
                         '\n' +
                         "writer.endArray();\n")
                .build();
    }

    /**
     * Generates the ParseUtils class. This class includes
     * parsing/write methods for JsonArray -> ArrayList, and
     * the parsing/write methods for all objects supported by
     * the Stag library.
     *
     * @throws IOException thrown if we are unable to write
     *                     to the source file for ParseUtils. Most likely the
     *                     file is being held by another process, barring us from
     *                     modifying it.
     */
    public void generateParsingCode() throws IOException {
        TypeSpec.Builder typeSpecBuilder =
                TypeSpec.classBuilder(CLASS_PARSE_UTILS).addModifiers(Modifier.FINAL);

        typeSpecBuilder.addMethod(ParseGenerator.generateParseArraySpec());
        typeSpecBuilder.addMethod(ParseGenerator.generateWriteArraySpec());

{'fraction_non_alphanumeric': 0.09706583381277226, 'fraction_numerical': 0.0006575162324319882, 'mean_word_length': 2.5777712437518376, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5456421', 'n_tokens_mistral': 3359, 'n_tokens_neox': 3193, 'n_words': 834}