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;
public abstract class ExecutableService implements Service {
public abstract class ExecutableService implements IService {
private final int serviceID;
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) {
this.serviceID = serviceID;
this.plugin = plugin;
this.serviceID = serviceID;
this.repeating = repeating;
this.delay = delay;
this.period = period;
this.delayed = delayed;
}
@Override
@ -20,4 +28,24 @@ public abstract class ExecutableService implements Service {
public Plugin getProvidingPlugin() {
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 reactor.core.publisher.Mono;
public interface Scheduler {
public interface ISchedule {
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;
import io.github.simplex.simplexss.ServicePool;
import org.bukkit.plugin.Plugin;
import reactor.core.publisher.Mono;
import java.util.concurrent.RunnableScheduledFuture;
public interface Service extends RunnableScheduledFuture<Service> {
public interface IService extends RunnableScheduledFuture<IService> {
int getServiceID();
boolean isDelayed();

View File

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

View File

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

View File

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