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

org.tiogasolutions.push.kernel.system.PluginManager Maven / Gradle / Ivy

The newest version!
package org.tiogasolutions.push.kernel.system;

import org.tiogasolutions.push.kernel.plugins.Plugin;
import org.tiogasolutions.push.pub.common.PushType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PluginManager {

    private final Map map = new HashMap<>();

    public PluginManager(List plugins) {

        for (Plugin plugin : plugins) {
            PushType pushType = plugin.getPushType();

            if (map.containsKey(pushType)) {
                String msg = String.format("The push type \"%s\" has already been registered.", pushType);
                throw new IllegalArgumentException(msg);
            }
            map.put(pushType, plugin);
        }
    }

    public List getPlugins() {
        return new ArrayList<>(map.values());
    }

    public  T getPlugin(Class type) {
        for (Plugin plugin : map.values()) {
            if (type == plugin.getClass()) {
                // noinspection unchecked
                return (T) plugin;
            }
        }
        return null;
    }

    public Plugin getPlugin(PushType pushType) {
        if (map.containsKey(pushType) == false) {
            String msg = String.format("The plugin for \"%s\" was not found.", pushType.getCode());
            throw new IllegalArgumentException(msg);
        }
        return map.get(pushType);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy