Removed DynamicParsedArgument, undoing previous work to split it up.

Realized that returning a fixed partial is the same thing, but doesn't complicate the code so much.
This commit is contained in:
Nathan Adams 2017-07-25 13:18:22 +02:00
parent 8e53d8ff4f
commit ce5eb700b8
13 changed files with 72 additions and 233 deletions

View file

@ -3,7 +3,6 @@ package com.mojang.brigadier.arguments;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.FixedParsedArgument;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
@ -31,9 +30,9 @@ public class BoolArgumentType implements ArgumentType<Boolean> {
}
if (raw.equals("true")) {
return new FixedParsedArgument<>(raw, true);
return new ParsedArgument<>(raw, true);
} else if (raw.equals("false")) {
return new FixedParsedArgument<>(raw, false);
return new ParsedArgument<>(raw, false);
} else {
throw ERROR_INVALID.create();
}

View file

@ -2,7 +2,6 @@ package com.mojang.brigadier.arguments;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.FixedParsedArgument;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException;
@ -19,7 +18,7 @@ public class CommandArgumentType<T> implements ArgumentType<ParseResults<T>> {
final ParseResults<S> parse = contextBuilder.getDispatcher().parse(command, contextBuilder.getSource());
//noinspection unchecked
return new FixedParsedArgument<>(command, (ParseResults<T>) parse);
return new ParsedArgument<>(command, (ParseResults<T>) parse);
}
@Override

View file

@ -1,16 +1,13 @@
package com.mojang.brigadier.arguments;
import com.google.common.base.Splitter;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.FixedParsedArgument;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
import java.util.Objects;
import java.util.Set;
public class IntegerArgumentType implements ArgumentType<Integer> {
public static final ParameterizedCommandExceptionType ERROR_NOT_A_NUMBER = new ParameterizedCommandExceptionType("argument.integer.invalid", "Expected an integer, found '${found}'", "found");
@ -77,7 +74,7 @@ public class IntegerArgumentType implements ArgumentType<Integer> {
throw ERROR_TOO_BIG.create(value, maximum);
}
return new FixedParsedArgument<>(raw, value);
return new ParsedArgument<>(raw, value);
} catch (NumberFormatException ignored) {
throw ERROR_NOT_A_NUMBER.create(number);
}

View file

@ -3,7 +3,6 @@ package com.mojang.brigadier.arguments;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.FixedParsedArgument;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException;
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
@ -40,14 +39,14 @@ public class StringArgumentType implements ArgumentType<String> {
@Override
public <S> ParsedArgument<S, String> parse(String command, CommandContextBuilder<S> contextBuilder) throws CommandException {
if (type == StringType.GREEDY_PHRASE) {
return new FixedParsedArgument<>(command, command);
return new ParsedArgument<>(command, command);
} else if (type == StringType.SINGLE_WORD) {
int index = command.indexOf(CommandDispatcher.ARGUMENT_SEPARATOR);
if (index > 0) {
final String word = command.substring(0, index);
return new FixedParsedArgument<>(word, word);
return new ParsedArgument<>(word, word);
} else {
return new FixedParsedArgument<>(command, command);
return new ParsedArgument<>(command, command);
}
} else {
StringBuilder result = new StringBuilder();
@ -90,7 +89,7 @@ public class StringArgumentType implements ArgumentType<String> {
i++;
}
return new FixedParsedArgument<>(command.substring(0, i), result.toString());
return new ParsedArgument<>(command.substring(0, i), result.toString());
}
}

View file

@ -5,14 +5,13 @@ import com.google.common.collect.Maps;
import com.google.common.primitives.Primitives;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.tree.CommandNode;
import sun.security.x509.OIDMap;
import java.util.Map;
public class CommandContext<S> {
private final S source;
private final Map<String, ParsedArgument<S, ?>> arguments;
private final Command<S> command;
private final Map<String, ParsedArgument<S, ?>> arguments;
private final Map<CommandNode<S>, String> nodes;
private final String input;

View file

@ -1,54 +0,0 @@
package com.mojang.brigadier.context;
import java.util.function.Function;
public class DynamicParsedArgument<S, T> implements ParsedArgument<S, T> {
private final String raw;
private Function<S, T> supplier;
private boolean evaluated;
private T result;
public DynamicParsedArgument(String raw, Function<S, T> supplier) {
this.raw = raw;
this.supplier = supplier;
}
@Override
public String getRaw() {
return raw;
}
@Override
public T getResult(S source) {
if (!evaluated) {
result = supplier.apply(source);
evaluated = true;
}
return result;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof DynamicParsedArgument)) return false;
DynamicParsedArgument that = (DynamicParsedArgument) o;
if (!raw.equals(that.raw)) return false;
if (!supplier.equals(that.supplier)) return false;
return true;
}
@Override
public int hashCode() {
int result = raw.hashCode();
result = 31 * result + supplier.hashCode();
return result;
}
@Override
public ParsedArgument<S, T> copy() {
return new DynamicParsedArgument<>(raw, supplier);
}
}

View file

@ -1,46 +0,0 @@
package com.mojang.brigadier.context;
public class FixedParsedArgument<S, T> implements ParsedArgument<S, T> {
private final String raw;
private final T result;
public FixedParsedArgument(String raw, T result) {
this.raw = raw;
this.result = result;
}
@Override
public String getRaw() {
return raw;
}
@Override
public T getResult(S source) {
return result;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof FixedParsedArgument)) return false;
FixedParsedArgument that = (FixedParsedArgument) o;
if (!raw.equals(that.raw)) return false;
if (!result.equals(that.result)) return false;
return true;
}
@Override
public int hashCode() {
int result = raw.hashCode();
result = 31 * result + this.result.hashCode();
return result;
}
@Override
public ParsedArgument<S, T> copy() {
return new FixedParsedArgument<>(raw, result);
}
}

View file

@ -1,9 +1,43 @@
package com.mojang.brigadier.context;
public interface ParsedArgument<S, T> {
String getRaw();
public class ParsedArgument<S, T> {
private final String raw;
private final T result;
T getResult(S source);
public ParsedArgument(String raw, T result) {
this.raw = raw;
this.result = result;
}
ParsedArgument<S, T> copy();
public String getRaw() {
return raw;
}
public T getResult(S source) {
return result;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ParsedArgument)) return false;
ParsedArgument that = (ParsedArgument) o;
if (!raw.equals(that.raw)) return false;
if (!result.equals(that.result)) return false;
return true;
}
@Override
public int hashCode() {
int result = raw.hashCode();
result = 31 * result + this.result.hashCode();
return result;
}
public ParsedArgument<S, T> copy() {
return new ParsedArgument<>(raw, result);
}
}

View file

@ -3,8 +3,6 @@ package com.mojang.brigadier.arguments;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.exceptions.CommandException;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

View file

@ -82,20 +82,4 @@ public class CommandContextTest {
assertThat(context.getInput(), is("foo 100 baz"));
}
@Test
public void testCopy() throws Exception {
Object first = new Object();
Object second = new Object();
@SuppressWarnings("unchecked") Function<Object, Object> supplier = (Function<Object, Object>) mock(Function.class);
when(supplier.apply(source)).thenReturn(first);
CommandContext<Object> context = builder.withNode(literal("test").build(), "test").withArgument("test", new DynamicParsedArgument<>("test", supplier)).build();
assertThat(context.getArgument("test", Object.class), is(first));
when(supplier.apply(source)).thenReturn(second);
CommandContext<Object> copy = context.copy();
assertThat(context, is(equalTo(copy)));
assertThat(copy.getArgument("test", Object.class), is(second));
}
}

View file

@ -1,68 +0,0 @@
package com.mojang.brigadier.context;
import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.function.Function;
import java.util.function.Supplier;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class DynamicParsedArgumentTest {
private DynamicParsedArgument<Object, Object> subject;
@Mock
private Function<Object, Object> supplier;
@Mock
private Object source;
@Before
public void setUp() throws Exception {
subject = new DynamicParsedArgument<>("raw", supplier);
}
@Test
public void suppliedOnce() throws Exception {
Object result = new Object();
when(supplier.apply(source)).thenReturn(result);
assertThat("first evaluation", subject.getResult(source), is(result));
assertThat("already evaluated", subject.getResult(source), is(result));
verify(supplier, times(1)).apply(source);
}
@Test
public void copy() throws Exception {
Object result = new Object();
when(supplier.apply(source)).thenReturn(result);
assertThat(subject.getResult(source), is(result));
Object newResult = new Object();
when(supplier.apply(source)).thenReturn(newResult);
ParsedArgument<Object, Object> copy = subject.copy();
assertThat(copy.getResult(source), is(newResult));
assertThat(copy, is(equalTo(subject)));
verify(supplier, times(2)).apply(source);
}
@Test
public void testEquals() throws Exception {
new EqualsTester()
.addEqualityGroup(new FixedParsedArgument<>("foo", "bar"), new FixedParsedArgument<>("foo", "bar"))
.addEqualityGroup(new FixedParsedArgument<>("bar", "baz"), new FixedParsedArgument<>("bar", "baz"))
.addEqualityGroup(new FixedParsedArgument<>("foo", "baz"), new FixedParsedArgument<>("foo", "baz"))
.testEquals();
}
}

View file

@ -1,27 +0,0 @@
package com.mojang.brigadier.context;
import com.google.common.testing.EqualsTester;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class FixedParsedArgumentTest {
@Test
public void testEquals() throws Exception {
new EqualsTester()
.addEqualityGroup(new FixedParsedArgument<>("foo", "bar"), new FixedParsedArgument<>("foo", "bar"))
.addEqualityGroup(new FixedParsedArgument<>("bar", "baz"), new FixedParsedArgument<>("bar", "baz"))
.addEqualityGroup(new FixedParsedArgument<>("foo", "baz"), new FixedParsedArgument<>("foo", "baz"))
.testEquals();
}
@Test
public void copy() throws Exception {
final FixedParsedArgument<Object, String> argument = new FixedParsedArgument<>("foo", "bar");
assertThat(argument.copy(), is(equalTo(argument)));
}
}

View file

@ -0,0 +1,25 @@
package com.mojang.brigadier.context;
import com.google.common.testing.EqualsTester;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class ParsedArgumentTest {
@Test
public void testEquals() throws Exception {
new EqualsTester()
.addEqualityGroup(new ParsedArgument<>("foo", "bar"), new ParsedArgument<>("foo", "bar"))
.addEqualityGroup(new ParsedArgument<>("bar", "baz"), new ParsedArgument<>("bar", "baz"))
.addEqualityGroup(new ParsedArgument<>("foo", "baz"), new ParsedArgument<>("foo", "baz"))
.testEquals();
}
@Test
public void copy() throws Exception {
final ParsedArgument<Object, String> argument = new ParsedArgument<>("foo", "bar");
assertThat(argument.copy(), is(equalTo(argument)));
}
}