<commit_before>package de.skuzzle.enforcer.restrictimports.parser.lang;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import de.skuzzle.enforcer.restrictimports.parser.ImportStatement;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;


public class KotlinGroovyLanguageSupport implements LanguageSupport {

    private static final String IMPORT_STATEMENT = "import ";

    @Override
    public Set<String> getSupportedFileExtensions() {
        return ImmutableSet.of("groovy", "kt");
    }

    @Override
    public Optional<String> parsePackage(String line) {
        if (!isPackage(line)) {
            return Optional.empty();
        }

        return Optional.of(extractPackageName(line));
    }

    @Override
    public List<ImportStatement> parseImport(String line, int lineNumber) {
        if (!isImport(line)) {
            return ImmutableList.of();
        }

        // There can be multiple import statements within te same line, so
        // we simply split them at their ';'
        final String trimmed = line.trim();
        int start = 0;
        int semiIdx = trimmed.indexOf(';');
        final List<ImportStatement> imports = new ArrayList<>();
        while (start < trimmed.length() && semiIdx >= 0) {
            final String importStatement = trimmed.substring(start, semiIdx);
            final String packageOnly = importStatement
                    .trim()
                    .substring(IMPORT_STATEMENT.length())
                    .trim();
            imports.add(new ImportStatement(removeAlias(packageOnly), lineNumber));

            start = semiIdx + 1;
            semiIdx = trimmed.indexOf(';', start);

            // the statement must not necessarily end in semicolon, so make sure we consume the line until the end
            if (semiIdx < 0) {
                semiIdx = trimmed.length();
            }
        }
        // lines do not necessarily end in semicolons
        if (semiIdx < 0) {
            imports.add(new ImportStatement(removeAlias(extractPackageName(line)), lineNumber));
        }
        return imports;
    }

    private boolean is(String compare, String line) {
        return line.startsWith(compare);
    }

    private boolean isPackage(String line) {
        return is("package ", line);
    }

    private boolean isImport(String line) {
        return is("import ", line);
    }

    private static String extractPackageName(String line) {
        // groovy may or may not have a semicolon at the end<commit_msg>Extract constant<commit_after>package de.skuzzle.enforcer.restrictimports.parser.lang;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import de.skuzzle.enforcer.restrictimports.parser.ImportStatement;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;


public class KotlinGroovyLanguageSupport implements LanguageSupport {

    private static final String IMPORT_STATEMENT = "import ";
    private static final String PACKAGE_STATEMENT = "package ";

    @Override
    public Set<String> getSupportedFileExtensions() {
        return ImmutableSet.of("groovy", "kt");
    }

    @Override
    public Optional<String> parsePackage(String line) {
        if (!isPackage(line)) {
            return Optional.empty();
        }

        return Optional.of(extractPackageName(line));
    }

    @Override
    public List<ImportStatement> parseImport(String line, int lineNumber) {
        if (!isImport(line)) {
            return ImmutableList.of();
        }

        // There can be multiple import statements within te same line, so
        // we simply split them at their ';'
        final String trimmed = line.trim();
        int start = 0;
        int semiIdx = trimmed.indexOf(';');
        final List<ImportStatement> imports = new ArrayList<>();
        while (start < trimmed.length() && semiIdx >= 0) {
            final String importStatement = trimmed.substring(start, semiIdx);
            final String packageOnly = importStatement
                    .trim()
                    .substring(IMPORT_STATEMENT.length())
                    .trim();
            imports.add(new ImportStatement(removeAlias(packageOnly), lineNumber));

            start = semiIdx + 1;
            semiIdx = trimmed.indexOf(';', start);

            // the statement must not necessarily end in semicolon, so make sure we consume the line until the end
            if (semiIdx < 0) {
                semiIdx = trimmed.length();
            }
        }
        // lines do not necessarily end in semicolons
        if (semiIdx < 0) {
            imports.add(new ImportStatement(removeAlias(extractPackageName(line)), lineNumber));
        }
        return imports;
    }

    private boolean is(String compare, String line) {
        return line.startsWith(compare);
    }

    private boolean isPackage(String line) {
        return is(PACKAGE_STATEMENT, line);
    }

    private boolean isImport(String line) {
        return is(IMPORT_STATEMENT, line);
    }

    private static String extractPackageName(String line) {
        // groovy may or may not have a semicolon at the end
{'fraction_non_alphanumeric': 0.09531042339092463, 'fraction_numerical': 0.0018986140117714068, 'mean_word_length': 2.443137254901961, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, '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': '7594868', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1352, 'n_words': 386}