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
29 changes: 17 additions & 12 deletions formats/src/main/java/io/parsingdata/metal/format/JPEG.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,36 @@
*/
public final class JPEG {

public static final String MARKER = "marker";
public static final String IDENTIFIER = "identifier";
public static final String LENGTH = "length";
public static final String PAYLOAD = "payload";

private JPEG() {}

private static final Token HEADER =
seq("start of image",
def("marker", con(1), eq(con(0xff))),
def("identifier", con(1), eq(con(0xd8))));
def(MARKER, con(1), eq(con(0xff))),
def(IDENTIFIER, con(1), eq(con(0xd8))));

private static final Token FOOTER =
seq("end of image",
def("marker", con(1), eq(con(0xff))),
def("identifier", con(1), eq(con(0xd9))));
def(MARKER, con(1), eq(con(0xff))),
def(IDENTIFIER, con(1), eq(con(0xd9))));

private static final Token SIZED_SEGMENT =
seq("sized segment",
def("marker", con(1), eq(con(0xff))),
def("identifier", con(1), or(ltNum(con(0xd8)), gtNum(con(0xda)))),
def("length", con(2)),
def("payload", sub(last(ref("length")), con(2))));
def(MARKER, con(1), eq(con(0xff))),
def(IDENTIFIER, con(1), or(ltNum(con(0xd8)), gtNum(con(0xda)))),
def(LENGTH, con(2)),
def(PAYLOAD, sub(last(ref(LENGTH)), con(2))));

private static final Token SCAN_SEGMENT =
seq("scan segment",
def("marker", con(1), eq(con(0xff))),
def("identifier", con(1), eq(con(0xda))),
def("length", con(2)),
def("payload", sub(last(ref("length")), con(2))),
def(MARKER, con(1), eq(con(0xff))),
def(IDENTIFIER, con(1), eq(con(0xda))),
def(LENGTH, con(2)),
def(PAYLOAD, sub(last(ref(LENGTH)), con(2))),
rep(cho(def("scandata", con(1), not(eq(con(0xff)))),
def("escape", con(2), or(eq(con(0xff00)), and(gtNum(con(0xffcf)), ltNum(con(0xffd8))))))));

Expand Down
20 changes: 13 additions & 7 deletions formats/src/main/java/io/parsingdata/metal/format/PNG.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@

public final class PNG {

public static final String LENGTH = "length";
public static final String TYPE = "type";
public static final String CRC_32 = "crc32";
public static final String IEND = "IEND";
public static final String DATA = "data";

private PNG() {}

private static final Token HEADER =
Expand All @@ -43,16 +49,16 @@ private PNG() {}

private static final Token FOOTER =
seq("footer",
def("footerlength", con(4), eqNum(con(0))),
def("footertype", con(4), eq(con("IEND"))),
def("footercrc32", con(4), eq(con(0xae, 0x42, 0x60, 0x82))));
def(LENGTH, con(4), eqNum(con(0))),
def(TYPE, con(4), eq(con(IEND))),
def(CRC_32, con(4), eq(con(0xae, 0x42, 0x60, 0x82))));

private static final Token STRUCT =
seq("chunk",
def("length", con(4)),
def("chunktype", con(4), not(eq(con("IEND")))),
def("chunkdata", last(ref("length"))),
def("crc32", con(4), eq(crc32(cat(last(ref("chunktype")), last(ref("chunkdata")))))));
def(LENGTH, con(4)),
def(TYPE, con(4), not(eq(con(IEND)))),
def(DATA, last(ref(LENGTH))),
def(CRC_32, con(4), eq(crc32(cat(last(ref(TYPE)), last(ref(DATA)))))));

public static final Token FORMAT =
seq("PNG", DEFAULT_ENCODING,
Expand Down
77 changes: 46 additions & 31 deletions formats/src/main/java/io/parsingdata/metal/format/ZIP.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,53 @@
*/
public final class ZIP {

public static final String CRC_32 = "crc32";
public static final String COMPRESSED_SIZE = "compressedsize";
public static final String FILE_NAME_SIZE = "filenamesize";
public static final String EXTRA_FIELD_SIZE = "extrafieldsize";
public static final String EXTRACT_VERSION = "extractversion";
public static final String BIT_FLAG = "bitflag";
public static final String COMPRESSION_METHOD = "compressionmethod";
public static final String LAST_MOD_TIME = "lastmodtime";
public static final String LAST_MOD_DATE = "lastmoddate";
public static final String UNCOMPRESSED_SIZE = "uncompressedsize";
public static final String FILE_NAME = "filename";
public static final String COMPRESSED_DATA = "compresseddata";
public static final String DIR_SIGNATURE = "dirsignature";
public static final String EXTRA_FIELD = "extrafield";
jvdb marked this conversation as resolved.
Show resolved Hide resolved

private ZIP() {}

private static Token localFileBody(final String name, final int cm, final Expression crc, final Expression cs, final Expression usp) {
return
seq(name,
def("filesignature", con(4), eq(con(0x50, 0x4b, 0x03, 0x04))),
def("extractversion", con(2)),
def("bitflag", con(2)),
def("compressionmethod", con(2), eqNum(con(cm))),
def("lastmodtime", con(2)),
def("lastmoddate", con(2)),
def("crc32", con(4), crc),
def("compressedsize", con(4), cs),
def("uncompressedsize", con(4), usp),
def("filenamesize", con(2)),
def("extrafieldsize", con(2)),
def("filename", last(ref("filenamesize"))),
def("extrafield", last(ref("extrafieldsize"))));
def(EXTRACT_VERSION, con(2)),
def(BIT_FLAG, con(2)),
def(COMPRESSION_METHOD, con(2), eqNum(con(cm))),
def(LAST_MOD_TIME, con(2)),
def(LAST_MOD_DATE, con(2)),
def(CRC_32, con(4), crc),
def(COMPRESSED_SIZE, con(4), cs),
def(UNCOMPRESSED_SIZE, con(4), usp),
def(FILE_NAME_SIZE, con(2)),
def(EXTRA_FIELD_SIZE, con(2)),
def(FILE_NAME, last(ref(FILE_NAME_SIZE))),
def(EXTRA_FIELD, last(ref(EXTRA_FIELD_SIZE))));
}

private static final Token LOCAL_DEFLATED_FILE =
seq("localdeflatedfile",
localFileBody("", 8, TRUE, TRUE, TRUE),
def("compresseddata", last(ref("compressedsize")), eqNum(crc32(inflate(SELF)), last(ref("crc32")))));
def(COMPRESSED_DATA, last(ref(COMPRESSED_SIZE)), eqNum(crc32(inflate(SELF)), last(ref(CRC_32)))));

private static final Token LOCAL_EMPTY_FILE =
localFileBody("localemptyfile", 0, eqNum(con(0)), eqNum(con(0)), eqNum(con(0)));

private static final Token LOCAL_STORED_FILE =
seq("localstoredfile",
localFileBody("", 0, TRUE, TRUE, eq(last(ref("compressedsize")))),
def("compresseddata", last(ref("compressedsize")), eqNum(crc32(SELF), last(ref("crc32")))));
localFileBody("", 0, TRUE, TRUE, eq(last(ref(COMPRESSED_SIZE)))),
def(COMPRESSED_DATA, last(ref(COMPRESSED_SIZE)), eqNum(crc32(SELF), last(ref(CRC_32)))));

private static final Token FILES =
rep("files",
Expand All @@ -87,25 +102,25 @@ private static Token localFileBody(final String name, final int cm, final Expres

private static final Token DIR_ENTRY =
seq("direntry",
def("dirsignature", con(4), eq(con(0x50, 0x4b, 0x01, 0x02))),
def(DIR_SIGNATURE, con(4), eq(con(0x50, 0x4b, 0x01, 0x02))),
def("makeversion", con(2)),
def("extractversion", con(2)),
def("bitflag", con(2)),
def("compressionmethod", con(2)),
def("lastmodtime", con(2)),
def("lastmoddate", con(2)),
def("crc32", con(4)),
def("compressedsize", con(4)),
def("uncompressedsize", con(4)),
def("filenamesize", con(2)),
def("extrafieldsize", con(2)),
def(EXTRACT_VERSION, con(2)),
def(BIT_FLAG, con(2)),
def(COMPRESSION_METHOD, con(2)),
def(LAST_MOD_TIME, con(2)),
def(LAST_MOD_DATE, con(2)),
def(CRC_32, con(4)),
def(COMPRESSED_SIZE, con(4)),
def(UNCOMPRESSED_SIZE, con(4)),
def(FILE_NAME_SIZE, con(2)),
def(EXTRA_FIELD_SIZE, con(2)),
def("filecommentsize", con(2)),
def("filedisk", con(2), eqNum(con(0))),
def("intfileattr", con(2)),
def("extfileattr", con(4)),
def("offset", con(4)),
def("filename", last(ref("filenamesize"))),
def("extrafield", last(ref("extrafieldsize"))),
def(FILE_NAME, last(ref(FILE_NAME_SIZE))),
def(EXTRA_FIELD, last(ref(EXTRA_FIELD_SIZE))),
def("filecomment", last(ref("filecommentsize"))));

private static final Token DIRS =
Expand All @@ -117,10 +132,10 @@ private static Token localFileBody(final String name, final int cm, final Expres
def("endofdirsignature", con(4), eq(con(0x50, 0x4b, 0x05, 0x06))),
def("disknumber", con(2), eqNum(con(0))),
def("dirdisk", con(2), eqNum(con(0))),
def("numlocaldirs", con(2), eqNum(count(ref("dirsignature")))),
def("numlocaldirs", con(2), eqNum(count(ref(DIR_SIGNATURE)))),
def("numtotaldirs", con(2), eqNum(last(ref("numlocaldirs")))),
def("dirsize", con(4), eqNum(sub(offset(last(ref("endofdirsignature"))), offset(first(ref("dirsignature")))))),
def("diroffset", con(4), eqNum(offset(first(ref("dirsignature"))))),
def("dirsize", con(4), eqNum(sub(offset(last(ref("endofdirsignature"))), offset(first(ref(DIR_SIGNATURE)))))),
def("diroffset", con(4), eqNum(offset(first(ref(DIR_SIGNATURE))))),
def("commentsize", con(2)),
def("comment", last(ref("commentsize"))));

Expand Down