Add AbstractValidator
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Deprecate AbstractGuardChecker
Dieser Commit ist enthalten in:
yoyosource 2022-06-16 13:18:14 +02:00
Ursprung 492894ca8d
Commit b8d659d1c6
7 geänderte Dateien mit 91 neuen und 3 gelöschten Zeilen

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.command;
@Deprecated
@FunctionalInterface
public interface AbstractGuardChecker<T> {
/**

Datei anzeigen

@ -38,6 +38,7 @@ public abstract class AbstractSWCommand<T> {
private final Map<String, AbstractTypeMapper<T, ?>> localTypeMapper = new HashMap<>();
private final Map<String, AbstractGuardChecker<T>> localGuardChecker = new HashMap<>();
private final Map<String, AbstractValidator<T, ?>> localValidators = new HashMap<>();
protected AbstractSWCommand(Class<T> clazz, String command) {
this(clazz, command, new String[0]);
@ -127,6 +128,20 @@ public abstract class AbstractSWCommand<T> {
SWCommandUtils.getGUARD_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), guardChecker);
}
});
addValidator(Validator.class, method, i -> i == 0, false, AbstractValidator.class, (anno, validator) -> {
if (anno.local()) {
localValidators.putIfAbsent(anno.value(), (AbstractValidator<T, ?>) validator);
} else {
SWCommandUtils.getVALIDATOR_FUNCTIONS().putIfAbsent(anno.value(), validator);
}
});
addValidator(ClassValidator.class, method, i -> i == 0, false, AbstractValidator.class, (anno, validator) -> {
if (anno.local()) {
localValidators.putIfAbsent(anno.value().getTypeName(), (AbstractValidator<T, ?>) validator);
} else {
SWCommandUtils.getVALIDATOR_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), validator);
}
});
}
for (Method method : methods) {
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
@ -228,6 +243,17 @@ public abstract class AbstractSWCommand<T> {
});
}
private <T extends Annotation> void addValidator(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, AbstractValidator<T, ?>> consumer) {
add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> {
try {
method.setAccessible(true);
consumer.accept(anno, (AbstractValidator<T, ?>) method.invoke(this));
} catch (Exception e) {
throw new SecurityException(e.getMessage(), e);
}
});
}
// TODO: Implement this when Message System is ready
/*
public void addDefaultHelpMessage(String message) {
@ -280,6 +306,7 @@ public abstract class AbstractSWCommand<T> {
boolean local() default false;
}
@Deprecated
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.METHOD})
protected @interface Guard {
@ -288,6 +315,7 @@ public abstract class AbstractSWCommand<T> {
boolean local() default false;
}
@Deprecated
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
protected @interface ClassGuard {
@ -296,6 +324,22 @@ public abstract class AbstractSWCommand<T> {
boolean local() default false;
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.METHOD})
protected @interface Validator {
String value() default "";
boolean local() default false;
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
protected @interface ClassValidator {
Class<?> value();
boolean local() default false;
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
protected @interface StaticValue {
@ -320,4 +364,13 @@ public abstract class AbstractSWCommand<T> {
*/
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
protected @interface ErrorMessage {
/**
* Error message to be displayed when the parameter is invalid.
*/
String value();
}
}

Datei anzeigen

@ -21,7 +21,7 @@ package de.steamwar.command;
import java.util.Collection;
public interface AbstractTypeMapper<K, T> {
public interface AbstractTypeMapper<K, T> extends AbstractValidator<K, T> {
/**
* The CommandSender can be null!
*/

Datei anzeigen

@ -0,0 +1,26 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 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.command;
public interface AbstractValidator<K, T> {
default boolean validate(K sender, T value) {
return true;
}
}

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.command;
@Deprecated
public enum GuardCheckType {
COMMAND,
HELP_COMMAND,

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.command;
@Deprecated
public enum GuardResult {
ALLOWED,
DENIED_WITH_HELP,

Datei anzeigen

@ -28,6 +28,7 @@ import java.lang.reflect.Parameter;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
@UtilityClass
public class SWCommandUtils {
@ -36,8 +37,12 @@ public class SWCommandUtils {
private final Map<String, AbstractTypeMapper<?, ?>> MAPPER_FUNCTIONS = new HashMap<>();
@Getter
@Deprecated
private final Map<String, AbstractGuardChecker<?>> GUARD_FUNCTIONS = new HashMap<>();
@Getter
private final Map<String, AbstractValidator<?, ?>> VALIDATOR_FUNCTIONS = new HashMap<>();
private SWTypeMapperCreator swTypeMapperCreator = (mapper, tabCompleter) -> new AbstractTypeMapper<Object, Object>() {
@Override
public Object map(Object sender, String[] previousArguments, String s) {
@ -209,8 +214,9 @@ public class SWCommandUtils {
}
public static <T extends AbstractTypeMapper<K, String>, K> T createMapper(String... values) {
List<String> strings = Arrays.asList(values);
return createMapper((s) -> strings.contains(s) ? s : null, s -> strings);
List<String> strings = Arrays.stream(values).map(String::toLowerCase).collect(Collectors.toList());
List<String> tabCompletes = Arrays.asList(values);
return createMapper(s -> strings.contains(s.toLowerCase()) ? s : null, s -> tabCompletes);
}
public static <T extends AbstractTypeMapper<K, V>, K, V> T createMapper(Function<String, V> mapper, Function<String, Collection<String>> tabCompleter) {