<commit_before>/*
 * Copyright (c) 2006-2012 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * Facilitates simple injection of parameters into a constructor.
 * <p>
 * Injectors may be given a set of parent injectors, from which they will
 * inherit parameters. No checking is performed to prevent circular references
 * within parents.
 */
public class SimpleInjector {

    /** The parent injectors, if any. */
    private final SimpleInjector[] parents;

    /** A mapping of known classes to the objects that should be injected. */
    private final Map<Class<?>, Object> parameters
            = new HashMap<Class<?>, Object>();

    /**
     * Creates a new injector which will inherit injection parameters from
     * the specified parent. Parent graphs must be acyclic.
     *
     * @param parents The injectors to inherit parameters from.
     */
    public SimpleInjector(final SimpleInjector ... parents) {
        this.parents = parents;
    }

    /**
     * Adds a new injectable object to this injector. If a constructor requires
     * an instance of the specified class, it will be given the specified
     * object. If an association has already been made for the specified class,
     * it will be replaced with the new object.
     *
     * @param clazz The type of parameter which should be injected
     * @param object The value to inject for the parameter
     */
    public void addParameter(final Class<?> clazz, final Object object) {
        parameters.put(clazz, object);
    }

    /**
     * Adds the specified object as an injectable parameter for all of its
     * known classes and interface. This is equivalent to calling
     * {@link #addParameter(java.lang.Class, java.lang.Object)} for the object's
     * class, all superclasses, and all interfaces.
     *
     * @param object The object to be injected
     */
    public void addParameter(final Object object) {
        // Iterate the object hierarchy up
        Class<?> target = object.getClass();
        do {
            addParameter(target, object);
            target = target.getSuperclass();
        } while (target != null);

        // Add all interfaces
        for (Class<?> iface : object.getClass().getInterfaces()) {
            addParameter(iface, object);
        }
    }

    /**
     * Retrieves a mapping of known injectable types to their corresponding
     * values. This mapping will also include mappings defined in all of this
     * injector's parents.
     *
     * @return A map of injectable parameters
     */
    public Map<Class<?>, Object> getParameters() {
        final Map<Class<?>, Object> localParams
                = new HashMap<Class<?>, Object>(parameters.size());

        for (SimpleInjector parent : parents) {
            localParams.putAll(parent.getParameters());
        }

        // Note: insert local parameters after so they override parent params.
        localParams.putAll(parameters);

        return localParams;
    }

    /**
     * Attempts to create a new instance of the specified class by injecting
     * parameters into the constructor(s). If no constructors are found which
     * can be satisfied by the known parameters, or if all satisfiable
     * constructors throw exceptions, an IllegalArgumentException will be
     * thrown.
     * <p>
     * If multiple constructors are available that are satisfiable using the<commit_msg>SimpleInjector now includes all parent interfaces.<commit_after>/*
 * Copyright (c) 2006-2012 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * Facilitates simple injection of parameters into a constructor.
 * <p>
 * Injectors may be given a set of parent injectors, from which they will
 * inherit parameters. No checking is performed to prevent circular references
 * within parents.
 */
public class SimpleInjector {

    /** The parent injectors, if any. */
    private final SimpleInjector[] parents;

    /** A mapping of known classes to the objects that should be injected. */
    private final Map<Class<?>, Object> parameters
            = new HashMap<Class<?>, Object>();

    /**
     * Creates a new injector which will inherit injection parameters from
     * the specified parent. Parent graphs must be acyclic.
     *
     * @param parents The injectors to inherit parameters from.
     */
    public SimpleInjector(final SimpleInjector ... parents) {
        this.parents = parents;
    }

    /**
     * Adds a new injectable object to this injector. If a constructor requires
     * an instance of the specified class, it will be given the specified
     * object. If an association has already been made for the specified class,
     * it will be replaced with the new object.
     *
     * @param clazz The type of parameter which should be injected
     * @param object The value to inject for the parameter
     */
    public void addParameter(final Class<?> clazz, final Object object) {
        parameters.put(clazz, object);
    }

    /**
     * Adds the specified object as an injectable parameter for all of its
     * known classes and interface. This is equivalent to calling
     * {@link #addParameter(java.lang.Class, java.lang.Object)} for the object's
     * class, all superclasses, and all interfaces.
     *
     * @param object The object to be injected
     */
    public void addParameter(final Object object) {
        // Iterate the object hierarchy up
        Class<?> target = object.getClass();
        do {
            addParameter(target, object);

            // Add all interfaces
            for (Class<?> iface : target.getInterfaces()) {
                addParameter(iface, object);
            }

            target = target.getSuperclass();
        } while (target != null);
    }

    /**
     * Retrieves a mapping of known injectable types to their corresponding
     * values. This mapping will also include mappings defined in all of this
     * injector's parents.
     *
     * @return A map of injectable parameters
     */
    public Map<Class<?>, Object> getParameters() {
        final Map<Class<?>, Object> localParams
                = new HashMap<Class<?>, Object>(parameters.size());

        for (SimpleInjector parent : parents) {
            localParams.putAll(parent.getParameters());
        }

        // Note: insert local parameters after so they override parent params.
        localParams.putAll(parameters);

        return localParams;
    }

    /**
     * Attempts to create a new instance of the specified class by injecting
     * parameters into the constructor(s). If no constructors are found which
     * can be satisfied by the known parameters, or if all satisfiable
     * constructors throw exceptions, an IllegalArgumentException will be
     * thrown.
     * <p>
     * If multiple constructors are available that are satisfiable using the
{'fraction_non_alphanumeric': 0.0737943683245226, 'fraction_numerical': 0.0017261840543747978, 'mean_word_length': 2.926302414231258, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2964654', 'n_tokens_mistral': 2299, 'n_tokens_neox': 2223, 'n_words': 1064}