net.e6tech.elements.common.resources.plugin.PluginModel Maven / Gradle / Ivy
/*
* Copyright 2017 Futeh Kao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.e6tech.elements.common.resources.plugin;
import net.e6tech.elements.common.resources.InstanceNotFoundException;
import net.e6tech.elements.common.resources.Resources;
import net.e6tech.elements.common.util.SystemException;
import java.lang.reflect.InvocationTargetException;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
@SuppressWarnings("unchecked")
public interface PluginModel {
Resources getResources();
String getName();
default PluginEntry
registerPlugin(PluginPath
path, P plugin) {
PluginManager pluginManager = getResources().getPluginManager();
PluginEntry
entry = pluginManager.add(path, plugin);
pluginManager.lookup(entry.getPath())
.ifPresent(p -> p.onRegistered(this));
return entry;
}
default
PluginEntry
registerPlugin(PluginPath
path, Class extends P> pluginClass) {
PluginManager pluginManager = getResources().getPluginManager();
PluginEntry
entry = pluginManager.add(path, (Class) pluginClass);
pluginManager.lookup(entry.getPath())
.ifPresent(p -> p.onRegistered(this));
return entry;
}
default
Object unregisterPlugin(PluginPath
path) {
PluginManager pluginManager = getResources().getPluginManager();
Object entry = pluginManager.remove(path);
pluginManager.lookup(path)
.ifPresent(p -> p.onUnregistered(this));
return entry;
}
default
PluginEntry
registerPlugin(Class
pluginBaseClass, P plugin) {
return registerPlugin(PluginPath.of(getClass(), getName()).and(pluginBaseClass), plugin);
}
default
PluginEntry
registerPlugin(Class
pluginBaseClass, Class extends P> pluginClass) {
return registerPlugin(PluginPath.of(getClass(), getName()).and(pluginBaseClass), pluginClass);
}
default
Object unregisterPlugin(Class
pluginBaseClass) {
return unregisterPlugin(PluginPath.of(getClass(), getName()).and(pluginBaseClass));
}
default
PluginEntry
registerPlugin(Class pathClass, String pathAttribute, Class
pluginBaseClass, P plugin) {
return registerPlugin(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(pluginBaseClass), plugin);
}
default
PluginEntry
registerPlugin(Class pathClass, String pathAttribute, Class
pluginBaseClass, Class extends P> pluginClass) {
return registerPlugin(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(pluginBaseClass), (Class) pluginClass);
}
default
Object unregisterPlugin(Class pathClass, String pathAttribute, Class
pluginBaseClass) {
return unregisterPlugin(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(pluginBaseClass));
}
default
PluginEntry
registerPlugin(Class pathClass, String pathAttribute, Class pathClass2, String pathAttribute2, Class
pluginBaseClass, P plugin) {
return registerPlugin(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(pathClass2, pathAttribute2).and(pluginBaseClass), plugin);
}
default
PluginEntry
registerPlugin(Class pathClass, String pathAttribute, Class pathClass2, String pathAttribute2, Class
pluginBaseClass, Class extends P> pluginClass) {
return registerPlugin(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(pathClass2, pathAttribute2).and(pluginBaseClass), (Class) pluginClass);
}
default
Object unregisterPlugin(Class pathClass, String pathAttribute, Class pathClass2, String pathAttribute2, Class
pluginBaseClass) {
return unregisterPlugin(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(pathClass2, pathAttribute2).and(pluginBaseClass));
}
default
PluginEntry
registerPlugin(Class pathClass, String pathAttribute, Class pathClass2, String pathAttribute2,
Class pathClass3, String pathAttribute3, Class
pluginBaseClass, P plugin) {
return registerPlugin(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(pathClass2, pathAttribute2).and(pathClass3, pathAttribute3).and(pluginBaseClass), plugin);
}
default
PluginEntry
registerPlugin(Class pathClass, String pathAttribute, Class pathClass2, String pathAttribute2,
Class pathClass3, String pathAttribute3, Class
pluginBaseClass, Class extends P> pluginClass) {
return registerPlugin(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(pathClass2, pathAttribute2).and(pathClass3, pathAttribute3).and(pluginBaseClass), (Class) pluginClass);
}
default
Object unregisterPlugin(Class pathClass, String pathAttribute, Class pathClass2, String pathAttribute2,
Class pathClass3, String pathAttribute3, Class
pluginBaseClass) {
return unregisterPlugin(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(pathClass2, pathAttribute2).and(pathClass3, pathAttribute3).and(pluginBaseClass));
}
default
boolean isPluginRegistered(Class
pluginBaseClass) {
return getResources().getPlugin(PluginPath.of(getClass(), getName()).and(pluginBaseClass)).isPresent();
}
default
boolean isPluginRegistered(Class pathClass, String pathAttribute, Class
pluginBaseClass) {
return getResources().getPlugin(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(pluginBaseClass)).isPresent();
}
default
boolean isPluginRegistered(Class pathClass, String pathAttribute, Class pathClass2, String pathAttribute2, Class
pluginBaseClass) {
return getResources().getPlugin(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(pathClass2, pathAttribute2).and(pluginBaseClass)).isPresent();
}
default
boolean isPluginRegistered(Class pathClass, String pathAttribute, Class pathClass2, String pathAttribute2,
Class pathClass3, String pathAttribute3, Class
pluginBaseClass) {
return getResources().getPlugin(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(pathClass2, pathAttribute2).and(pathClass3, pathAttribute3).and(pluginBaseClass)).isPresent();
}
default
PluginPaths
getPluginPaths(Class
cls) {
return PluginPaths.of(getClass(), getName(), cls);
}
default
PluginPaths
getPluginPaths(Class pathClass, String pathAttribute,
Class
cls) {
return PluginPaths.of(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(cls));
}
default
PluginPaths
getPluginPaths(Class pathClass, String pathAttribute,
Class pathClass2, String pathAttribute2,
Class
cls) {
return PluginPaths.of(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(pathClass2, pathAttribute2).and(cls));
}
default
PluginPaths
getPluginPaths(Class pathClass, String pathAttribute,
Class pathClass2, String pathAttribute2,
Class pathClass3, String pathAttribute3,
Class
cls) {
return PluginPaths.of(PluginPath.of(getClass(), getName()).and(pathClass, pathAttribute).and(pathClass2, pathAttribute2).and(pathClass3, pathAttribute3).and(cls));
}
default
Optional
getPlugin(PluginPath
path, Object ... args) {
Optional
optional = getResources().getPlugin(path, args);
if (optional.isPresent())
return optional;
if (path.parent() == null)
return DefaultPluginModel.from(getResources()).getPlugin(path.getType(), args);
return optional;
}
default
Optional> getPluginEntry(PluginPath path) {
return getResources().getPluginManager().getEntry(path);
}
default
Optional
getPlugin(Class
cls, Object ... args) {
Optional
optional = getResources().getPlugin(getPluginPaths(cls), args);
if (optional.isPresent())
return optional;
return DefaultPluginModel.from(getResources()).getPlugin(cls, args);
}
default
Optional> getPluginEntry(Class cls) {
return getResources().getPluginManager().getEntry(getPluginPaths(cls));
}
default
Optional
getLevel2Plugin(Class pathClass, String pathAttribute,
Class
cls, Object ... args) {
return getResources().getPlugin(getPluginPaths(pathClass, pathAttribute, cls), args);
}
default
Optional> getLevel2PluginEntry(Class pathClass, String pathAttribute,
Class cls) {
return getResources().getPluginManager().getEntry(getPluginPaths(pathClass, pathAttribute, cls));
}
default
Optional
getLevel3Plugin(Class pathClass, String pathAttribute,
Class pathClass2, String pathAttribute2,
Class
cls, Object ... args) {
return getResources().getPlugin(getPluginPaths(pathClass, pathAttribute, pathClass2, pathAttribute2, cls), args);
}
default
Optional> getLevel3PluginEntry(Class pathClass, String pathAttribute,
Class pathClass2, String pathAttribute2,
Class cls) {
return getResources().getPluginManager().getEntry(getPluginPaths(pathClass, pathAttribute, pathClass2, pathAttribute2, cls));
}
default
Optional
getLevel4Plugin(Class pathClass, String pathAttribute,
Class pathClass2, String pathAttribute2,
Class pathClass3, String pathAttribute3,
Class
cls, Object ... args) {
return getResources().getPlugin(getPluginPaths(pathClass, pathAttribute, pathClass2, pathAttribute2, pathClass3, pathAttribute3, cls), args);
}
default
Optional> getLevel4PluginEntry(Class pathClass, String pathAttribute,
Class pathClass2, String pathAttribute2,
Class pathClass3, String pathAttribute3,
Class cls) {
return getResources().getPluginManager().getEntry(getPluginPaths(pathClass, pathAttribute, pathClass2, pathAttribute2, pathClass3, pathAttribute3, cls));
}
default
P newPlugin(Class
cls, Object ... args) {
try {
return getPlugin(cls, args).orElse(cls.getDeclaredConstructor().newInstance());
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new SystemException(e);
}
}
/**
* Returns a plugin and bind it with resources
* @param cls Plugin class
* @param args arguments
* @param
Plugin
* @return Plugin
*/
default
P requirePlugin(Class
cls, Object ... args) {
return getPlugin(cls, args).orElseThrow(InstanceNotFoundException::new);
}
/**
* plugin specified by cls must exist.
*/
default
P acceptPlugin(Class
cls, Consumer
consumer, Object ... args) {
P plugin = requirePlugin(cls, args);
consumer.accept(plugin);
return plugin;
}
/**
* plugin specified by cls must exist.
*/
default
R applyPlugin(Class
cls, Function
function, Object ... args) {
P plugin = requirePlugin(cls, args);
return function.apply(plugin);
}
/**
* If plugin exists, consumer.accept is called
*/
default
Optional
ifPlugin(Class
cls, Consumer
consumer, Object ... args) {
Optional
optional = getPlugin(cls, args);
optional.ifPresent(consumer);
return optional;
}
/**
* If plugin exists, function.apply is called. The result is returned.
*/
default
Optional mapPlugin(Class cls, Function
function, Object ... args) {
Optional
optional = getPlugin(cls, args);
return optional.map(function);
}
default Optional parent() {
return Optional.empty();
}
}