cn.nukkit.plugin.service.NKServiceManager Maven / Gradle / Ivy
package cn.nukkit.plugin.service;
import cn.nukkit.Server;
import cn.nukkit.plugin.Plugin;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.*;
/**
* @since 16-11-20
*/
public class NKServiceManager implements ServiceManager {
private final Map, List>> handle = new HashMap<>();
@Override
public boolean register(Class service, T provider, Plugin plugin, ServicePriority priority) {
Preconditions.checkNotNull(provider);
Preconditions.checkNotNull(priority);
Preconditions.checkNotNull(service);
// build-in service provider needn't plugin param
if (plugin == null && provider.getClass().getClassLoader() != Server.class.getClassLoader()) {
throw new NullPointerException("plugin");
}
return provide(service, provider, plugin, priority);
}
protected boolean provide(Class service, T instance, Plugin plugin, ServicePriority priority) {
synchronized (handle) {
List> list = handle.computeIfAbsent(service, k -> new ArrayList<>());
RegisteredServiceProvider registered = new RegisteredServiceProvider<>(service, instance, priority, plugin);
int position = Collections.binarySearch(list, registered);
if (position > -1) return false;
list.add(-(position + 1), registered);
}
return true;
}
@Override
public List> cancel(Plugin plugin) {
ImmutableList.Builder> builder = ImmutableList.builder();
Iterator> it;
RegisteredServiceProvider> registered;
synchronized (handle) {
for (List> list : handle.values()) {
it = list.iterator();
while (it.hasNext()) {
registered = it.next();
if (registered.getPlugin() == plugin) {
it.remove();
builder.add(registered);
}
}
}
}
return builder.build();
}
@Override
public RegisteredServiceProvider cancel(Class service, T provider) {
RegisteredServiceProvider result = null;
synchronized (handle) {
Iterator> it = handle.get(service).iterator();
RegisteredServiceProvider next;
while (it.hasNext() && result == null) {
next = it.next();
if (next.getProvider() == provider) {
it.remove();
result = next;
}
}
}
return result;
}
@Override
public RegisteredServiceProvider getProvider(Class service) {
synchronized (handle) {
List> list = handle.get(service);
if (list == null || list.isEmpty()) return null;
return (RegisteredServiceProvider) list.get(0);
}
}
@Override
public List> getKnownService() {
return ImmutableList.copyOf(handle.keySet());
}
@Override
public List> getRegistrations(Plugin plugin) {
ImmutableList.Builder> builder = ImmutableList.builder();
synchronized (handle) {
for (List> registered : handle.values()) {
for (RegisteredServiceProvider> provider : registered) {
if (provider.getPlugin().equals(plugin)) {
builder.add(provider);
}
}
}
}
return builder.build();
}
@Override
public List> getRegistrations(Class service) {
ImmutableList.Builder> builder = ImmutableList.builder();
synchronized (handle) {
List> registered = handle.get(service);
if (registered == null) {
ImmutableList> empty = ImmutableList.of();
return empty;
}
for (RegisteredServiceProvider> provider : registered) {
builder.add((RegisteredServiceProvider)provider);
}
}
return builder.build();
}
@Override
public boolean isProvidedFor(Class service) {
synchronized (handle) {
return handle.containsKey(service);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy