<commit_before>package com.proofpoint.lifecycle;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

class LifeCycleMethods
{
    private final Multimap<Class<? extends Annotation>, Method> methodMap = ArrayListMultimap.create();

    LifeCycleMethods(Class<?> clazz)
    {
        addLifeCycleMethods(clazz, new HashSet<String>(), new HashSet<String>());
    }

    boolean      hasFor(Class<? extends Annotation> annotation)
    {
        Collection<Method> methods = methodMap.get(annotation);
        return (methods != null) && (methods.size() > 0);
    }

    Collection<Method>      methodsFor(Class<? extends Annotation> annotation)
    {
        Collection<Method> methods = methodMap.get(annotation);
        return (methods != null) ? methods : Lists.<Method>newArrayList();
    }

    private void addLifeCycleMethods(Class<?> clazz, Set<String> usedConstructNames, Set<String> usedDestroyNames)
    {
        if ( clazz == null )
        {
            return;
        }

        for ( Method method : clazz.getDeclaredMethods() )
        {
            if ( method.isSynthetic() || method.isBridge() )
            {
                continue;
            }

            if ( method.isAnnotationPresent(PostConstruct.class) )
            {
                if ( !usedConstructNames.contains(method.getName()) )
                {
                    validateMethod(method);
                    usedConstructNames.add(method.getName());
                    methodMap.put(PostConstruct.class, method);
                }
            }
            if ( method.isAnnotationPresent(PreDestroy.class) )
            {
                if ( !usedDestroyNames.contains(method.getName()) )
                {
                    validateMethod(method);
                    usedDestroyNames.add(method.getName());
                    methodMap.put(PreDestroy.class, method);
                }
            }
        }

        addLifeCycleMethods(clazz.getSuperclass(), usedConstructNames, usedDestroyNames);
        for ( Class<?> face : clazz.getInterfaces() )
        {
            addLifeCycleMethods(face, usedConstructNames, usedDestroyNames);
        }
    }

    private void        validateMethod(Method m)
    {
        if ( m.getParameterTypes().length != 0 )
        {
            throw new UnsupportedOperationException(String.format("@PostConstruct/@PreDestroy methods cannot have arguments: %s", m.getDeclaringClass().getName() + "." + m.getName() + "(...)"));
        }
    }
}<commit_msg>A bit of refactoring<commit_after>package com.proofpoint.lifecycle;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

class LifeCycleMethods
{
    private final Multimap<Class<? extends Annotation>, Method> methodMap = ArrayListMultimap.create();

    LifeCycleMethods(Class<?> clazz)
    {
        addLifeCycleMethods(clazz, new HashSet<String>(), new HashSet<String>());
    }

    boolean      hasFor(Class<? extends Annotation> annotation)
    {
        Collection<Method> methods = methodMap.get(annotation);
        return (methods != null) && (methods.size() > 0);
    }

    Collection<Method>      methodsFor(Class<? extends Annotation> annotation)
    {
        Collection<Method> methods = methodMap.get(annotation);
        return (methods != null) ? methods : Lists.<Method>newArrayList();
    }

    private void addLifeCycleMethods(Class<?> clazz, Set<String> usedConstructNames, Set<String> usedDestroyNames)
    {
        if ( clazz == null )
        {
            return;
        }

        for ( Method method : clazz.getDeclaredMethods() )
        {
            if ( method.isSynthetic() || method.isBridge() )
            {
                continue;
            }

            procesMethod(method, PostConstruct.class, usedConstructNames);
            procesMethod(method, PreDestroy.class, usedDestroyNames);
        }

        addLifeCycleMethods(clazz.getSuperclass(), usedConstructNames, usedDestroyNames);
        for ( Class<?> face : clazz.getInterfaces() )
        {
            addLifeCycleMethods(face, usedConstructNames, usedDestroyNames);
        }
    }

    private void procesMethod(Method method, Class<? extends Annotation> annotationClass, Set<String> usedSet)
    {
        if ( method.isAnnotationPresent(annotationClass) )
        {
            if ( !usedSet.contains(method.getName()) )
            {
                if ( method.getParameterTypes().length != 0 )
                {
                    throw new UnsupportedOperationException(String.format("@PostConstruct/@PreDestroy methods cannot have arguments: %s", method.getDeclaringClass().getName() + "." + method.getName() + "(...)"));
                }

                usedSet.add(method.getName());
                methodMap.put(PostConstruct.class, method);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.11334687096178697, 'fraction_numerical': 0.0007384160974709249, 'mean_word_length': 2.4032663316582914, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, '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': '5832833', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1411, 'n_words': 264}