All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.yandex.qatools.allure.plugins.DefaultPluginsIndex.groovy Maven / Gradle / Ivy

There is a newer version: 1.5.4
Show newest version
package ru.yandex.qatools.allure.plugins

import static java.util.Collections.unmodifiableList

/**
 * @author Dmitry Baev [email protected]
 *         Date: 10.07.15
 */
class DefaultPluginsIndex implements PluginsIndex {

    private final List plugins

    /**
     * Creates an instance of plugin index from given plugins. Plugins were
     * sorted by priority. Plugins with higher priority will be first,
     * if some plugins have equals priority then they will be ordered alphabetically.
     */
    public DefaultPluginsIndex(List plugins) {
        this.plugins = plugins ? unmodifiableList(plugins
                .findAll { it }
                .sort(false, sortClosure)
        ) : []
    }

    /**
     * @inheritDoc
     */
    @Override
    def  T find(Class type) {
        filterByType(plugins, type).find()
    }

    /**
     * @inheritDoc
     */
    @Override
    def  List findAll(Class type) {
        filterByType(plugins, type)
    }

    /**
     * @inheritDoc
     */
    @Override
    Plugin find(String name) {
        filterByType(plugins, WithResources).find { it.name == name } as Plugin
    }

    /**
     * @inheritDoc
     */
    @Override
    List getPlugins() {
        return plugins
    }

    /**
     * Get priority of given plugin.
     * @see WithPriority
     */
    protected static int getPriority(Plugin plugin) {
        plugin instanceof WithPriority ? (plugin as WithPriority).priority : 0
    }

    /**
     * Find all plugins with specified type.
     */
    protected static  List filterByType(List plugins, Class type) {
        unmodifiableList(plugins.findAll {
            type.isAssignableFrom((it as Object).class)
        } as List)
    }

    /**
     * Returns the closure to sort plugins by priority and alphabetically in case equals priority.
     */
    protected static Closure getSortClosure() {
        { first, second ->
            getPriority(second as Plugin) <=> getPriority(first as Plugin) ?:
                    first.class.simpleName <=> second.class.simpleName
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy