Pretty sure this library is done

Finalized I think!
This commit is contained in:
Paldiu 2022-11-19 22:36:54 -06:00
parent 6257edb19b
commit bbd09910f3
7 changed files with 112 additions and 94 deletions

View File

@ -2,13 +2,21 @@ package io.github.simplex.api;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
public abstract class ExecutableService implements Service { public abstract class ExecutableService implements IService {
private final int serviceID; private final int serviceID;
private final Plugin plugin; private final Plugin plugin;
private final long delay;
private final long period;
private final boolean delayed;
private final boolean repeating;
public ExecutableService(Plugin plugin, int serviceID, long delay, long period, boolean delayed, boolean repeating) { public ExecutableService(Plugin plugin, int serviceID, long delay, long period, boolean delayed, boolean repeating) {
this.serviceID = serviceID;
this.plugin = plugin; this.plugin = plugin;
this.serviceID = serviceID;
this.repeating = repeating;
this.delay = delay;
this.period = period;
this.delayed = delayed;
} }
@Override @Override
@ -20,4 +28,24 @@ public abstract class ExecutableService implements Service {
public Plugin getProvidingPlugin() { public Plugin getProvidingPlugin() {
return plugin; return plugin;
} }
@Override
public long getDelay() {
return delay;
}
@Override
public long getPeriod() {
return period;
}
@Override
public boolean isDelayed() {
return delayed;
}
@Override
public boolean isRepeating() {
return repeating;
}
} }

View File

@ -4,14 +4,14 @@ import io.github.simplex.simplexss.ServiceManager;
import io.github.simplex.simplexss.ServicePool; import io.github.simplex.simplexss.ServicePool;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
public interface Scheduler { public interface ISchedule {
Mono<ServiceManager> getServiceManager(); Mono<ServiceManager> getServiceManager();
Mono<ServicePool> queue(Service service); Mono<ServicePool> queue(IService service);
Mono<Void> runOnce(Service service); Mono<Void> runOnce(IService service);
Mono<Void> forceStop(Service service); Mono<Void> forceStop(IService service);
Mono<Void> forceStart(Service service); Mono<Void> forceStart(IService service);
} }

View File

@ -1,12 +1,11 @@
package io.github.simplex.api; package io.github.simplex.api;
import io.github.simplex.simplexss.ServicePool;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.util.concurrent.RunnableScheduledFuture; import java.util.concurrent.RunnableScheduledFuture;
public interface Service extends RunnableScheduledFuture<Service> { public interface IService extends RunnableScheduledFuture<IService> {
int getServiceID(); int getServiceID();
boolean isDelayed(); boolean isDelayed();

View File

@ -1,20 +1,22 @@
package io.github.simplex.simplexss; package io.github.simplex.simplexss;
import io.github.simplex.api.Scheduler; import io.github.simplex.api.ISchedule;
import io.github.simplex.api.Service; import io.github.simplex.api.IService;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects;
import java.util.Set; import java.util.Set;
public final class SchedulingSystem implements Scheduler { public final class SchedulingSystem implements ISchedule {
private final ServiceManager serviceManager; private final ServiceManager serviceManager;
private final Plugin plugin; private final Plugin plugin;
private final Set<ServicePool> repeatingPools; private final Set<ServicePool> repeatingPools;
private final Set<ServicePool> delayedPools; private final Set<ServicePool> delayedPools;
public SchedulingSystem(ServiceManager serviceManager, Plugin plugin) { public SchedulingSystem(@NotNull ServiceManager serviceManager, Plugin plugin) {
this.serviceManager = serviceManager; this.serviceManager = serviceManager;
this.plugin = plugin; this.plugin = plugin;
this.repeatingPools = new HashSet<>(); this.repeatingPools = new HashSet<>();
@ -35,27 +37,29 @@ public final class SchedulingSystem implements Scheduler {
} }
@Override @Override
public Mono<ServicePool> queue(Service service) { @NotNull
public Mono<ServicePool> queue(@NotNull IService service) {
return getServiceManager().flatMap(serviceManager -> { return getServiceManager().flatMap(serviceManager -> {
Mono<ServicePool> pool = Mono.justOrEmpty(serviceManager.getAssociatedServicePool(service)); Mono<ServicePool> pool = serviceManager.getAssociatedServicePool(service);
return pool.defaultIfEmpty(serviceManager.createServicePool(service)); return pool.defaultIfEmpty(Objects.requireNonNull(serviceManager.createServicePool(service).block()));
}); });
} }
@Override @Override
public Mono<Void> runOnce(Service service) { public Mono<Void> runOnce(IService service) {
service.start().block(); return Mono.just(service).doOnNext(s -> {
service.stop().block(); s.start();
return Mono.empty(); s.stop();
}).then();
} }
@Override @Override
public Mono<Void> forceStop(Service service) { public Mono<Void> forceStop(IService service) {
return service.stop(); return service.stop();
} }
@Override @Override
public Mono<Void> forceStart(Service service) { public Mono<Void> forceStart(IService service) {
return service.start(); return service.start();
} }

View File

@ -1,10 +1,11 @@
package io.github.simplex.simplexss; package io.github.simplex.simplexss;
import io.github.simplex.api.Service; import io.github.simplex.api.IService;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -20,40 +21,37 @@ public final class ServiceManager {
} }
@Contract(pure = true, value = "_ -> new") @Contract(pure = true, value = "_ -> new")
public @NotNull ServicePool createServicePool(Service... services) { public @NotNull Mono<ServicePool> createServicePool(IService... services) {
ServicePool pool = new ServicePool(); ServicePool pool = new ServicePool();
Stream.of(services).forEach(pool::addService); Stream.of(services).forEach(pool::addService);
return pool; return Mono.just(pool);
} }
@Contract("_, _ -> param1") @Contract("_, _ -> param1")
public ServicePool addToExistingPool(@NotNull ServicePool pool, Service... services) { public Mono<ServicePool> addToExistingPool(@NotNull ServicePool pool, IService... services) {
Stream.of(services).forEach(pool::addService); Stream.of(services).forEach(pool::addService);
return pool; return Mono.just(pool);
} }
@Contract("_, _ -> param1") @Contract("_, _ -> param1")
public ServicePool takeFromExistingPool(@NotNull ServicePool pool, Service... services) { public Mono<ServicePool> takeFromExistingPool(@NotNull ServicePool pool, IService... services) {
Stream.of(services).forEach(pool::removeService); Stream.of(services).forEach(pool::removeService);
return pool; return Mono.just(pool);
} }
public Set<ServicePool> getServicePools() { public Flux<ServicePool> getServicePools() {
return servicePools; return Flux.fromIterable(servicePools);
} }
public boolean locateServiceWithinPools(Service service) { public boolean locateServiceWithinPools(IService service) {
return servicePools.stream().map(p -> p.isValidService(service)).findFirst().orElseGet(() -> false); return servicePools.stream().map(p -> p.isValidService(service)).findFirst().orElseGet(() -> false);
} }
public @Nullable ServicePool getAssociatedServicePool(Service service) { public @NotNull Mono<ServicePool> getAssociatedServicePool(IService service) {
if (!locateServiceWithinPools(service)) return null; if (!locateServiceWithinPools(service)) return Mono.empty();
return getServicePools() return getServicePools()
.stream()
.filter(p -> p.getAssociatedServices().contains(service)) .filter(p -> p.getAssociatedServices().contains(service))
.findFirst() .next();
.orElseGet(() -> null);
} }
public Plugin getProvidingPlugin() { public Plugin getProvidingPlugin() {

View File

@ -1,7 +1,9 @@
package io.github.simplex.simplexss; package io.github.simplex.simplexss;
import io.github.simplex.api.Service; import io.github.simplex.api.IService;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler; import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers; import reactor.core.scheduler.Schedulers;
@ -13,11 +15,9 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public final class ServicePool { public final class ServicePool {
private final Set<Service> associatedServices; private final Set<IService> associatedServices;
private final Scheduler scheduler; private final Scheduler scheduler;
private final ExecutorService executor; private final ExecutorService executor;
private boolean delayed = false;
private boolean repeating = false;
public ServicePool() { public ServicePool() {
this.associatedServices = new HashSet<>(); this.associatedServices = new HashSet<>();
@ -25,55 +25,61 @@ public final class ServicePool {
this.scheduler = Schedulers.fromExecutorService(executor); this.scheduler = Schedulers.fromExecutorService(executor);
} }
void addService(Service service) { void addService(IService service) {
getAssociatedServices().add(service); getAssociatedServices().add(service);
} }
boolean isValidService(Service service) { boolean isValidService(IService service) {
return getAssociatedServices().contains(service); return getAssociatedServices().contains(service);
} }
@NotNull @NotNull
public Set<Service> getAssociatedServices() { public Set<IService> getAssociatedServices() {
return associatedServices; return associatedServices;
} }
public Mono<Void> startServices() { public Flux<Disposable> startServices() {
return Mono.just(getAssociatedServices()).doOnNext(services -> { return Mono.just(getAssociatedServices()).flatMapMany(services -> {
for (Service service : services) { Set<Disposable> disposables = new HashSet<>();
for (IService service : services) {
if (service.isRepeating()) { if (service.isRepeating()) {
scheduler.schedulePeriodically(service, service.getDelay() * 5, service.getPeriod() * 5, TimeUnit.MILLISECONDS); disposables.add(scheduler.schedulePeriodically(service,
service.getDelay() * 5,
service.getPeriod() * 5,
TimeUnit.MILLISECONDS));
} else if (service.isDelayed()) { } else if (service.isDelayed()) {
scheduler.schedule(service, service.getDelay() * 5, TimeUnit.MILLISECONDS); disposables.add(scheduler.schedule(service,
service.getDelay() * 5,
TimeUnit.MILLISECONDS));
} else {
disposables.add(scheduler.schedule(service));
} }
} }
}).then(); ;
return Flux.fromIterable(disposables);
});
} }
public Mono<Void> stopServices() { public Mono<Void> stopServices(Flux<Disposable> disposableThread) {
return Mono.just(getAssociatedServices()).doOnNext(services -> { return Mono.just(getAssociatedServices()).doOnNext(services -> {
for (Service service : services) { for (IService service : services) {
service.stop(); disposableThread.doOnNext(Disposable::dispose);
} }
}).then(); }).then();
} }
public Service getService(int serviceID) { public Mono<IService> getService(int serviceID) {
return getAssociatedServices() return Flux.fromIterable(getAssociatedServices())
.stream() .filter(service -> service.getServiceID() == serviceID)
.filter(s -> s.getServiceID() == serviceID) .next();
.findFirst()
.orElse(null);
} }
public void removeService(Service service) { void removeService(IService service) {
getAssociatedServices().remove(service); getAssociatedServices().remove(service);
} }
public ServicePool recycle() { public ServicePool recycle() {
this.getAssociatedServices().clear(); this.getAssociatedServices().clear();
this.repeating = false;
this.delayed = false;
return this; return this;
} }
} }

View File

@ -1,17 +0,0 @@
package io.github.simplex.simplexss;
import org.bukkit.plugin.java.JavaPlugin;
public final class SimplexSS extends JavaPlugin {
@Override
public void onEnable() {
// Plugin startup logic
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
}