-
-
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.
Can now parse all enums by default, and add infrastructure to parse a…
…ny generic type
- Loading branch information
Showing
16 changed files
with
170 additions
and
23 deletions.
There are no files selected for viewing
4 changes: 3 additions & 1 deletion
4
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package fr.zcraft.quartzlib.components.commands; | ||
|
||
import fr.zcraft.quartzlib.components.commands.exceptions.ArgumentParseException; | ||
|
||
@FunctionalInterface | ||
public interface ArgumentType<T> { | ||
T parse(String raw); | ||
T parse(String raw) throws ArgumentParseException; | ||
} |
4 changes: 1 addition & 3 deletions
4
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
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
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
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
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: 7 additions & 0 deletions
7
src/main/java/fr/zcraft/quartzlib/components/commands/GenericArgumentType.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,7 @@ | ||
package fr.zcraft.quartzlib.components.commands; | ||
|
||
import java.util.Optional; | ||
|
||
public interface GenericArgumentType<T> { | ||
Optional<ArgumentType<T>> getMatchingArgumentType(Class<?> type); | ||
} |
59 changes: 59 additions & 0 deletions
59
...main/java/fr/zcraft/quartzlib/components/commands/arguments/generic/EnumArgumentType.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,59 @@ | ||
package fr.zcraft.quartzlib.components.commands.arguments.generic; | ||
|
||
import fr.zcraft.quartzlib.components.commands.ArgumentType; | ||
import fr.zcraft.quartzlib.components.commands.GenericArgumentType; | ||
import fr.zcraft.quartzlib.components.commands.exceptions.ArgumentParseException; | ||
|
||
import java.lang.reflect.Modifier; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class EnumArgumentType implements GenericArgumentType<Enum<?>> { | ||
@Override | ||
public Optional<ArgumentType<Enum<?>>> getMatchingArgumentType(Class<?> type) { | ||
if (type.isEnum()) { | ||
return Optional.of(new DiscreteEnumArgumentType(type)); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
static private class DiscreteEnumArgumentType implements ArgumentType<Enum<?>> { | ||
private final Map<String, Enum<?>> enumValues; | ||
|
||
public DiscreteEnumArgumentType(Class<?> enumClass) { | ||
enumValues = getEnumValues(enumClass); | ||
} | ||
|
||
@Override | ||
public Enum<?> parse(String raw) throws ArgumentParseException { | ||
Enum<?> value = enumValues.get(raw); | ||
if (value == null) throw new EnumParseException(); | ||
return value; | ||
} | ||
} | ||
|
||
static private class EnumParseException extends ArgumentParseException { | ||
|
||
} | ||
|
||
static private Map<String, Enum<?>> getEnumValues (Class<?> enumClass) { | ||
Map<String, Enum<?>> enumValues = new HashMap<>(); | ||
|
||
Arrays.stream(enumClass.getDeclaredFields()) | ||
.filter(f -> Modifier.isPublic(f.getModifiers()) | ||
&& Modifier.isStatic(f.getModifiers()) | ||
&& enumClass.isAssignableFrom(f.getType())) | ||
.forEach(f -> { | ||
try { | ||
f.setAccessible(true); | ||
enumValues.put(f.getName().toLowerCase(), (Enum<?>)f.get(null)); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
|
||
return enumValues; | ||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
src/main/java/fr/zcraft/quartzlib/components/commands/exceptions/ArgumentParseException.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,4 @@ | ||
package fr.zcraft.quartzlib.components.commands.exceptions; | ||
|
||
public class ArgumentParseException extends CommandException { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/fr/zcraft/quartzlib/components/commands/exceptions/CommandException.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,4 @@ | ||
package fr.zcraft.quartzlib.components.commands.exceptions; | ||
|
||
public abstract class CommandException extends Exception { | ||
} |
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
22 changes: 22 additions & 0 deletions
22
...java/fr/zcraft/quartzlib/components/commands/arguments/generic/EnumArgumentTypeTests.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,22 @@ | ||
package fr.zcraft.quartzlib.components.commands.arguments.generic; | ||
|
||
import fr.zcraft.quartzlib.components.commands.ArgumentType; | ||
import fr.zcraft.quartzlib.components.commands.exceptions.ArgumentParseException; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class EnumArgumentTypeTests { | ||
private final EnumArgumentType enumArgumentType = new EnumArgumentType(); | ||
|
||
private enum SimpleEnum { FOO, BAR } | ||
|
||
@Test | ||
public void worksOnSimpleEnum() throws ArgumentParseException { | ||
ArgumentType<?> argumentType = enumArgumentType.getMatchingArgumentType(SimpleEnum.class).get(); | ||
|
||
Assertions.assertEquals(SimpleEnum.FOO, argumentType.parse("foo")); | ||
Assertions.assertEquals(SimpleEnum.BAR, argumentType.parse("bar")); | ||
|
||
Assertions.assertThrows(ArgumentParseException.class, () -> argumentType.parse("blah")); | ||
} | ||
} |