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

Resolve sonar code smells #273

Merged
merged 10 commits into from
Dec 27, 2018
10 changes: 5 additions & 5 deletions core/src/main/java/io/parsingdata/metal/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@

public final class Util {

final private static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); // Private because array content is mutable.
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray(); // Private because array content is mutable.

private Util() {}

public static <T>T checkNotNull(final T argument, final String name) {
if (argument == null) {
throw new IllegalArgumentException("Argument " + name + " may not be null.");
throw new IllegalArgumentException(String.format("Argument %s may not be null.", name));
jvdb marked this conversation as resolved.
Show resolved Hide resolved
}
return argument;
}
Expand All @@ -48,15 +48,15 @@ public static <T>T[] checkContainsNoNulls(final T[] arguments, final String name
checkNotNull(arguments, name);
for (final T argument : arguments) {
if (argument == null) {
throw new IllegalArgumentException("Value in array " + name + " may not be null.");
throw new IllegalArgumentException(String.format("Value in array %s may not be null.", name));
}
}
return arguments;
}

public static String checkNotEmpty(final String argument, final String name) {
if (checkNotNull(argument, name).isEmpty()) {
throw new IllegalArgumentException("Argument " + name + " may not be empty.");
throw new IllegalArgumentException(String.format("Argument %s may not be empty.", name));
}
return argument;
}
Expand All @@ -68,7 +68,7 @@ public static boolean notNullAndSameClass(final Object object, final Object othe

public static BigInteger checkNotNegative(final BigInteger argument, final String name) {
if (checkNotNull(argument, name).compareTo(ZERO) < 0) {
throw new IllegalArgumentException("Argument " + name + " may not be negative.");
throw new IllegalArgumentException(String.format("Argument %s may not be negative.", name));
}
return argument;
}
Expand Down
6 changes: 2 additions & 4 deletions core/src/main/java/io/parsingdata/metal/data/ParseState.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import java.util.Optional;

import io.parsingdata.metal.Util;
import io.parsingdata.metal.encoding.Encoding;
import io.parsingdata.metal.expression.value.ValueExpression;
import io.parsingdata.metal.token.Token;

public class ParseState {
Expand Down Expand Up @@ -81,8 +79,8 @@ public Optional<ParseState> seek(final BigInteger newOffset) {
return newOffset.compareTo(ZERO) >= 0 ? Optional.of(new ParseState(order, source, newOffset, iterations)) : Optional.empty();
}

public ParseState source(final ValueExpression dataExpression, final int index, final ParseState parseState, final Encoding encoding) {
return new ParseState(order, new DataExpressionSource(dataExpression, index, parseState, encoding), ZERO, iterations);
public ParseState withSource(final Source source) {
jvdb marked this conversation as resolved.
Show resolved Hide resolved
jvdb marked this conversation as resolved.
Show resolved Hide resolved
return new ParseState(order, source, ZERO, iterations);
}

public Optional<Slice> slice(final BigInteger length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ private Ref(final T reference, final Predicate<ParseValue> predicate, final Valu

public static class NameRef extends Ref<String> {
public NameRef(final String reference) { this(reference, null); }
public NameRef(final String reference, final ValueExpression limit) { super(reference, (value) -> value.matches(reference), limit); }
public NameRef(final String reference, final ValueExpression limit) { super(reference, value -> value.matches(reference), limit); }
}

public static class DefinitionRef extends Ref<Token> {
public DefinitionRef(final Token reference) { this(reference, null); }
public DefinitionRef(final Token reference, final ValueExpression limit) { super(reference, (value) -> value.definition.equals(reference), limit); }
public DefinitionRef(final Token reference, final ValueExpression limit) { super(reference, value -> value.definition.equals(reference), limit); }
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/io/parsingdata/metal/token/Tie.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import io.parsingdata.metal.Trampoline;
import io.parsingdata.metal.Util;
import io.parsingdata.metal.data.DataExpressionSource;
import io.parsingdata.metal.data.Environment;
import io.parsingdata.metal.data.ImmutableList;
import io.parsingdata.metal.data.ParseState;
Expand Down Expand Up @@ -74,7 +75,7 @@ private Trampoline<Optional<ParseState>> iterate(final Environment environment,
}
return values.head
.map(value -> token
.parse(environment.withParseState(environment.parseState.source(dataExpression, index, environment.parseState, environment.encoding)))
.parse(environment.withParseState(environment.parseState.withSource(new DataExpressionSource(dataExpression, index, environment.parseState, environment.encoding))))
.map(nextParseState -> intermediate(() -> iterate(environment.withParseState(nextParseState), values.tail, index + 1, returnParseState)))
.orElseGet(() -> complete(Util::failure)))
.orElseGet(() -> complete(Util::failure));
Expand Down

This file was deleted.

12 changes: 6 additions & 6 deletions core/src/test/java/io/parsingdata/metal/token/UntilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public void threeNewLines() {
assertTrue(parseState.isPresent());
ImmutableList<ParseValue> values = getAllValues(parseState.get().order, "line");
assertEquals(3, values.size);
assertEquals(values.head.asString(), INPUT_3);
assertEquals(values.tail.head.asString(), INPUT_2);
assertEquals(values.tail.tail.head.asString(), INPUT_1);
assertEquals(INPUT_3, values.head.asString());
assertEquals(INPUT_2, values.tail.head.asString());
assertEquals(INPUT_1, values.tail.tail.head.asString());
}

@Test
Expand All @@ -70,9 +70,9 @@ public void untilInclusive() {
assertTrue(parseState.isPresent());
ImmutableList<ParseValue> values = getAllValues(parseState.get().order, "line");
assertEquals(3, values.size);
assertEquals(values.head.asString(), INPUT_3 + '\n');
assertEquals(values.tail.head.asString(), INPUT_2 + '\n');
assertEquals(values.tail.tail.head.asString(), INPUT_1 + '\n');
assertEquals(INPUT_3 + '\n', values.head.asString());
assertEquals(INPUT_2 + '\n', values.tail.head.asString());
assertEquals(INPUT_1 + '\n', values.tail.tail.head.asString());
}

@Test
Expand Down