Skip to content

Commit

Permalink
Use String#isEmpty(), remove redundancies, and reduce verbose code (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Feb 15, 2024
1 parent 8fef53d commit 5a22ee4
Show file tree
Hide file tree
Showing 48 changed files with 209 additions and 210 deletions.
28 changes: 14 additions & 14 deletions src/main/java/com/fasterxml/jackson/core/Base64Variant.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public Base64Variant(String name, String base64Alphabet, boolean writePadding, c

// Plus if we use padding, add that in too
if (writePadding) {
_asciiToBase64[(int) paddingChar] = BASE64_VALUE_PADDING;
_asciiToBase64[paddingChar] = BASE64_VALUE_PADDING;
}

// By default, require padding on input if written on output; do not
Expand Down Expand Up @@ -342,7 +342,7 @@ public boolean acceptsPaddingOnRead() {
}

public boolean usesPaddingChar(char c) { return c == _paddingChar; }
public boolean usesPaddingChar(int ch) { return ch == (int) _paddingChar; }
public boolean usesPaddingChar(int ch) { return ch == _paddingChar; }

/**
* @return Indicator on how this Base64 encoding will handle possible padding
Expand Down Expand Up @@ -370,7 +370,7 @@ public boolean acceptsPaddingOnRead() {
*/
public int decodeBase64Char(char c)
{
int ch = (int) c;
int ch = c;
return (ch <= 127) ? _asciiToBase64[ch] : BASE64_VALUE_INVALID;
}

Expand All @@ -381,7 +381,7 @@ public int decodeBase64Char(int ch)

public int decodeBase64Byte(byte b)
{
int ch = (int) b;
int ch = b;
// note: cast retains sign, so it's from -128 to +127
if (ch < 0) {
return BASE64_VALUE_INVALID;
Expand Down Expand Up @@ -578,9 +578,9 @@ public String encode(byte[] input, boolean addQuotes)

while (inputPtr <= safeInputEnd) {
// First, mash 3 bytes into lsb of 32-bit int
int b24 = ((int) input[inputPtr++]) << 8;
b24 |= ((int) input[inputPtr++]) & 0xFF;
b24 = (b24 << 8) | (((int) input[inputPtr++]) & 0xFF);
int b24 = (input[inputPtr++]) << 8;
b24 |= (input[inputPtr++]) & 0xFF;
b24 = (b24 << 8) | ((input[inputPtr++]) & 0xFF);
encodeBase64Chunk(sb, b24);
if (--chunksBeforeLF <= 0) {
// note: must quote in JSON value, so not really useful...
Expand All @@ -593,9 +593,9 @@ public String encode(byte[] input, boolean addQuotes)
// And then we may have 1 or 2 leftover bytes to encode
int inputLeft = inputEnd - inputPtr; // 0, 1 or 2
if (inputLeft > 0) { // yes, but do we have room for output?
int b24 = ((int) input[inputPtr++]) << 16;
int b24 = (input[inputPtr++]) << 16;
if (inputLeft == 2) {
b24 |= (((int) input[inputPtr++]) & 0xFF) << 8;
b24 |= ((input[inputPtr++]) & 0xFF) << 8;
}
encodeBase64Partial(sb, b24, inputLeft);
}
Expand Down Expand Up @@ -633,9 +633,9 @@ public String encode(byte[] input, boolean addQuotes, String linefeed)
int safeInputEnd = inputEnd-3;

while (inputPtr <= safeInputEnd) {
int b24 = ((int) input[inputPtr++]) << 8;
b24 |= ((int) input[inputPtr++]) & 0xFF;
b24 = (b24 << 8) | (((int) input[inputPtr++]) & 0xFF);
int b24 = (input[inputPtr++]) << 8;
b24 |= (input[inputPtr++]) & 0xFF;
b24 = (b24 << 8) | ((input[inputPtr++]) & 0xFF);
encodeBase64Chunk(sb, b24);
if (--chunksBeforeLF <= 0) {
sb.append(linefeed);
Expand All @@ -644,9 +644,9 @@ public String encode(byte[] input, boolean addQuotes, String linefeed)
}
int inputLeft = inputEnd - inputPtr;
if (inputLeft > 0) {
int b24 = ((int) input[inputPtr++]) << 16;
int b24 = (input[inputPtr++]) << 16;
if (inputLeft == 2) {
b24 |= (((int) input[inputPtr++]) & 0xFF) << 8;
b24 |= ((input[inputPtr++]) & 0xFF) << 8;
}
encodeBase64Partial(sb, b24, inputLeft);
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/fasterxml/jackson/core/JsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
@SuppressWarnings("resource")
public class JsonFactory
extends TokenStreamFactory
implements Versioned,
java.io.Serializable // since 2.1 (for Android, mostly)
implements java.io.Serializable
{
private static final long serialVersionUID = 2;

Expand Down Expand Up @@ -545,7 +544,7 @@ protected static <T> List<T> _copy(List<T> src) {
if (src == null) {
return src;
}
return new ArrayList<T>(src);
return new ArrayList<>(src);
}

/*
Expand Down Expand Up @@ -899,7 +898,7 @@ public JsonFactory setStreamReadConstraints(StreamReadConstraints src) {
* @since 2.16
*/
public JsonFactory setErrorReportConfiguration(ErrorReportConfiguration src) {
_errorReportConfiguration = Objects.requireNonNull(src, "Cannot pass null ErrorReportConfiguration");;
_errorReportConfiguration = Objects.requireNonNull(src, "Cannot pass null ErrorReportConfiguration");
return this;
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/fasterxml/jackson/core/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
public abstract class JsonParser
implements Closeable, Versioned
{
private final static int MIN_BYTE_I = (int) Byte.MIN_VALUE;
private final static int MIN_BYTE_I = Byte.MIN_VALUE;
// as per [JACKSON-804], allow range up to and including 255
private final static int MAX_BYTE_I = (int) 255;
private final static int MAX_BYTE_I = 255;

private final static int MIN_SHORT_I = (int) Short.MIN_VALUE;
private final static int MAX_SHORT_I = (int) Short.MAX_VALUE;
private final static int MIN_SHORT_I = Short.MIN_VALUE;
private final static int MAX_SHORT_I = Short.MAX_VALUE;

/**
* Enumeration of possible "native" (optimal) types that can be
Expand Down Expand Up @@ -1279,7 +1279,7 @@ public Boolean nextBooleanValue() throws IOException {
* @since 2.8
*/
public void finishToken() throws IOException {
; // nothing
// nothing to do
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/fasterxml/jackson/core/TSFBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected static <T> List<T> _copy(List<T> src) {
if (src == null) {
return src;
}
return new ArrayList<T>(src);
return new ArrayList<>(src);
}

// // // Accessors
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/fasterxml/jackson/core/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Version(int major, int minor, int patchLevel, String snapshotInfo,
public boolean isUnknownVersion() { return (this == UNKNOWN_VERSION); }

public boolean isSnapshot() {
return (_snapshotInfo != null) && (_snapshotInfo.length() > 0);
return (_snapshotInfo != null) && !_snapshotInfo.isEmpty();
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/fasterxml/jackson/core/base/ParserBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ protected void convertNumberToInt() throws IOException
if ((_numTypesValid & NR_LONG) != 0) {
// Let's verify its lossless conversion by simple roundtrip
int result = (int) _numberLong;
if (((long) result) != _numberLong) {
if (result != _numberLong) {
reportOverflowInt(getText(), currentToken());
}
_numberInt = result;
Expand Down Expand Up @@ -1089,7 +1089,7 @@ protected void convertNumberToInt() throws IOException
protected void convertNumberToLong() throws IOException
{
if ((_numTypesValid & NR_INT) != 0) {
_numberLong = (long) _numberInt;
_numberLong = _numberInt;
} else if ((_numTypesValid & NR_BIGINT) != 0) {
final BigInteger bigInteger = _getBigInteger();
if (BI_MIN_LONG.compareTo(bigInteger) > 0
Expand Down Expand Up @@ -1159,14 +1159,14 @@ protected void convertNumberToDouble() throws IOException
_numberDouble = _getBigInteger().doubleValue();
}
} else if ((_numTypesValid & NR_LONG) != 0) {
_numberDouble = (double) _numberLong;
_numberDouble = _numberLong;
} else if ((_numTypesValid & NR_INT) != 0) {
_numberDouble = (double) _numberInt;
_numberDouble = _numberInt;
} else if ((_numTypesValid & NR_FLOAT) != 0) {
if (_numberString != null) {
_numberDouble = _getNumberDouble();
} else {
_numberDouble = (double) _getNumberFloat();
_numberDouble = _getNumberFloat();
}
} else {
_throwInternal();
Expand Down Expand Up @@ -1195,9 +1195,9 @@ protected void convertNumberToFloat() throws IOException
_numberFloat = _getBigInteger().floatValue();
}
} else if ((_numTypesValid & NR_LONG) != 0) {
_numberFloat = (float) _numberLong;
_numberFloat = _numberLong;
} else if ((_numTypesValid & NR_INT) != 0) {
_numberFloat = (float) _numberInt;
_numberFloat = _numberInt;
} else if ((_numTypesValid & NR_DOUBLE) != 0) {
if (_numberString != null) {
_numberFloat = _getNumberFloat();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,16 @@ public abstract class ParserMinimalBase extends JsonParser
protected final static BigDecimal BD_MIN_INT = new BigDecimal(BI_MIN_INT);
protected final static BigDecimal BD_MAX_INT = new BigDecimal(BI_MAX_INT);

protected final static long MIN_INT_L = (long) Integer.MIN_VALUE;
protected final static long MAX_INT_L = (long) Integer.MAX_VALUE;
protected final static long MIN_INT_L = Integer.MIN_VALUE;
protected final static long MAX_INT_L = Integer.MAX_VALUE;

// These are not very accurate, but have to do... (for bounds checks)

protected final static double MIN_LONG_D = (double) Long.MIN_VALUE;
protected final static double MAX_LONG_D = (double) Long.MAX_VALUE;
protected final static double MIN_LONG_D = Long.MIN_VALUE;
protected final static double MAX_LONG_D = Long.MAX_VALUE;

protected final static double MIN_INT_D = (double) Integer.MIN_VALUE;
protected final static double MAX_INT_D = (double) Integer.MAX_VALUE;
protected final static double MIN_INT_D = Integer.MIN_VALUE;
protected final static double MAX_INT_D = Integer.MAX_VALUE;

/*
/**********************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public char[] quoteAsString(String input)
int length = (escCode < 0)
? _appendNumeric(d, qbuf)
: _appendNamed(escCode, qbuf);
;

if ((outPtr + length) > outputBuffer.length) {
int first = outputBuffer.length - outPtr;
if (first > 0) {
Expand Down Expand Up @@ -216,7 +216,7 @@ public char[] quoteAsString(CharSequence input)
int length = (escCode < 0)
? _appendNumeric(d, qbuf)
: _appendNamed(escCode, qbuf);
;

if ((outPtr + length) > outputBuffer.length) {
int first = outputBuffer.length - outPtr;
if (first > 0) {
Expand Down Expand Up @@ -344,7 +344,7 @@ public byte[] quoteAsUTF8(String text)
outputPtr = 0;
}
// Ok, so what did we hit?
int ch = (int) text.charAt(inputPtr++);
int ch = text.charAt(inputPtr++);
if (ch <= 0x7F) { // needs quoting
int escape = escCodes[ch];
// ctrl-char, 6-byte escape...
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/fasterxml/jackson/core/io/NumberInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static long parseLong(char[] ch, int off, int len)
// Note: caller must ensure length is [10, 18]
int len1 = len-9;
long val = parseInt(ch, off, len1) * L_BILLION;
return val + (long) parseInt(ch, off+len1, 9);
return val + parseInt(ch, off+len1, 9);
}

/**
Expand Down Expand Up @@ -200,7 +200,7 @@ public static long parseLong(String s)
// that is, if we know they must be ints, call int parsing
int length = s.length();
if (length <= 9) {
return (long) parseInt(s);
return parseInt(s);
}
// !!! TODO: implement efficient 2-int parsing...
return Long.parseLong(s);
Expand Down Expand Up @@ -367,8 +367,7 @@ public static double parseAsDouble(String s, final double def, final boolean use
{
if (s == null) { return def; }
s = s.trim();
int len = s.length();
if (len == 0) {
if (s.isEmpty()) {
return def;
}
try {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/fasterxml/jackson/core/io/NumberOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public final class NumberOutput
private static int BILLION = 1000000000;
private static long BILLION_L = 1000000000L;

private static long MIN_INT_AS_LONG = (long) Integer.MIN_VALUE;
private static long MAX_INT_AS_LONG = (long) Integer.MAX_VALUE;
private static long MIN_INT_AS_LONG = Integer.MIN_VALUE;
private static long MAX_INT_AS_LONG = Integer.MAX_VALUE;

final static String SMALLEST_INT = String.valueOf(Integer.MIN_VALUE);
final static String SMALLEST_LONG = String.valueOf(Long.MIN_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ private static int skipSpace(InputAccessor acc) throws IOException
private static int skipSpace(InputAccessor acc, byte b) throws IOException
{
while (true) {
int ch = (int) b & 0xFF;
int ch = b & 0xFF;
if (!(ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t')) {
return ch;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public boolean isDup(String name) throws JsonParseException
return true;
}
if (_seen == null) {
_seen = new HashSet<String>(16); // 16 is default, seems reasonable
_seen = new HashSet<>(16); // 16 is default, seems reasonable
_seen.add(_firstName);
_seen.add(_secondName);
}
Expand Down
Loading

0 comments on commit 5a22ee4

Please sign in to comment.