<commit_before>package de.synyx.metrics.servlet.listener;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.ScheduledReporter;
import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.Long.parseLong;

/**
 * Date: 31.07.2014
 * Time: 14:40
 */
public abstract class MetricReportHandler {

    final String  rdefault = "20s";
    final Pattern rpattern = Pattern.compile ("^(\\d+)\\s*([a-z]{1,2})$");

    final Logger logger = LoggerFactory.getLogger (getClass ());


    protected abstract String scheme ();

    protected abstract ScheduledReporter reporter (MetricRegistry registry, URI location);

    public final Optional<ScheduledReporter> select (MetricRegistry registry, URI location) {
        String scheme;

                              scheme = scheme ();
        if (! Objects.equals (scheme, location.getScheme ())) return Optional.absent ();

        logger.info ("{} reporter detected: {} ", scheme, location);

        return Optional.fromNullable (reporter (registry, location));
    }

    protected final ScheduledReporter start (ScheduledReporter reporter, String refresh) {
        Matcher matcher;

            matcher = rpattern.matcher (or (refresh, ""));
        if (matcher.matches ()) {
                   reporter.start (parseLong (matcher.group (1)), timeunit (matcher.group (2)));
            return reporter;
        }

        logger.warn ("graphite refresh rate {} invalid, using {} instead", refresh, rdefault);

        return start (reporter, rdefault);
    }

    protected final Map<String, String> parameters (String query) {
        return Splitter.on ("&").omitEmptyStrings ().withKeyValueSeparator ("=").split (query);
    }

    protected final String or (String val, String or) {
        return Objects.toString (val, or);
    }

    protected final TimeUnit timeunit (String timeunit) {
        switch (timeunit) {
            case  "d" : return TimeUnit.DAYS;
            case  "h" : return TimeUnit.HOURS;
            case  "m" : return TimeUnit.MINUTES;
            case  "s" : return TimeUnit.SECONDS;
            case "ms" : return TimeUnit.MILLISECONDS;
            case "µs" : return TimeUnit.MICROSECONDS;
            case "ns" : return TimeUnit.NANOSECONDS;<commit_msg>scheme should be used in the log message<commit_after>package de.synyx.metrics.servlet.listener;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.ScheduledReporter;
import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.Long.parseLong;

/**
 * Date: 31.07.2014
 * Time: 14:40
 */
public abstract class MetricReportHandler {

    final String  rdefault = "20s";
    final Pattern rpattern = Pattern.compile ("^(\\d+)\\s*([a-z]{1,2})$");

    final Logger logger = LoggerFactory.getLogger (getClass ());


    protected abstract String scheme ();

    protected abstract ScheduledReporter reporter (MetricRegistry registry, URI location);

    public final Optional<ScheduledReporter> select (MetricRegistry registry, URI location) {
        String scheme;

                              scheme = scheme ();
        if (! Objects.equals (scheme, location.getScheme ())) return Optional.absent ();

        logger.info ("{} reporter detected: {} ", scheme, location);

        return Optional.fromNullable (reporter (registry, location));
    }

    protected final ScheduledReporter start (ScheduledReporter reporter, String refresh) {
        Matcher matcher;

            matcher = rpattern.matcher (or (refresh, ""));
        if (matcher.matches ()) {
                   reporter.start (parseLong (matcher.group (1)), timeunit (matcher.group (2)));
            return reporter;
        }

        logger.warn ("{} refresh rate '{}' not usable, initializing with {} instead", scheme (), refresh, rdefault);

        return start (reporter, rdefault);
    }

    protected final Map<String, String> parameters (String query) {
        return Splitter.on ("&").omitEmptyStrings ().withKeyValueSeparator ("=").split (query);
    }

    protected final String or (String val, String or) {
        return Objects.toString (val, or);
    }

    protected final TimeUnit timeunit (String timeunit) {
        switch (timeunit) {
            case  "d" : return TimeUnit.DAYS;
            case  "h" : return TimeUnit.HOURS;
            case  "m" : return TimeUnit.MINUTES;
            case  "s" : return TimeUnit.SECONDS;
            case "ms" : return TimeUnit.MILLISECONDS;
            case "µs" : return TimeUnit.MICROSECONDS;
            case "ns" : return TimeUnit.NANOSECONDS;
{'fraction_non_alphanumeric': 0.11585963536561458, 'fraction_numerical': 0.007841599686336012, 'mean_word_length': 2.9367283950617282, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4630344', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1414, 'n_words': 388}