-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
198 additions
and
44 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
src/main/java/fr/zcraft/quartzlib/components/commands/ArgumentType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package fr.zcraft.quartzlib.components.commands; | ||
|
||
@FunctionalInterface | ||
public interface ArgumentType<T> { | ||
T parse(String raw); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/fr/zcraft/quartzlib/components/commands/ArgumentTypeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package fr.zcraft.quartzlib.components.commands; | ||
|
||
import fr.zcraft.quartzlib.components.commands.ArgumentType; | ||
|
||
public class ArgumentTypeHandler<T> { | ||
private final Class<T> resultType; | ||
private final ArgumentType<T> typeHandler; | ||
|
||
public ArgumentTypeHandler(Class<T> resultType, ArgumentType<T> typeHandler) { | ||
this.resultType = resultType; | ||
this.typeHandler = typeHandler; | ||
} | ||
|
||
public ArgumentType<T> getTypeHandler() { | ||
return typeHandler; | ||
} | ||
|
||
public Class<T> getResultType() { | ||
return resultType; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/fr/zcraft/quartzlib/components/commands/ArgumentTypeHandlerCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package fr.zcraft.quartzlib.components.commands; | ||
|
||
import fr.zcraft.quartzlib.components.commands.arguments.primitive.IntegerTypeHandler; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
class ArgumentTypeHandlerCollection { | ||
private final Map<Class<?>, ArgumentTypeHandler<?>> argumentTypeHandlerMap = new HashMap<>(); | ||
|
||
public ArgumentTypeHandlerCollection () { | ||
this.registerNativeTypes(); | ||
} | ||
|
||
public <T> void register(ArgumentTypeHandler<T> typeHandler) | ||
{ | ||
argumentTypeHandlerMap.put(typeHandler.getResultType(), typeHandler); | ||
} | ||
|
||
public Optional<ArgumentTypeHandler<?>> findTypeHandler(Class<?> resultType) { | ||
return Optional.ofNullable(argumentTypeHandlerMap.get(resultType)); | ||
} | ||
|
||
private void registerNativeTypes () { | ||
register(new ArgumentTypeHandler<>(Integer.class, new IntegerTypeHandler())); | ||
|
||
register(new ArgumentTypeHandler<>(String.class, s -> s)); | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
...ts/commands/internal/CommandEndpoint.java → .../components/commands/CommandEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/fr/zcraft/quartzlib/components/commands/CommandManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package fr.zcraft.quartzlib.components.commands; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
public class CommandManager { | ||
private final Map<String, CommandNode> rootCommands = new HashMap<>(); | ||
private final ArgumentTypeHandlerCollection typeHandlerCollection = new ArgumentTypeHandlerCollection(); | ||
|
||
public <T> void registerCommand(String name, Class<T> commandType, Supplier<T> commandClassSupplier) { | ||
CommandGroup group = new CommandGroup(commandType, commandClassSupplier, name, typeHandlerCollection); | ||
rootCommands.put(name, group); | ||
} | ||
|
||
public void run(String commandName, String... args) { | ||
((CommandGroup) rootCommands.get(commandName)).run(args); // TODO | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/fr/zcraft/quartzlib/components/commands/CommandMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package fr.zcraft.quartzlib.components.commands; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
|
||
class CommandMethod { | ||
private final Method method; | ||
private final String name; | ||
private final CommandMethodArgument[] arguments; | ||
|
||
CommandMethod(Method method, ArgumentTypeHandlerCollection typeHandlerCollection) { | ||
this.method = method; | ||
this.name = method.getName(); | ||
|
||
arguments = Arrays.stream(method.getParameters()) | ||
.map(p -> new CommandMethodArgument(p, typeHandlerCollection)) | ||
.toArray(CommandMethodArgument[]::new); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void run(Object target, String[] args) { | ||
Object[] parsedArgs = parseArguments(args); | ||
try { | ||
this.method.invoke(target, parsedArgs); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
e.printStackTrace(); // TODO | ||
} | ||
} | ||
|
||
private Object[] parseArguments(String[] args) { | ||
Object[] parsed = new Object[args.length]; | ||
|
||
for (int i = 0; i < args.length; i++) { | ||
parsed[i] = arguments[i].parse(args[i]); | ||
} | ||
|
||
return parsed; | ||
} | ||
|
||
public CommandMethodArgument[] getArguments() { | ||
return arguments; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/fr/zcraft/quartzlib/components/commands/CommandMethodArgument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package fr.zcraft.quartzlib.components.commands; | ||
|
||
import java.lang.reflect.Parameter; | ||
|
||
public class CommandMethodArgument { | ||
private final Parameter parameter; | ||
private final ArgumentTypeHandler<?> typeHandler; | ||
|
||
public CommandMethodArgument(Parameter parameter, ArgumentTypeHandlerCollection typeHandlerCollection) { | ||
this.parameter = parameter; | ||
this.typeHandler = typeHandlerCollection.findTypeHandler(parameter.getType()).get(); // FIXME: handle unknown types | ||
} | ||
|
||
public Object parse(String raw) { | ||
return this.typeHandler.getTypeHandler().parse(raw); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...onents/commands/internal/CommandNode.java → ...zlib/components/commands/CommandNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
...nts/commands/internal/DiscoveryUtils.java → ...b/components/commands/DiscoveryUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
.../java/fr/zcraft/quartzlib/components/commands/arguments/primitive/IntegerTypeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package fr.zcraft.quartzlib.components.commands.arguments.primitive; | ||
|
||
import fr.zcraft.quartzlib.components.commands.ArgumentType; | ||
|
||
public class IntegerTypeHandler implements ArgumentType<Integer> { | ||
@Override | ||
public Integer parse(String raw) { | ||
return Integer.parseInt(raw, 10); // TODO: handle exceptions | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
src/main/java/fr/zcraft/quartzlib/components/commands/internal/CommandMethod.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters