<commit_before>package com.alpha.csv.serialize;

import java.io.IOException;
import java.lang.reflect.Field;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

import com.alpha.csv.base.CsvMetaNode;
import com.alpha.csv.base.CsvMetaTreeBuilder;
import com.alpha.csv.base.CsvMetaTreeBuilder.CsvMetaTree;
import com.alpha.csv.convertor.Converter;
import com.alpha.csv.exceptions.CsvException;
import com.alpha.csv.resolver.CsvMetaTreeResolver;
import com.alpha.csv.resolver.PropertyResolver;
import com.alpha.csv.util.Utils;

/**
 * Use following method to serialize object:
 * <p>{@link #serialize(Object)}
 * <p>{@link #serialize(Object, CSVFormat)}
 * <p>{@link #serialize(Iterable, Class)}
 * <p>{@link #serialize(Iterable, Class, CSVFormat)}
 * */
public class CsvSerializer {
    
    public static String serialize(final Object object) throws CsvException, IOException {
        return serialize(object, CSVFormat.DEFAULT);
    }

    public static String serialize(final Object object, final CSVFormat csvFormat) throws CsvException, IOException {
        if(object.getClass().isArray()) {
            return serialize0((Object[])object, CSVFormat.DEFAULT);
        }else {
            return serialize0(object, CSVFormat.DEFAULT);
        }
    }
    
    public static <T extends Iterable<?>> String serialize(final T iterable, final Class<?> clazz) throws CsvException, IOException {
        return serialize(iterable, clazz, CSVFormat.DEFAULT);
    }
    
    public static <T extends Iterable<?>> String serialize(final T iterable, final Class<?> clazz, final CSVFormat csvFormat) throws CsvException, IOException {
        return serialize0(iterable, clazz, csvFormat);
    }
    
    private static String serialize0(final Object object, final CSVFormat csvFormat) throws CsvException, IOException {
        Class<?> clazz = object.getClass();
        CsvMetaNode[] csvMetaNodes = resolveClass(clazz);
        CSVPrinter printer = new CSVPrinter(new StringBuilder(), csvFormat);
        printHeader(printer, csvMetaNodes);
        printObject(printer, csvMetaNodes, object);
        return printer.getOut().toString();
    }
    
    private static String serialize0(final Object[] object, final CSVFormat csvFormat) throws CsvException, IOException {
        Class<?> clazz = object.getClass();
        CsvMetaNode[] csvMetaNodes = resolveClass(clazz);
        CSVPrinter printer = new CSVPrinter(new StringBuilder(), csvFormat);
        printHeader(printer, csvMetaNodes);
        printObjects(printer, csvMetaNodes, object);
        return printer.getOut().toString();
    }
    
    private static <T extends Iterable<?>> String serialize0(final T iterable, final Class<?> clazz, final CSVFormat csvFormat) throws CsvException, IOException {
        long start = System.currentTimeMillis();
        CsvMetaNode[] csvMetaNodes = resolveClass(clazz);
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        start = end;
        CSVPrinter printer = new CSVPrinter(new StringBuilder(), csvFormat);
        printHeader(printer, csvMetaNodes);
        end = System.currentTimeMillis();
        System.out.println(end - start);
        start = end;
        printObjects(printer, csvMetaNodes, iterable);
        end = System.currentTimeMillis();
        System.out.println(end - start);
        start = end;
        return printer.getOut().toString();
    }
    
    private static CsvMetaNode[] resolveClass(Class<?> clazz) throws CsvException {
        Field[] fields = clazz.getDeclaredFields();
        fields = PropertyResolver.filterIgnoreProperties(clazz, fields);
        CsvMetaTree csvMetaTree = CsvMetaTreeBuilder.buildCsvMetaTree(fields);
        CsvMetaTreeResolver.scanCsvMetaTree(csvMetaTree);
        CsvMetaTreeResolver.resolveCsvMetaTree(csvMetaTree);
        CsvMetaTreeResolver.sortCsvMetaTree(csvMetaTree);
        CsvMetaNode[] csvMetaNodes = new CsvMetaNode[csvMetaTree.getLeafNodeCount()];
        Utils.convertCsvMetaTreeIntoArray(csvMetaTree.getRoot(), csvMetaNodes, 0);
        return csvMetaNodes;
    }
    
    
    private static void printHeader(final CSVPrinter printer, final CsvMetaNode[] csvMetaNodes) throws CsvException, IOException {
        for (CsvMetaNode csvMetaNode : csvMetaNodes) {
            CsvMetaNode[] path = CsvMetaTreeResolver.resolveNodePath(csvMetaNode);
            csvMetaNode.setPath(path);
            printer.print(csvMetaNode.getCsvMetaInfo().getHeader());
        }
        printer.println();
    }
    
    private static <T extends Iterable<?>> void printObjects(final CSVPrinter printer, final CsvMetaNode[] csvMetaNodes, final T iterable) throws CsvException, IOException {
        for (Object obj : iterable) {
            printObject(printer, csvMetaNodes, obj);
        }
    }
    <commit_msg>remove metric code<commit_after>package com.alpha.csv.serialize;

import java.io.IOException;
import java.lang.reflect.Field;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

import com.alpha.csv.base.CsvMetaNode;
import com.alpha.csv.base.CsvMetaTreeBuilder;
import com.alpha.csv.base.CsvMetaTreeBuilder.CsvMetaTree;
import com.alpha.csv.convertor.Converter;
import com.alpha.csv.exceptions.CsvException;
import com.alpha.csv.resolver.CsvMetaTreeResolver;
import com.alpha.csv.resolver.PropertyResolver;
import com.alpha.csv.util.Utils;

/**
 * Use following method to serialize object:
 * <p>{@link #serialize(Object)}
 * <p>{@link #serialize(Object, CSVFormat)}
 * <p>{@link #serialize(Iterable, Class)}
 * <p>{@link #serialize(Iterable, Class, CSVFormat)}
 * */
public class CsvSerializer {
    
    public static String serialize(final Object object) throws CsvException, IOException {
        return serialize(object, CSVFormat.DEFAULT);
    }

    public static String serialize(final Object object, final CSVFormat csvFormat) throws CsvException, IOException {
        if(object.getClass().isArray()) {
            return serialize0((Object[])object, CSVFormat.DEFAULT);
        }else {
            return serialize0(object, CSVFormat.DEFAULT);
        }
    }
    
    public static <T extends Iterable<?>> String serialize(final T iterable, final Class<?> clazz) throws CsvException, IOException {
        return serialize(iterable, clazz, CSVFormat.DEFAULT);
    }
    
    public static <T extends Iterable<?>> String serialize(final T iterable, final Class<?> clazz, final CSVFormat csvFormat) throws CsvException, IOException {
        return serialize0(iterable, clazz, csvFormat);
    }
    
    private static String serialize0(final Object object, final CSVFormat csvFormat) throws CsvException, IOException {
        Class<?> clazz = object.getClass();
        CsvMetaNode[] csvMetaNodes = resolveClass(clazz);
        CSVPrinter printer = new CSVPrinter(new StringBuilder(), csvFormat);
        printHeader(printer, csvMetaNodes);
        printObject(printer, csvMetaNodes, object);
        return printer.getOut().toString();
    }
    
    private static String serialize0(final Object[] object, final CSVFormat csvFormat) throws CsvException, IOException {
        Class<?> clazz = object.getClass();
        CsvMetaNode[] csvMetaNodes = resolveClass(clazz);
        CSVPrinter printer = new CSVPrinter(new StringBuilder(), csvFormat);
        printHeader(printer, csvMetaNodes);
        printObjects(printer, csvMetaNodes, object);
        return printer.getOut().toString();
    }
    
    private static <T extends Iterable<?>> String serialize0(final T iterable, final Class<?> clazz, final CSVFormat csvFormat) throws CsvException, IOException {
        CsvMetaNode[] csvMetaNodes = resolveClass(clazz);
        CSVPrinter printer = new CSVPrinter(new StringBuilder(), csvFormat);
        printHeader(printer, csvMetaNodes);
        printObjects(printer, csvMetaNodes, iterable);
        return printer.getOut().toString();
    }
    
    private static CsvMetaNode[] resolveClass(Class<?> clazz) throws CsvException {
        Field[] fields = clazz.getDeclaredFields();
        fields = PropertyResolver.filterIgnoreProperties(clazz, fields);
        CsvMetaTree csvMetaTree = CsvMetaTreeBuilder.buildCsvMetaTree(fields);
        CsvMetaTreeResolver.scanCsvMetaTree(csvMetaTree);
        CsvMetaTreeResolver.resolveCsvMetaTree(csvMetaTree);
        CsvMetaTreeResolver.sortCsvMetaTree(csvMetaTree);
        CsvMetaNode[] csvMetaNodes = new CsvMetaNode[csvMetaTree.getLeafNodeCount()];
        Utils.convertCsvMetaTreeIntoArray(csvMetaTree.getRoot(), csvMetaNodes, 0);
        return csvMetaNodes;
    }
    
    
    private static void printHeader(final CSVPrinter printer, final CsvMetaNode[] csvMetaNodes) throws CsvException, IOException {
        for (CsvMetaNode csvMetaNode : csvMetaNodes) {
            CsvMetaNode[] path = CsvMetaTreeResolver.resolveNodePath(csvMetaNode);
            csvMetaNode.setPath(path);
            printer.print(csvMetaNode.getCsvMetaInfo().getHeader());
        }
        printer.println();
    }
    
    private static <T extends Iterable<?>> void printObjects(final CSVPrinter printer, final CsvMetaNode[] csvMetaNodes, final T iterable) throws CsvException, IOException {
        for (Object obj : iterable) {
            printObject(printer, csvMetaNodes, obj);
        }
    }
    
{'fraction_non_alphanumeric': 0.10067473492556496, 'fraction_numerical': 0.0014994109456999037, 'mean_word_length': 3.6736736736736737, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, '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': '2557166', 'n_tokens_mistral': 2661, 'n_tokens_neox': 2604, 'n_words': 606}