Skip to content

Commit

Permalink
Added completable event
Browse files Browse the repository at this point in the history
  • Loading branch information
Smudgge committed Nov 19, 2023
1 parent bb077bd commit 768c261
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

/**
* Represents a cancellable event.
* Lets you set the event to be canceled when returned as a result.
* <ul>
* <li>This won't stop the event being sent to all clients and listeners.</li>
* </ul>
*/
public class CancellableEvent implements Event, Cancellable<CancellableEvent> {

Expand Down
61 changes: 61 additions & 0 deletions src/main/java/com/github/kerbity/kerb/event/CompletableEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Kerb
* Event and request distributor server software.
*
* Copyright (C) 2023 Smuddgge
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.github.kerbity.kerb.event;

import com.github.kerbity.kerb.indicator.Completable;
import org.jetbrains.annotations.NotNull;

/**
* Used to create a completable event.
* This lets you check if an event is completed
* and lets you set to true when listening to it.
* This essentially makes the event act as a task
* to complete.
*/
public class CompletableEvent implements Event, Completable<CompletableEvent> {

private boolean isComplete;

/**
* Used to create a completable event.
* An event that can be marked as completed.
*/
public CompletableEvent() {
this.isComplete = false;
}

@Override
public @NotNull CompletableEvent complete() {
this.isComplete = true;
return this;
}

@Override
public @NotNull CompletableEvent setComplete(boolean isComplete) {
this.isComplete = isComplete;
return this;
}

@Override
public boolean isComplete() {
return this.isComplete;
}
}
54 changes: 54 additions & 0 deletions src/main/java/com/github/kerbity/kerb/indicator/Completable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Kerb
* Event and request distributor server software.
*
* Copyright (C) 2023 Smuddgge
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.github.kerbity.kerb.indicator;

import org.jetbrains.annotations.NotNull;

/**
* Indicates if a class is completable.
*
* @param <T> The instance of the class that
* is completable.
*/
public interface Completable<T> {

/**
* Used to complete the task.
*
* @return The instance of the completable class.
*/
@NotNull T complete();

/**
* Used to set if the class is completed.
*
* @param isComplete True if the class is completed.
* @return The instance of the completable class.
*/
@NotNull T setComplete(boolean isComplete);

/**
* Used to check if the class is completed.
*
* @return True if the class is completed.
*/
boolean isComplete();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.github.kerbity.kerb.datatype.Ratio;
import com.github.kerbity.kerb.indicator.Cancellable;
import com.github.kerbity.kerb.indicator.Completable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -36,14 +37,15 @@
*
* @param <T> The type of result.
*/
public class CompletableResultSet<T> implements Cancellable<CompletableResultSet<T>> {
public class CompletableResultSet<T> {

private static final int LOCK_TIME_MILLS = 100;

private final @NotNull List<T> result;
private final int size;
private boolean isComplete;
private boolean isCancelled;
private boolean containsCancelled;
private boolean containsCompleted;

/**
* Used to create a completable
Expand All @@ -56,7 +58,7 @@ public CompletableResultSet(int size) {
this.result = new ArrayList<>();
this.size = size;
this.isComplete = false;
this.isCancelled = false;
this.containsCancelled = false;
}

/**
Expand Down Expand Up @@ -295,13 +297,12 @@ public boolean containsNonNull() {
* Used to set if the results should be perceived as cancelled.
* This will not stop new results from appearing.
*
* @param isCancelled True if the results should be
* perceived as cancelled.
* @param containsCancelled True if the results should be
* perceived as cancelled.
* @return This instance.
*/
@Override
public @NotNull CompletableResultSet<T> setCancelled(boolean isCancelled) {
this.isCancelled = isCancelled;
public @NotNull CompletableResultSet<T> setContainsCancelled(boolean containsCancelled) {
this.containsCancelled = containsCancelled;
return this;
}

Expand All @@ -311,8 +312,7 @@ public boolean containsNonNull() {
*
* @return True if cancelled.
*/
@Override
public boolean isCancelled() {
public boolean containsCancelled() {

// Check if a result has been canceled.
for (T result : this.result) {
Expand All @@ -321,6 +321,36 @@ public boolean isCancelled() {
}

// Return if all results should be rendered as canceled.
return this.isCancelled;
return this.containsCancelled;
}

/**
* Used to set if the results should be perceived as completed.
*
* @param containsCompleted True if the results should be seen as
* containing a completed result.
* @return This instance.
*/
public @NotNull CompletableResultSet<T> setContainsCompleted(boolean containsCompleted) {
this.containsCancelled = containsCompleted;
return this;
}

/**
* Used to check if the results contain a result
* that has the completed value set to true.
*
* @return True if a result is set to be completed.
*/
public boolean containsCompleted() {

// Check if a result has been canceled.
for (T result : this.result) {
if (!(result instanceof Completable<?> completable)) continue;
if (completable.isComplete()) return true;
}

// Return if all results should be rendered as canceled.
return this.containsCompleted;
}
}

0 comments on commit 768c261

Please sign in to comment.