Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default arguments in CommandContext #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/main/java/com/mojang/brigadier/context/CommandContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

public class CommandContext<S> {

Expand Down Expand Up @@ -77,6 +78,17 @@ public S getSource() {
return source;
}

public boolean hasArgument(final String name) {
return arguments.containsKey(name);
}

public <V> boolean hasArgumentOfType(final String name, final Class<V> clazz) {
final ParsedArgument<S, ?> argument = arguments.get(name);

return argument != null &&
PRIMITIVE_TO_WRAPPER.getOrDefault(clazz, clazz).isAssignableFrom(argument.getResult().getClass());
}

@SuppressWarnings("unchecked")
public <V> V getArgument(final String name, final Class<V> clazz) {
final ParsedArgument<S, ?> argument = arguments.get(name);
Expand All @@ -93,6 +105,38 @@ public <V> V getArgument(final String name, final Class<V> clazz) {
}
}

@SuppressWarnings("unchecked")
public <V> V getArgumentOrDefault(final String name, final Class<V> clazz, final V defaultValue) {
final ParsedArgument<S, ?> argument = arguments.get(name);

if (argument == null) {
return defaultValue;
}

final Object result = argument.getResult();
if (PRIMITIVE_TO_WRAPPER.getOrDefault(clazz, clazz).isAssignableFrom(result.getClass())) {
return (V) result;
} else {
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + result.getClass().getSimpleName() + ", not " + clazz);
}
}

@SuppressWarnings("unchecked")
public <V> V getArgumentOrCompute(final String name, final Class<V> clazz, final Supplier<V> defaultSupplier) {
final ParsedArgument<S, ?> argument = arguments.get(name);

if (argument == null) {
return defaultSupplier.get();
}

final Object result = argument.getResult();
if (PRIMITIVE_TO_WRAPPER.getOrDefault(clazz, clazz).isAssignableFrom(result.getClass())) {
return (V) result;
} else {
throw new IllegalArgumentException("Argument '" + name + "' is defined as " + result.getClass().getSimpleName() + ", not " + clazz);
}
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
Expand Down
28 changes: 26 additions & 2 deletions src/test/java/com/mojang/brigadier/context/CommandContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.RootCommandNode;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -45,12 +44,37 @@ public void testGetArgument_wrongType() throws Exception {
context.getArgument("foo", String.class);
}

@Test
public void testHasArgument() throws Exception {
final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123");
assertThat(context.hasArgument("foo"), is(true));
assertThat(context.hasArgument("bar"), is(false));
}

@Test
public void testHasArgumentOfType() throws Exception {
final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123");
assertThat(context.hasArgumentOfType("foo", Integer.class), is(true));
assertThat(context.hasArgumentOfType("foo", String.class), is(false));
assertThat(context.hasArgumentOfType("bar", Object.class), is(false));
}

@Test
public void testGetArgument() throws Exception {
final CommandContext<Object> context = builder.withArgument("foo", new ParsedArgument<>(0, 1, 123)).build("123");
assertThat(context.getArgument("foo", int.class), is(123));
}

@Test
public void testGetArgumentOrDefault() throws Exception {
assertThat(builder.build("").getArgumentOrDefault("foo", String.class, "bar"), is("bar"));
}

@Test
public void testGetArgumentOrCompute() throws Exception {
assertThat(builder.build("").getArgumentOrCompute("foo", String.class, () -> "bar"), is("bar"));
}

@Test
public void testSource() throws Exception {
assertThat(builder.build("").getSource(), is(source));
Expand Down Expand Up @@ -82,4 +106,4 @@ public void testEquals() throws Exception {
.addEqualityGroup(new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withNode(otherNode, StringRange.between(0, 3)).withNode(node, StringRange.between(4, 6)).build("123 456"), new CommandContextBuilder<>(dispatcher, source, rootNode, 0).withNode(otherNode, StringRange.between(0, 3)).withNode(node, StringRange.between(4, 6)).build("123 456"))
.testEquals();
}
}
}