Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mimbrero committed Jul 13, 2023
1 parent 0e933d0 commit e685244
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package st.networkers.rimor.command;

import st.networkers.rimor.instruction.InstructionResolver;
import st.networkers.rimor.instruction.ResolvedInstructions;
import st.networkers.rimor.instruction.InstructionProcessor;
import st.networkers.rimor.instruction.InstructionSet;
import st.networkers.rimor.util.ReflectionUtils;

import java.util.Arrays;
Expand All @@ -14,22 +14,22 @@
*/
public class CommandResolver {

private final InstructionResolver instructionResolver;
private final InstructionProcessor instructionProcessor;

public CommandResolver() {
this(new InstructionResolver());
this(new InstructionProcessor());
}

public CommandResolver(InstructionResolver instructionResolver) {
this.instructionResolver = instructionResolver;
public CommandResolver(InstructionProcessor instructionProcessor) {
this.instructionProcessor = instructionProcessor;
}

public MappedCommand resolve(Object commandInstance) {
if (!commandInstance.getClass().isAnnotationPresent(Command.class))
throw new IllegalArgumentException("there is no Command annotation in " + commandInstance.getClass().getName());

List<String> identifiers = this.resolveIdentifiers(commandInstance);
ResolvedInstructions instructions = instructionResolver.resolveInstructions(commandInstance);
InstructionSet instructions = instructionProcessor.resolveInstructions(commandInstance);
Collection<MappedCommand> subcommands = this.resolveSubcommands(commandInstance);

return new MappedCommandBuilder()
Expand Down
63 changes: 56 additions & 7 deletions rimor/src/main/java/st/networkers/rimor/command/MappedCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@
*/
public class MappedCommand {

private final Object commandInstance;
private final Object bean;

private final List<String> identifiers;
private final Instruction mainInstruction;
private final Map<String, Instruction> instructions;
private final Map<String, MappedCommand> subcommands;

public MappedCommand(Object commandInstance,
public MappedCommand(Object bean,
List<String> identifiers,
Instruction mainInstruction,
Map<String, Instruction> instructions,
Map<String, MappedCommand> subcommands) {
this.commandInstance = commandInstance;
this.bean = bean;
this.identifiers = identifiers.stream().map(String::toLowerCase).collect(Collectors.toList());
this.mainInstruction = mainInstruction;
this.instructions = instructions;
this.subcommands = subcommands;
}

public Object getCommandInstance() {
return commandInstance;
public Object getBean() {
return bean;
}

public Collection<String> getIdentifiers() {
Expand Down Expand Up @@ -69,11 +69,60 @@ public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MappedCommand)) return false;
MappedCommand that = (MappedCommand) o;
return Objects.equals(commandInstance, that.commandInstance) && Objects.equals(identifiers, that.identifiers) && Objects.equals(mainInstruction, that.mainInstruction) && Objects.equals(instructions, that.instructions) && Objects.equals(subcommands, that.subcommands);
return Objects.equals(bean, that.bean) && Objects.equals(identifiers, that.identifiers) && Objects.equals(mainInstruction, that.mainInstruction) && Objects.equals(instructions, that.instructions) && Objects.equals(subcommands, that.subcommands);
}

@Override
public int hashCode() {
return Objects.hash(commandInstance, identifiers, mainInstruction, instructions, subcommands);
return Objects.hash(bean, identifiers, mainInstruction, instructions, subcommands);
}

public static class Builder {
private Object commandInstance;
private List<String> identifiers;
private Instruction mainInstruction;
private Map<String, Instruction> instructions;
private Map<String, MappedCommand> subcommands;

public Builder setCommandInstance(Object commandInstance) {
this.commandInstance = commandInstance;
return this;
}

public Builder setIdentifiers(List<String> identifiers) {
this.identifiers = identifiers;
return this;
}

public Builder setMainInstruction(Instruction mainInstruction) {
this.mainInstruction = mainInstruction;
return this;
}

public Builder setInstructions(Collection<Instruction> instructions) {
Map<String, Instruction> mappedInstructions = new HashMap<>();
instructions.forEach(instruction -> instruction.getIdentifiers().forEach(identifier -> mappedInstructions.put(identifier.toLowerCase(), instruction)));
return this.setInstructions(mappedInstructions);
}

private Builder setInstructions(Map<String, Instruction> instructions) {
this.instructions = instructions;
return this;
}

public Builder setSubcommands(Collection<MappedCommand> subcommands) {
Map<String, MappedCommand> mappedSubcommands = new HashMap<>();
subcommands.forEach(subcommand -> subcommand.getIdentifiers().forEach(identifier -> mappedSubcommands.put(identifier.toLowerCase(), subcommand)));
return this.setSubcommands(mappedSubcommands);
}

private Builder setSubcommands(Map<String, MappedCommand> subcommands) {
this.subcommands = subcommands;
return this;
}

public MappedCommand create() {
return new MappedCommand(commandInstance, identifiers, mainInstruction, instructions, subcommands);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public interface CommandExecutor {
/**
* Executes the given {@link Instruction} with the given {@link ExecutionContext}.
*
* @param instruction instruction to execute.
* @param context context of the command execution.
* @param instruction instruction to execute.
* @param executionContext context of the command execution.
* @return the result of executing the instruction method.
*/
Object execute(Instruction instruction, ExecutionContext context);
Object execute(Instruction instruction, ExecutionContext executionContext);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public DefaultCommandExecutor(RimorInjector injector) {
}

@Override
public Object execute(Instruction instruction, ExecutionContext context) {
return injector.invokeMethod(instruction.getMethod(), instruction.getCommandInstance(), context);
public Object execute(Instruction instruction, ExecutionContext executionContext) {
return instruction.run(executionContext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package st.networkers.rimor.instruction;

import st.networkers.rimor.inject.ExecutionContext;
import st.networkers.rimor.inject.RimorInjector;
import st.networkers.rimor.reflect.CachedMethod;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Objects;

public class HandlerMethodInstruction implements Instruction {

public static Builder builder() {
return new Builder();
}

private final RimorInjector injector;

private final Object bean;
private final CachedMethod method;
private final Collection<String> identifiers;

public HandlerMethodInstruction(RimorInjector injector, Object bean, CachedMethod method, Collection<String> identifiers) {
this.injector = injector;
this.bean = bean;
this.method = method;
this.identifiers = identifiers;
}

@Override
public Object run(ExecutionContext executionContext) {
return injector.invokeMethod(method, bean, executionContext);
}

public Object getBean() {
return bean;
}

public CachedMethod getMethod() {
return method;
}

public Collection<String> getIdentifiers() {
return identifiers;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof HandlerMethodInstruction)) return false;
HandlerMethodInstruction that = (HandlerMethodInstruction) o;
return Objects.equals(bean, that.bean) && Objects.equals(method, that.method) && Objects.equals(identifiers, that.identifiers);
}

@Override
public int hashCode() {
return Objects.hash(bean, method, identifiers);
}

public static class Builder {

private RimorInjector injector;
private Object bean;
private Method method;
private Collection<String> identifiers;

public Builder setInjector(RimorInjector injector) {
this.injector = injector;
return this;
}

public Builder setBean(Object bean) {
this.bean = bean;
return this;
}

public Builder setMethod(Method method) {
this.method = method;
return this;
}

public Builder setIdentifiers(Collection<String> identifiers) {
this.identifiers = identifiers;
return this;
}

public HandlerMethodInstruction create() {
return new HandlerMethodInstruction(injector, bean, CachedMethod.build(method), identifiers);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@
package st.networkers.rimor.instruction;

import st.networkers.rimor.execute.Executable;
import st.networkers.rimor.reflect.CachedMethod;
import st.networkers.rimor.inject.ExecutionContext;

import java.util.Collection;
import java.util.Objects;
/**
* A command instruction.
*/
public interface Instruction extends Executable {

public class Instruction implements Executable {
Object run(ExecutionContext executionContext);

private final Object commandInstance;
private final CachedMethod method;

private final Collection<String> identifiers;

public Instruction(Object commandInstance, CachedMethod method, Collection<String> identifiers) {
this.commandInstance = commandInstance;
this.method = method;
this.identifiers = identifiers;
}

public Object getCommandInstance() {
return commandInstance;
}

public CachedMethod getMethod() {
return method;
}

public Collection<String> getIdentifiers() {
return identifiers;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Instruction)) return false;
Instruction that = (Instruction) o;
return Objects.equals(commandInstance, that.commandInstance) && Objects.equals(method, that.method) && Objects.equals(identifiers, that.identifiers);
}

@Override
public int hashCode() {
return Objects.hash(commandInstance, method, identifiers);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@
/**
* Class to resolve annotation-based instruction handler methods.
*/
public class InstructionResolver {
public class InstructionProcessor {

public ResolvedInstructions resolveInstructions(Object commandInstance) {
ResolvedInstructions results = new ResolvedInstructions();
public InstructionSet resolveInstructions(Object bean) {
InstructionSet instructionSet = new InstructionSet();

for (Method method : commandInstance.getClass().getMethods()) {
for (Method method : bean.getClass().getMethods()) {
if (!method.isAnnotationPresent(MainInstructionMapping.class) && !method.isAnnotationPresent(InstructionMapping.class))
continue;

Instruction instruction = this.resolveInstruction(commandInstance, method);
Instruction instruction = this.resolveInstruction(bean, method);
if (method.isAnnotationPresent(MainInstructionMapping.class))
results.setMainInstruction(instruction);
instructionSet.setMainInstruction(instruction);

if (method.isAnnotationPresent(InstructionMapping.class))
results.addInstruction(instruction);
instructionSet.addInstruction(instruction);
}
return results;
return instructionSet;
}

public Instruction resolveInstruction(Object commandInstance, Method method) {
public Instruction resolveInstruction(Object bean, Method method) {
if (!method.isAnnotationPresent(InstructionMapping.class) && !method.isAnnotationPresent(MainInstructionMapping.class))
throw new IllegalArgumentException("there is no InstructionMapping or MainInstructionMapping annotation in " + method.getName());

return new InstructionBuilder()
.setCommandInstance(commandInstance)
return HandlerMethodInstruction.builder()
.setBean(bean)
.setMethod(method)
.setIdentifiers(this.resolveIdentifiers(method))
.create();
Expand Down
Loading

0 comments on commit e685244

Please sign in to comment.