<commit_before>/*
 *  Copyright 2021 Cojen.org
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.cojen.tupl.rows;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import org.cojen.maker.ClassMaker;

/**
 * Cache of objects used by code generation.
 *
 * @author <NAME>
 */
class RowGen {
    final RowInfo info;

    private volatile String[] mStateFields;
    private volatile Map<String, Integer> mColumnNumbers;
    private volatile ColumnCodec[] mKeyCodecs;
    private volatile ColumnCodec[] mValueCodecs;

    RowGen(RowInfo info) {
        this.info = info;
    }

    /**
     * @param suffix appended to class name
     */
    public ClassMaker beginClassMaker(Class<?> who, Class<?> rowType, String suffix) {
        return beginClassMaker(who, rowType, info, null, suffix);
    }

    /**
     * @param subPackage optional
     * @param suffix appended to class name
     */
    public ClassMaker beginClassMaker(Class<?> who, Class<?> rowType,
                                      String subPackage, String suffix)
    {
        return beginClassMaker(who, rowType, info, subPackage, suffix);
    }

    /**
     * @param subPackage optional
     * @param suffix appended to class name
     */
    public static ClassMaker beginClassMaker(Class<?> who, Class<?> rowType, RowInfo info,
                                             String subPackage, String suffix)
    {
        String name;
        {
            int ix = info.name.lastIndexOf('.');
            if (ix > 0) {
                if (subPackage == null) {
                    name = info.name + suffix;
                } else {
                    String packageName = info.name.substring(0, ix) + '.' + subPackage;
                    name = packageName + '.' + info.name.substring(ix + 1) + suffix;
                }
            } else if (subPackage == null) {
                name = info.name + suffix;
            } else {
                name = subPackage + '.' + info.name + suffix;
            }
        }

        ClassMaker cm = ClassMaker.begin(name, rowType.getClassLoader(), RowGen.class);

        if (who != null) {
            cm.sourceFile(who.getSimpleName());
        }

        var thisModule = RowGen.class.getModule();<commit_msg>Bring back the special key object. The RowGen class shouldn't be used as a key, since it can be accessed as a backdoor even though it's not public.<commit_after>/*
 *  Copyright 2021 Cojen.org
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.cojen.tupl.rows;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import org.cojen.maker.ClassMaker;

/**
 * Cache of objects used by code generation.
 *
 * @author <NAME>
 */
class RowGen {
    private static final Object MAKER_KEY = new Object(); // must be a private hidden instance

    final RowInfo info;

    private volatile String[] mStateFields;
    private volatile Map<String, Integer> mColumnNumbers;
    private volatile ColumnCodec[] mKeyCodecs;
    private volatile ColumnCodec[] mValueCodecs;

    RowGen(RowInfo info) {
        this.info = info;
    }

    /**
     * @param suffix appended to class name
     */
    public ClassMaker beginClassMaker(Class<?> who, Class<?> rowType, String suffix) {
        return beginClassMaker(who, rowType, info, null, suffix);
    }

    /**
     * @param subPackage optional
     * @param suffix appended to class name
     */
    public ClassMaker beginClassMaker(Class<?> who, Class<?> rowType,
                                      String subPackage, String suffix)
    {
        return beginClassMaker(who, rowType, info, subPackage, suffix);
    }

    /**
     * @param subPackage optional
     * @param suffix appended to class name
     */
    public static ClassMaker beginClassMaker(Class<?> who, Class<?> rowType, RowInfo info,
                                             String subPackage, String suffix)
    {
        String name;
        {
            int ix = info.name.lastIndexOf('.');
            if (ix > 0) {
                if (subPackage == null) {
                    name = info.name + suffix;
                } else {
                    String packageName = info.name.substring(0, ix) + '.' + subPackage;
                    name = packageName + '.' + info.name.substring(ix + 1) + suffix;
                }
            } else if (subPackage == null) {
                name = info.name + suffix;
            } else {
                name = subPackage + '.' + info.name + suffix;
            }
        }

        ClassMaker cm = ClassMaker.begin(name, rowType.getClassLoader(), MAKER_KEY);

        if (who != null) {
            cm.sourceFile(who.getSimpleName());
        }

        var thisModule = RowGen.class.getModule();
{'fraction_non_alphanumeric': 0.09363110008271298, 'fraction_numerical': 0.0026468155500413565, 'mean_word_length': 2.163788592360021, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2358555', 'n_tokens_mistral': 1655, 'n_tokens_neox': 1612, 'n_words': 573}