<commit_before>package com.github.mizool.core.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class CheckDatabaseIdentifier implements ConstraintValidator<DatabaseIdentifier, Object>
{
    private boolean mandatory;

    @Override
    public void initialize(DatabaseIdentifier identifier)
    {
        mandatory = identifier.mandatory();
    }

    @Override
    public boolean isValid(Object validationObject, ConstraintValidatorContext constraintValidatorContext)
    {
        return ConstraintValidators.isValid(validationObject, mandatory, this::isValidValue);
    }

    private boolean isValidValue(Object validationObject)
    {
        boolean valid = false;
        if (validationObject instanceof String)
        {
            String validationString = (String) validationObject;
            return !validationString.isEmpty() &&
                validationString.matches("[a-zA-Z][a-zA-Z0-9_]*") &&
                validationString.length() <= 48;
        }
        return valid;
    }
}<commit_msg>Extracted regex pattern. Fixed usage of return variable. Simplified regex pattern.<commit_after>package com.github.mizool.core.validation;

import java.util.regex.Pattern;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class CheckDatabaseIdentifier implements ConstraintValidator<DatabaseIdentifier, Object>
{
    private static final Pattern PATTERN = Pattern.compile("[a-zA-Z]\\w*");

    private boolean mandatory;

    @Override
    public void initialize(DatabaseIdentifier identifier)
    {
        mandatory = identifier.mandatory();
    }

    @Override
    public boolean isValid(Object validationObject, ConstraintValidatorContext constraintValidatorContext)
    {
        return ConstraintValidators.isValid(validationObject, mandatory, this::isValidValue);
    }

    private boolean isValidValue(Object validationObject)
    {
        boolean valid = false;
        if (validationObject instanceof String)
        {
            String validationString = (String) validationObject;
            valid = !validationString.isEmpty() &&
                PATTERN.matcher(validationString)
                    .matches() &&
                validationString.length() <= 48;
        }
        return valid;
    }
}
{'fraction_non_alphanumeric': 0.08018667798048366, 'fraction_numerical': 0.002545608824777259, 'mean_word_length': 3.2032085561497325, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '5110341', 'n_tokens_mistral': 561, 'n_tokens_neox': 544, 'n_words': 129}