<commit_before>package com.github.sherter.googlejavaformatgradleplugin

import com.github.sherter.googlejavaformatgradleplugin.format.Formatter
import com.github.sherter.googlejavaformatgradleplugin.format.FormatterOption
import com.github.sherter.googlejavaformatgradleplugin.format.Gjf
import com.google.common.collect.ImmutableList
import com.google.common.collect.ImmutableSet
import com.google.common.collect.ImmutableTable
import groovy.transform.CompileStatic
import groovy.transform.PackageScope
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ResolveException
import org.gradle.api.logging.Logger

@CompileStatic
class FormatterFactory {

    static final ImmutableTable<String, Object, FormatterOption> optionMapping =
            ImmutableTable.<String, Object, FormatterOption>builder()
                    .put('style', 'GOOGLE', FormatterOption.GOOGLE_STYLE)
                    .put('style', 'AOSP', FormatterOption.AOSP_STYLE)
                    .build()


    private final Project project
    private final Logger logger

    FormatterFactory(Project project, Logger logger) {
        this.project = Objects.requireNonNull(project)
        this.logger = Objects.requireNonNull(logger)
    }

    Formatter create(String toolVersion, ImmutableSet<FormatterOption> options) throws ResolveException {
        Objects.requireNonNull(toolVersion)
        def configuration = setupConfiguration(toolVersion)
        def classpath = configuration.resolve()
        def classLoader = new URLClassLoader(classpath.collect { it.toURI().toURL() } as URL[], (ClassLoader)null)
        boolean versionIsSupported = toolVersion in Gjf.SUPPORTED_VERSIONS
        if (!versionIsSupported) {
            logger.warn('Version {} of google-java-format-gradle-plugin is not tested against version {} of ' +
                    'google-java-format. This should not be a problem if the task is executed without failures.',
                    GoogleJavaFormatPlugin.PLUGIN_VERSION, toolVersion)
        }
        if (toolVersion.equals('0.1-alpha')) {
            return Gjf.newFormatter(classLoader, toolVersion, options as FormatterOption[])
        } else {
            return Gjf.newFormatter(classLoader, toolVersion,
                    (options as FormatterOption[]) + [FormatterOption.ECLIPSE_JAVADOC_FORMATTER,
                            FormatterOption.SORT_IMPORTS])
        }
    }

    @PackageScope
    static ImmutableSet<FormatterOption> mapOptions(Map<String, Object> optionsInDsl) {
        Set<FormatterOption> mapped = new HashSet<>(optionsInDsl.size())
        for (Map.Entry<String, Object> entry : optionsInDsl) {
            def option = optionMapping.get(entry.key, entry.value)
            if (option == null) {
                throw new IllegalArgumentException("invalid option: $entry")
            }
            mapped.add(option)
        }
        return ImmutableSet.copyOf(mapped);
    }

    private Configuration setupConfiguration(String toolVersion) {
        def configuration = project.configurations.maybeCreate("googleJavaFormat$toolVersion");
        def dependency = project.dependencies.create(
                group: Gjf.GROUP_ID,
                name: Gjf.ARTIFACT_ID,
                version: toolVersion
        )
        configuration.dependencies.add(dependency)
        return configuration
    }
}<commit_msg>Work around groovy type inference bug<commit_after>package com.github.sherter.googlejavaformatgradleplugin

import com.github.sherter.googlejavaformatgradleplugin.format.Formatter
import com.github.sherter.googlejavaformatgradleplugin.format.FormatterOption
import com.github.sherter.googlejavaformatgradleplugin.format.Gjf
import com.google.common.collect.ImmutableList
import com.google.common.collect.ImmutableSet
import com.google.common.collect.ImmutableTable
import groovy.transform.CompileStatic
import groovy.transform.PackageScope
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ResolveException
import org.gradle.api.logging.Logger

@CompileStatic
class FormatterFactory {

    static final ImmutableTable<String, Object, FormatterOption> optionMapping =
            ImmutableTable.<String, Object, FormatterOption>builder()
                    .put('style', 'GOOGLE', FormatterOption.GOOGLE_STYLE)
                    .put('style', 'AOSP', FormatterOption.AOSP_STYLE)
                    .build()


    private final Project project
    private final Logger logger

    FormatterFactory(Project project, Logger logger) {
        this.project = Objects.requireNonNull(project)
        this.logger = Objects.requireNonNull(logger)
    }

    Formatter create(String toolVersion, ImmutableSet<FormatterOption> options) throws ResolveException {
        Objects.requireNonNull(toolVersion)
        def configuration = setupConfiguration(toolVersion)
        def classpath = configuration.resolve()
        def classLoader = new URLClassLoader(classpath.collect { it.toURI().toURL() } as URL[], (ClassLoader)null)
        boolean versionIsSupported = toolVersion in Gjf.SUPPORTED_VERSIONS
        if (!versionIsSupported) {
            logger.warn('Version {} of google-java-format-gradle-plugin is not tested against version {} of ' +
                    'google-java-format. This should not be a problem if the task is executed without failures.',
                    GoogleJavaFormatPlugin.PLUGIN_VERSION, toolVersion)
        }
        if (toolVersion.equals('0.1-alpha')) {
            return Gjf.newFormatter(classLoader, toolVersion, options as FormatterOption[])
        } else {
            return Gjf.newFormatter(classLoader, toolVersion,
                    (options as FormatterOption[]) + [FormatterOption.ECLIPSE_JAVADOC_FORMATTER,
                            FormatterOption.SORT_IMPORTS])
        }
    }

    @PackageScope
    static ImmutableSet<FormatterOption> mapOptions(Map<String, Object> optionsInDsl) {
        Set<FormatterOption> mapped = new HashSet<FormatterOption>(optionsInDsl.size())
        for (Map.Entry<String, Object> entry : optionsInDsl) {
            def option = (FormatterOption) optionMapping.get(entry.key, entry.value)
            if (option == null) {
                throw new IllegalArgumentException("invalid option: $entry")
            }
            mapped.add(option)
        }
        return ImmutableSet.copyOf(mapped);
    }

    private Configuration setupConfiguration(String toolVersion) {
        def configuration = project.configurations.maybeCreate("googleJavaFormat$toolVersion");
        def dependency = project.dependencies.create(
                group: Gjf.GROUP_ID,
                name: Gjf.ARTIFACT_ID,
                version: toolVersion
        )
        configuration.dependencies.add(dependency)
        return configuration
    }
}
{'fraction_non_alphanumeric': 0.08385318438996082, 'fraction_numerical': 0.0005802988539097636, 'mean_word_length': 3.033937975424225, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '6893569', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1745, 'n_words': 368}