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

Miscellaneous clean-up #43

Merged
merged 7 commits into from
Apr 18, 2020
Merged
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: 22 additions & 22 deletions modules/core/src/main/java/org/dhallj/cbor/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public abstract class Reader {

/** Only allow symbols that correspond to entire encoded Dhall expressions. */
public <R> R nextSymbol(Visitor<R> visitor) {
public final <R> R nextSymbol(Visitor<R> visitor) {
skip55799();
byte b = this.read();
switch (MajorType.fromByte(b)) {
Expand Down Expand Up @@ -43,12 +43,12 @@ public <R> R nextSymbol(Visitor<R> visitor) {

protected abstract byte[] read(int count);

public BigInteger readUnsignedInteger() {
public final BigInteger readUnsignedInteger() {
skip55799();
return readUnsignedInteger(read());
}

public BigInteger readPositiveBigNum() {
public final BigInteger readPositiveBigNum() {
skip55799();
BigInteger result = readBigNum();
if (result.compareTo(BigInteger.ZERO) < 0) {
Expand All @@ -58,7 +58,7 @@ public BigInteger readPositiveBigNum() {
}
}

public BigInteger readBigNum() {
public final BigInteger readBigNum() {
skip55799();
byte next = read();
switch (MajorType.fromByte(next)) {
Expand All @@ -85,7 +85,7 @@ public BigInteger readBigNum() {
}
}

public String readNullableTextString() {
public final String readNullableTextString() {
skip55799();
byte next = read();
switch (MajorType.fromByte(next)) {
Expand All @@ -98,7 +98,7 @@ public String readNullableTextString() {
}
}

public byte[] readNullableByteString() {
public final byte[] readNullableByteString() {
skip55799();
byte next = read();
switch (MajorType.fromByte(next)) {
Expand All @@ -118,7 +118,7 @@ public byte[] readNullableByteString() {
* CBOR representation where we don't know simply from the length of the array and the first
* element what type of expression we're decoding - could be projection or projection by type
*/
public String tryReadTextString() {
public final String tryReadTextString() {
skip55799();
byte next = peek();
switch (MajorType.fromByte(next)) {
Expand All @@ -129,7 +129,7 @@ public String tryReadTextString() {
}
}

public BigInteger readArrayStart() {
public final BigInteger readArrayStart() {
skip55799();
byte next = read();
switch (MajorType.fromByte(next)) {
Expand All @@ -146,14 +146,14 @@ public BigInteger readArrayStart() {
}
}

public <R> Map<String, R> readMap(Visitor<R> visitor) {
public final <R> Map<String, R> readMap(Visitor<R> visitor) {
skip55799();
byte b = this.read();
switch (MajorType.fromByte(b)) {
case MAP:
BigInteger length = readMapStart(b);
Map<String, R> entries = new HashMap<>();
for (int i = 0; i < length.longValue(); i++) {
int length = readMapStart(b).intValue();
Map<String, R> entries = new HashMap<>(length);
for (int i = 0; i < length; i++) {
String key = readNullableTextString();
R value = nextSymbol(visitor);
entries.put(key, value);
Expand All @@ -165,17 +165,17 @@ public <R> Map<String, R> readMap(Visitor<R> visitor) {
}
}

private BigInteger readUnsignedInteger(byte b) {
private final BigInteger readUnsignedInteger(byte b) {
AdditionalInfo info = AdditionalInfo.fromByte(b);
return readBigInteger(info, b);
}

private BigInteger readNegativeInteger(byte b) {
private final BigInteger readNegativeInteger(byte b) {
AdditionalInfo info = AdditionalInfo.fromByte(b);
return BigInteger.valueOf(-1).subtract(readBigInteger(info, b));
}

private byte[] readByteString(byte b) {
private final byte[] readByteString(byte b) {
AdditionalInfo info = AdditionalInfo.fromByte(b);
BigInteger length = readBigInteger(info, b);
if (length.compareTo(BigInteger.ZERO) < 0) {
Expand All @@ -186,7 +186,7 @@ private byte[] readByteString(byte b) {
}
}

private String readTextString(byte b) {
private final String readTextString(byte b) {
AdditionalInfo info = AdditionalInfo.fromByte(b);
BigInteger length = readBigInteger(info, b);
if (length.compareTo(BigInteger.ZERO) < 0) {
Expand All @@ -198,7 +198,7 @@ private String readTextString(byte b) {
}
}

private <R> R readArrayStart(byte b, Visitor<R> visitor) {
private final <R> R readArrayStart(byte b, Visitor<R> visitor) {
AdditionalInfo info = AdditionalInfo.fromByte(b);
BigInteger length = readBigInteger(info, b);
if (length.compareTo(BigInteger.ZERO) < 0) {
Expand All @@ -220,7 +220,7 @@ private <R> R readArrayStart(byte b, Visitor<R> visitor) {
}
}

private BigInteger readMapStart(byte b) {
private final BigInteger readMapStart(byte b) {
AdditionalInfo info = AdditionalInfo.fromByte(b);
BigInteger length = readBigInteger(info, b);
if (length.compareTo(BigInteger.ZERO) < 0) {
Expand All @@ -230,7 +230,7 @@ private BigInteger readMapStart(byte b) {
}
}

private <R> R readPrimitive(byte b, Visitor<R> visitor) {
private final <R> R readPrimitive(byte b, Visitor<R> visitor) {
int value = b & 31;
if (0 <= value && value <= 19) {
throw new CBORException(String.format("Primitive %d is unassigned", value));
Expand Down Expand Up @@ -291,7 +291,7 @@ private <R> R readPrimitive(byte b, Visitor<R> visitor) {
}
}

private void skip55799() {
private final void skip55799() {
byte next = peek();
switch (MajorType.fromByte(next)) {
case SEMANTIC_TAG:
Expand All @@ -312,7 +312,7 @@ private void skip55799() {
}
}

private BigInteger readBigInteger(AdditionalInfo info, byte first) {
private final BigInteger readBigInteger(AdditionalInfo info, byte first) {
switch (info) {
case DIRECT:
return BigInteger.valueOf(first & 31);
Expand All @@ -333,7 +333,7 @@ private BigInteger readBigInteger(AdditionalInfo info, byte first) {
}
}

private BigInteger readBigInteger(long numBytes) {
private final BigInteger readBigInteger(long numBytes) {
BigInteger result = BigInteger.ZERO;
for (long i = 0; i < numBytes; i++) {
int next = this.read() & 0xff;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.math.BigInteger;
import java.net.URI;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

/**
Expand Down
4 changes: 2 additions & 2 deletions modules/core/src/main/java/org/dhallj/core/Expr.java
Original file line number Diff line number Diff line change
Expand Up @@ -1941,9 +1941,9 @@ private static final Entry<Expr, Expr> flattenToMapRecord(List<Entry<String, Exp
Expr value = null;

for (Entry<String, Expr> entry : fields) {
if (entry.getKey().equals("mapKey")) {
if (entry.getKey().equals(Constants.MAP_KEY_FIELD_NAME)) {
key = entry.getValue();
} else if (entry.getKey().equals("mapValue")) {
} else if (entry.getKey().equals(Constants.MAP_VALUE_FIELD_NAME)) {
value = entry.getValue();
}
}
Expand Down
65 changes: 14 additions & 51 deletions modules/core/src/main/java/org/dhallj/core/ToStringVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,77 +24,41 @@ final class ToStringState {
static final int NONE = Integer.MAX_VALUE;

private final String text;
private final int looseness;
private final int level;

ToStringState(String text, int looseness) {
ToStringState(String text, int level) {
this.text = text;
this.looseness = looseness;
this.level = level;
}

ToStringState(String text) {
this(text, BASE);
}

ToStringState withText(String text) {
return new ToStringState(text, this.looseness);
return new ToStringState(text, this.level);
}

String toString(int contextLooseness, boolean parenthesizeIfSame) {
if (contextLooseness > this.looseness
|| (!parenthesizeIfSame && contextLooseness == this.looseness)) {
String toString(int contextLevel, boolean parenthesizeIfSame) {
if (contextLevel > this.level || (!parenthesizeIfSame && contextLevel == this.level)) {
return this.text;
} else {
return String.format("(%s)", this.text);
}
}

String toString(int contextLooseness) {
return this.toString(contextLooseness, false);
String toString(int contextLevel) {
return this.toString(contextLevel, false);
}

public String toString() {
return this.toString(NONE);
}

private static final int baseOperatorLooseness = 4;

static final int getOperatorLooseness(Operator operator) {
int level = baseOperatorLooseness;

switch (operator) {
case COMPLETE:
level += 1;
;
case IMPORT_ALT:
level += 1;
case OR:
level += 1;
case PLUS:
level += 1;
case TEXT_APPEND:
level += 1;
case LIST_APPEND:
level += 1;
case AND:
level += 1;
case COMBINE:
level += 1;
case PREFER:
level += 1;
case COMBINE_TYPES:
level += 1;
case TIMES:
level += 1;
case EQUALS:
level += 1;
case NOT_EQUALS:
level += 1;
case EQUIVALENT:
level += 1;
default:
}
private static final int baseOperatorLevel = 2;

return level;
static final int getOperatorLevel(Operator operator) {
return baseOperatorLevel + operator.getPrecedence();
}
}

Expand Down Expand Up @@ -363,13 +327,12 @@ public ToStringState onApplication(ToStringState base, List<ToStringState> args)

public ToStringState onOperatorApplication(
Operator operator, ToStringState lhs, ToStringState rhs) {
int operatorLooseness = ToStringState.getOperatorLooseness(operator);
int operatorLevel = ToStringState.getOperatorLevel(operator);

return new ToStringState(
String.format(
"%s %s %s",
lhs.toString(operatorLooseness), operator, rhs.toString(operatorLooseness, true)),
operatorLooseness);
"%s %s %s", lhs.toString(operatorLevel), operator, rhs.toString(operatorLevel, true)),
operatorLevel);
}

public ToStringState onIf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.dhallj.core.Expr;

public class Decode {
public static Expr decode(byte[] bytes) {
public static final Expr decode(byte[] bytes) {
Reader reader = new Reader.ByteArrayReader(bytes);
// TODO check: if identifier then must be builtin using Expr.Constants.isBuiltInConstant
Expr e = reader.nextSymbol(new CBORDecodingVisitor(reader));
Expand Down
4 changes: 2 additions & 2 deletions modules/core/src/main/java/org/dhallj/core/binary/Encode.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ private final int modeLabel(Expr.ImportMode mode) {
}
}

private static byte[] multihash(byte[] hash) {
private static final byte[] multihash(byte[] hash) {
byte[] bytes = new byte[34];
// The label for SHA-256.
bytes[0] = 18;
Expand Down Expand Up @@ -434,7 +434,7 @@ public Void onEnvImport(final String value, final Expr.ImportMode mode, final by
return null;
}

private static int pathLabel(Path path) {
private static final int pathLabel(Path path) {
if (path.isAbsolute()) {
return Label.IMPORT_TYPE_LOCAL_ABSOLUTE;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package org.dhallj.core.converters;

import java.math.BigInteger;
import java.net.URI;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.dhallj.cbor.Writer;
import org.dhallj.core.Expr;
import org.dhallj.core.Operator;
import org.dhallj.core.Source;
import org.dhallj.core.Visitor;

public final class JsonConverter extends Visitor.Constant<Boolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public Expr onProjection(Expr base, String[] fieldNames) {
}

public Expr onProjectionByType(Expr base, Expr arg) {
Iterable<Entry<String, Expr>> argAsRecordType = Expr.Util.asRecordType(arg);
Set<String> keys = new TreeSet();
List<Entry<String, Expr>> argAsRecordType = Expr.Util.asRecordType(arg);
Set<String> keys = new TreeSet<>();
for (Entry<String, Expr> entry : argAsRecordType) {
keys.add(entry.getKey());
}
Expand Down
Loading