Skip to content

Commit

Permalink
Added settable event option
Browse files Browse the repository at this point in the history
  • Loading branch information
Smudgge committed Nov 19, 2023
1 parent 273cf03 commit 00ae173
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 3 deletions.
47 changes: 47 additions & 0 deletions src/main/java/com/github/kerbity/kerb/event/SettableEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.Settable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Used to create a single settable event.
* An event used as a request to get a value from the clients.
*
* @param <T> The type of class to get.
*/
public class SettableEvent<T> implements Event, Settable<T, SettableEvent<T>> {

private @Nullable T instance;

@Override
public @NotNull SettableEvent<T> set(@Nullable T instance) {
this.instance = instance;
return this;
}

@Override
public @Nullable T get() {
return this.instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* @param <T> The instance of the class to return.
*/
public interface Cancellable<T> {
public interface Cancellable<T extends Cancellable<T>> {

/**
* Used to set weather the class is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @param <T> The instance of the class that
* is completable.
*/
public interface Completable<T> {
public interface Completable<T extends Completable<T>> {

/**
* Used to complete the task.
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/com/github/kerbity/kerb/indicator/Settable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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;
import org.jetbrains.annotations.Nullable;

/**
* Indicates if the class is settable
* to a specific type.
*
* @param <T> The specific settable type.
* @param <C> The settable class type.
*/
public interface Settable<T, C extends Settable<T, C>> {

/**
* Used to set the settable value.
*
* @param instance The instance of the value.
* @return This instance.
*/
@NotNull C set(@Nullable T instance);

/**
* Used to get the settable value.
*
* @return The instance of the value.
*/
@Nullable T get();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.github.kerbity.kerb.datatype.Ratio;
import com.github.kerbity.kerb.indicator.Cancellable;
import com.github.kerbity.kerb.indicator.Completable;
import com.github.kerbity.kerb.indicator.Settable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -46,6 +47,7 @@ public class CompletableResultSet<T> {
private boolean isComplete;
private boolean containsCancelled;
private boolean containsCompleted;
private Object defaultSettableValue;

/**
* Used to create a completable
Expand Down Expand Up @@ -361,7 +363,7 @@ public boolean containsCancelled() {
*/
public boolean containsCompleted() {

// Check if a result has been canceled.
// Check if a result contains a completed class.
for (T result : this.result) {
if (!(result instanceof Completable<?> completable)) continue;
if (completable.isComplete()) return true;
Expand All @@ -370,4 +372,40 @@ public boolean containsCompleted() {
// Return if all results should be rendered as canceled.
return this.containsCompleted;
}

/**
* Used to set the default settable value if no
* event returns a settable value.
* This is specified in the {@link com.github.kerbity.kerb.indicator.Settable}
* interface.
*
* @param defaultSettableValue The default value.
* @return This instance.
*/
public @NotNull CompletableResultSet<T> setDefaultSettableValue(@Nullable Object defaultSettableValue) {
this.defaultSettableValue = defaultSettableValue;
return this;
}

@SuppressWarnings("unchecked")
public <C> @NotNull C getFirstSettable(@NotNull Class<C> type) {

// Check if a result contains a settable value.
for (T result : this.result) {
if (!(result instanceof Settable<?, ?> settable)) continue;
if (settable.get() == null) continue;

Object value = settable.get();

if (!type.isInstance(value)) continue;
return (C) value;
}

// Return the default value.
try {
return (C) this.defaultSettableValue;
} catch (Exception ignored) {
return null;
}
}
}

0 comments on commit 00ae173

Please sign in to comment.