SteamWar/SpigotCore
Archiviert
13
0

Simplify VersionedCallable

Simplify VersionedRunnable
Remove ExceptionlessCallable
Dieser Commit ist enthalten in:
jojo 2020-12-25 18:30:46 +01:00
Ursprung 97a766c797
Commit e01a72a42b
3 geänderte Dateien mit 11 neuen und 38 gelöschten Zeilen

Datei anzeigen

@ -1,25 +0,0 @@
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.core;
@FunctionalInterface
public interface ExceptionlessCallable<T> {
T call();
}

Datei anzeigen

@ -21,25 +21,27 @@
package de.steamwar.core; package de.steamwar.core;
import java.util.concurrent.Callable;
public class VersionedCallable<T> { public class VersionedCallable<T> {
private ExceptionlessCallable<T> exceptionlessCallable; private Callable<T> callable;
private int minVersion; private int minVersion;
public VersionedCallable(ExceptionlessCallable<T> exceptionlessCallable, int minVersion) { public VersionedCallable(Callable<T> callable, int minVersion) {
this.exceptionlessCallable = exceptionlessCallable; this.callable = callable;
this.minVersion = minVersion; this.minVersion = minVersion;
} }
public T call() {
return exceptionlessCallable.call();
}
public static <T> T call(VersionedCallable<T>... versionedCallables) { public static <T> T call(VersionedCallable<T>... versionedCallables) {
for (int i = versionedCallables.length - 1; i >= 0; i--) { for (int i = versionedCallables.length - 1; i >= 0; i--) {
VersionedCallable<T> versionedCallable = versionedCallables[i]; VersionedCallable<T> versionedCallable = versionedCallables[i];
if (Core.getVersion() >= versionedCallable.minVersion) { if (Core.getVersion() >= versionedCallable.minVersion) {
return versionedCallable.call(); try {
return versionedCallable.callable.call();
} catch (Exception e) {
throw new RuntimeException("Could not run version dependant code", e);
}
} }
} }
throw new SecurityException(); throw new SecurityException();

Datei anzeigen

@ -31,15 +31,11 @@ public class VersionedRunnable {
this.minVersion = minVersion; this.minVersion = minVersion;
} }
public void run() {
runnable.run();
}
public static void call(VersionedRunnable... versionedRunnables) { public static void call(VersionedRunnable... versionedRunnables) {
for (int i = versionedRunnables.length - 1; i >= 0; i--) { for (int i = versionedRunnables.length - 1; i >= 0; i--) {
VersionedRunnable versionedRunnable = versionedRunnables[i]; VersionedRunnable versionedRunnable = versionedRunnables[i];
if (Core.getVersion() >= versionedRunnable.minVersion) { if (Core.getVersion() >= versionedRunnable.minVersion) {
versionedRunnable.run(); versionedRunnable.runnable.run();
return; return;
} }
} }