<commit_before>package ch.raffael.util.cli;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.SetMultimap;

import static java.lang.reflect.Modifier.*;


/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class DefaultDispatcher implements CommandDispatcher {

    private final Map<String, CommandDescriptor> commands = new HashMap<String, CommandDescriptor>();
    private final SetMultimap<String, CommandDescriptor> aliases = HashMultimap.create();

    public DefaultDispatcher() {
    }

    public DefaultDispatcher(Object... targets) {
        for ( Object t : targets ) {
            if ( t instanceof Class ) {
                registerStatic((Class)t);
            }
            else {
                register(t);
            }
        }
    }

    void register(Object target) {
        for ( Method method : target.getClass().getMethods() ) {
            if ( method.getAnnotation(Command.class) != null ) {
                register(target, method);
            }
        }
    }

    void registerStatic(Class clazz) {
        for ( Method method : clazz.getMethods() ) {
            if ( method.getAnnotation(Command.class) != null && isStatic(method.getModifiers()) ) {
                register(null, method);
            }
        }
    }

    void register(Object target, Method method) {
        if ( target == null && !isStatic(method.getModifiers()) ) {
            throw ReflectionCommandHandler.illegalMethod(method, "Method must be static if target is null");
        }
        if ( !isPublic(method.getModifiers()) ) {
            throw ReflectionCommandHandler.illegalMethod(method, "Method is not public");
        }
        Command cmd = method.getAnnotation(Command.class);
        if ( cmd == null ) {
            throw ReflectionCommandHandler.illegalMethod(method, "No @Command annotation found");
        }
        register(cmd.name(), Arrays.asList(cmd.alias()), new ReflectionCommandHandler(target, method, cmd.doc()));
    }

    void register(String name, Iterable<String> aliases, CommandHandler handler) {
        if ( commands.containsKey(name) ) {
            throw new IllegalStateException("Duplicate command: " + name);
        }
        CommandDescriptor description = new CommandDescriptor(name, aliases, handler);
        commands.put(name, description);
        for ( String alias : aliases ) {
            this.aliases.put(alias, description);
        }
    }

    void unregister(String name) {
        CommandDescriptor description = commands.remove(name);
        if ( description != null ) {
            for ( String alias : description.getAliases() ) {
                aliases.remove(alias, description);
            }
        }
    }

    @Override
    public CommandDescriptor findCommand(String command) {
        CommandDescriptor description = commands.get(command);
        if ( description == null ) {
            Set<CommandDescriptor> alias = aliases.get(command);
            if ( aliases.size() != 1 ) {
                return null;
            }
            description = Iterables.getFirst(alias, null);
        }
        return description;
    }

    @Override
    public Iterable<CommandDescriptor> listCommands() {
        return Iterables.transform(commands.values(), new Function<CommandDescriptor, CommandDescriptor>() {
            @Override
            public CommandDescriptor apply(CommandDescriptor input) {
                return new CommandDescriptor(input.getName(), Iterables.filter(input.getAliases(), new Predicate<String>() {
                    @Override
                    public boolean apply(String input) {
                        return aliases.get(input).size() == 1;
                    }
                }), input.getHandler());
            }
        });
    }
}<commit_msg>Also filter aliases in command descriptor in findCommand().<commit_after>package ch.raffael.util.cli;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.SetMultimap;

import static java.lang.reflect.Modifier.*;


/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class DefaultDispatcher implements CommandDispatcher {

    private final Map<String, CommandDescriptor> commands = new HashMap<String, CommandDescriptor>();
    private final SetMultimap<String, CommandDescriptor> aliases = HashMultimap.create();

    public DefaultDispatcher() {
    }

    public DefaultDispatcher(Object... targets) {
        for ( Object t : targets ) {
            if ( t instanceof Class ) {
                registerStatic((Class)t);
            }
            else {
                register(t);
            }
        }
    }

    void register(Object target) {
        for ( Method method : target.getClass().getMethods() ) {
            if ( method.getAnnotation(Command.class) != null ) {
                register(target, method);
            }
        }
    }

    void registerStatic(Class clazz) {
        for ( Method method : clazz.getMethods() ) {
            if ( method.getAnnotation(Command.class) != null && isStatic(method.getModifiers()) ) {
                register(null, method);
            }
        }
    }

    void register(Object target, Method method) {
        if ( target == null && !isStatic(method.getModifiers()) ) {
            throw ReflectionCommandHandler.illegalMethod(method, "Method must be static if target is null");
        }
        if ( !isPublic(method.getModifiers()) ) {
            throw ReflectionCommandHandler.illegalMethod(method, "Method is not public");
        }
        Command cmd = method.getAnnotation(Command.class);
        if ( cmd == null ) {
            throw ReflectionCommandHandler.illegalMethod(method, "No @Command annotation found");
        }
        register(cmd.name(), Arrays.asList(cmd.alias()), new ReflectionCommandHandler(target, method, cmd.doc()));
    }

    void register(String name, Iterable<String> aliases, CommandHandler handler) {
        if ( commands.containsKey(name) ) {
            throw new IllegalStateException("Duplicate command: " + name);
        }
        CommandDescriptor description = new CommandDescriptor(name, aliases, handler);
        commands.put(name, description);
        for ( String alias : aliases ) {
            this.aliases.put(alias, description);
        }
    }

    void unregister(String name) {
        CommandDescriptor description = commands.remove(name);
        if ( description != null ) {
            for ( String alias : description.getAliases() ) {
                aliases.remove(alias, description);
            }
        }
    }

    @Override
    public CommandDescriptor findCommand(String command) {
        CommandDescriptor descriptor = commands.get(command);
        if ( descriptor == null ) {
            Set<CommandDescriptor> alias = aliases.get(command);
            if ( aliases.size() != 1 ) {
                return null;
            }
            descriptor = Iterables.getFirst(alias, null);
        }
        if ( descriptor != null ) {
            descriptor = aliasFilteredDescriptor(descriptor);
        }
        return descriptor;
    }

    @Override
    public Iterable<CommandDescriptor> listCommands() {
        return Iterables.transform(commands.values(), new Function<CommandDescriptor, CommandDescriptor>() {
            @Override
            public CommandDescriptor apply(CommandDescriptor input) {
                return aliasFilteredDescriptor(input);
            }
        });
    }

    private CommandDescriptor aliasFilteredDescriptor(CommandDescriptor input) {
        return new CommandDescriptor(input.getName(), Iterables.filter(input.getAliases(), new Predicate<String>() {
            @Override
            public boolean apply(String input) {
                return aliases.get(input).size() == 1;
            }
        }), input.getHandler());
    }
}
{'fraction_non_alphanumeric': 0.10910827265130715, 'fraction_numerical': 0.0004774979109466396, 'mean_word_length': 2.3285657528804133, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '4738430', 'n_tokens_mistral': 2144, 'n_tokens_neox': 2094, 'n_words': 476}