CMDAPIVarArgsSorting #43

Zusammengeführt
YoyoNow hat 3 Commits von CMDAPIVarArgsSorting nach master 2023-02-04 11:54:25 +01:00 zusammengeführt
5 geänderte Dateien mit 157 neuen und 10 gelöschten Zeilen
Nur Änderungen aus Commit ef79a2e7db werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -139,11 +139,8 @@ public abstract class AbstractSWCommand<T> {
});
}
this.commandList.sort((o1, o2) -> {
int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length);
if (compare == 0) return Integer.compare(o1.comparableValue, o2.comparableValue);
return compare;
});
Collections.sort(commandList);
System.out.println(commandList.stream().map(o -> o.method).collect(Collectors.toList()));
initialized = true;
}

Datei anzeigen

@ -32,7 +32,7 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
public class SubCommand<T> {
public class SubCommand<T> implements Comparable<SubCommand<T>> {
private AbstractSWCommand<T> abstractSWCommand;
Method method;
@ -42,7 +42,8 @@ public class SubCommand<T> {
private Function<T, ?> senderFunction;
AbstractValidator<T, T> validator;
boolean noTabComplete;
int comparableValue;
private Parameter[] parameters;
private CommandPart<T> commandPart;
@ -58,9 +59,7 @@ public class SubCommand<T> {
this.description = description;
this.noTabComplete = noTabComplete;
Parameter[] parameters = method.getParameters();
comparableValue = parameters[parameters.length - 1].isVarArgs() ? Integer.MAX_VALUE : -parameters.length;
parameters = method.getParameters();
AbstractSWCommand.Validator validator = parameters[0].getAnnotation(AbstractSWCommand.Validator.class);
if (validator != null) {
this.validator = (AbstractValidator<T, T>) SWCommandUtils.getValidator(validator, parameters[0].getType(), localValidator);
@ -72,6 +71,17 @@ public class SubCommand<T> {
senderFunction = t -> parameters[0].getType().cast(t);
}
@Override
public int compareTo(SubCommand<T> o) {
int tLength = parameters.length + subCommand.length;
int oLength = o.parameters.length + o.subCommand.length;
if (parameters[parameters.length - 1].isVarArgs()) tLength *= -1;
if (o.parameters[o.parameters.length - 1].isVarArgs()) oLength *= -1;
return -Integer.compare(tLength, oLength);
}
boolean invoke(Consumer<Runnable> errors, T sender, String alias, String[] args) {
try {
if (!senderPredicate.test(sender)) {

Datei anzeigen

@ -41,11 +41,13 @@ public class StaticValueCommandTest {
StaticValueCommand cmd = new StaticValueCommand();
try {
cmd.execute("", "", new String[] {"hello"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with hello");
}
try {
cmd.execute("", "", new String[] {"world"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with world");
}
@ -56,16 +58,19 @@ public class StaticValueCommandTest {
StaticValueCommand cmd = new StaticValueCommand();
try {
cmd.execute("", "", new String[] {"-a"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with false");
}
try {
cmd.execute("", "", new String[] {"-b"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true");
}
try {
cmd.execute("", "", new String[] {"-c"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true");
}
@ -76,16 +81,19 @@ public class StaticValueCommandTest {
StaticValueCommand cmd = new StaticValueCommand();
try {
cmd.execute("", "", new String[] {"-d"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true");
}
try {
cmd.execute("", "", new String[] {"-e"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with false");
}
try {
cmd.execute("", "", new String[] {"-f"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true");
}
@ -96,16 +104,19 @@ public class StaticValueCommandTest {
StaticValueCommand cmd = new StaticValueCommand();
try {
cmd.execute("", "", new String[] {"-g"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with int 0");
}
try {
cmd.execute("", "", new String[] {"-h"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with int 1");
}
try {
cmd.execute("", "", new String[] {"-i"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with int 2");
}
@ -116,16 +127,19 @@ public class StaticValueCommandTest {
StaticValueCommand cmd = new StaticValueCommand();
try {
cmd.execute("", "", new String[] {"-j"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with long 0");
}
try {
cmd.execute("", "", new String[] {"-k"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with long 1");
}
try {
cmd.execute("", "", new String[] {"-l"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with long 2");
}

Datei anzeigen

@ -0,0 +1,54 @@
/*
* 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;
import de.steamwar.command.dto.ExecutionIdentifier;
import de.steamwar.command.dto.TestSWCommand;
public class SubCMDSortingCommand extends TestSWCommand {
public SubCMDSortingCommand() {
super("subcmdsorting");
}
@Register
public void test(String s) {
System.out.println("HERE 1");
throw new ExecutionIdentifier("Command with 0 parameters");
}
@Register
public void test(String s, String... args) {
System.out.println("HERE 2");
throw new ExecutionIdentifier("Command with 1 parameters");
}
@Register
public void test(String s, String p, String... args) {
System.out.println("HERE 3");
throw new ExecutionIdentifier("Command with 2 parameters");
}
@Register
public void test(String s, String p, String p2, String... args) {
System.out.println("HERE 4");
throw new ExecutionIdentifier("Command with 3 parameters");
}
}

Datei anzeigen

@ -0,0 +1,72 @@
/*
* 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;
import de.steamwar.command.dto.ExecutionIdentifier;
import org.junit.Test;
import static de.steamwar.AssertionUtils.assertCMDFramework;
public class SubCMDSortingCommandTest {
@Test
public void testNoArgs() {
SubCMDSortingCommand cmd = new SubCMDSortingCommand();
try {
cmd.execute("", "", new String[]{});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "Command with 0 parameters");
}
}
@Test
public void testOneArgs() {
SubCMDSortingCommand cmd = new SubCMDSortingCommand();
try {
cmd.execute("", "", new String[]{"Hello"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "Command with 1 parameters");
}
}
@Test
public void testTwoArgs() {
SubCMDSortingCommand cmd = new SubCMDSortingCommand();
try {
cmd.execute("", "", new String[]{"Hello", "World"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "Command with 2 parameters");
}
}
@Test
public void testThreeArgs() {
SubCMDSortingCommand cmd = new SubCMDSortingCommand();
try {
cmd.execute("", "", new String[]{"Hello", "World", "!"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "Command with 3 parameters");
}
}
}