use checkPositive/checkPositiveOrZero (#8835)

Motivation:

We can replace some "hand-rolled" integer checks with our own static utility method to simplify the code.

Modifications:

Use methods provided by `ObjectUtil`.

Result:

Cleaner code and less duplication
This commit is contained in:
田欧 2019-02-04 22:55:07 +08:00 committed by Norman Maurer
parent b72fea340b
commit 4c64c98f34
24 changed files with 74 additions and 120 deletions

View File

@ -21,6 +21,7 @@ import io.netty.util.internal.UnstableApi;
import java.net.IDN;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
/**
* A skeletal implementation of {@link DnsRecord}.
@ -62,9 +63,7 @@ public abstract class AbstractDnsRecord implements DnsRecord {
* @param timeToLive the TTL value of the record
*/
protected AbstractDnsRecord(String name, DnsRecordType type, int dnsClass, long timeToLive) {
if (timeToLive < 0) {
throw new IllegalArgumentException("timeToLive: " + timeToLive + " (expected: >= 0)");
}
checkPositiveOrZero(timeToLive, "timeToLive");
// Convert to ASCII which will also check that the length is not too big.
// See:
// - https://github.com/netty/netty/issues/4937

View File

@ -372,8 +372,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
default:
// Check to see if the character is not an ASCII character, or invalid
if (value < 0) {
throw new IllegalArgumentException("a header name cannot contain non-ASCII character: " +
value);
throw new IllegalArgumentException("a header name cannot contain non-ASCII character: " + value);
}
}
}

View File

@ -15,6 +15,8 @@
*/
package io.netty.handler.codec.http;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
@ -168,21 +170,10 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
protected HttpObjectDecoder(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize,
boolean chunkedSupported, boolean validateHeaders, int initialBufferSize) {
if (maxInitialLineLength <= 0) {
throw new IllegalArgumentException(
"maxInitialLineLength must be a positive integer: " +
maxInitialLineLength);
}
if (maxHeaderSize <= 0) {
throw new IllegalArgumentException(
"maxHeaderSize must be a positive integer: " +
maxHeaderSize);
}
if (maxChunkSize <= 0) {
throw new IllegalArgumentException(
"maxChunkSize must be a positive integer: " +
maxChunkSize);
}
checkPositive(maxInitialLineLength, "maxInitialLineLength");
checkPositive(maxHeaderSize, "maxHeaderSize");
checkPositive(maxChunkSize, "maxChunkSize");
AppendableCharSequence seq = new AppendableCharSequence(initialBufferSize);
lineParser = new LineParser(seq, maxInitialLineLength);
headerParser = new HeaderParser(seq, maxHeaderSize);

View File

@ -22,6 +22,7 @@ import io.netty.util.CharsetUtil;
import static io.netty.handler.codec.http.HttpConstants.SP;
import static io.netty.util.ByteProcessor.FIND_ASCII_SPACE;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Integer.parseInt;
/**
@ -538,10 +539,7 @@ public class HttpResponseStatus implements Comparable<HttpResponseStatus> {
}
private HttpResponseStatus(int code, String reasonPhrase, boolean bytes) {
if (code < 0) {
throw new IllegalArgumentException(
"code: " + code + " (expected: 0+)");
}
checkPositiveOrZero(code, "code");
if (reasonPhrase == null) {
throw new NullPointerException("reasonPhrase");

View File

@ -15,6 +15,8 @@
*/
package io.netty.handler.codec.http;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
@ -165,12 +167,8 @@ public class HttpVersion implements Comparable<HttpVersion> {
}
}
if (majorVersion < 0) {
throw new IllegalArgumentException("negative majorVersion");
}
if (minorVersion < 0) {
throw new IllegalArgumentException("negative minorVersion");
}
checkPositiveOrZero(majorVersion, "majorVersion");
checkPositiveOrZero(minorVersion, "minorVersion");
this.protocolName = protocolName;
this.majorVersion = majorVersion;

View File

@ -128,8 +128,7 @@ public abstract class AbstractMemoryHttpData extends AbstractHttpData {
}
long newsize = file.length();
if (newsize > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"File too big to be loaded in memory");
throw new IllegalArgumentException("File too big to be loaded in memory");
}
checkSize(newsize);
FileInputStream inputStream = new FileInputStream(file);

View File

@ -15,6 +15,8 @@
*/
package io.netty.handler.codec.spdy;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.internal.StringUtil;
/**
@ -62,10 +64,7 @@ public class DefaultSpdyGoAwayFrame implements SpdyGoAwayFrame {
@Override
public SpdyGoAwayFrame setLastGoodStreamId(int lastGoodStreamId) {
if (lastGoodStreamId < 0) {
throw new IllegalArgumentException("Last-good-stream-ID"
+ " cannot be negative: " + lastGoodStreamId);
}
checkPositiveOrZero(lastGoodStreamId, "lastGoodStreamId");
this.lastGoodStreamId = lastGoodStreamId;
return this;
}

View File

@ -15,6 +15,8 @@
*/
package io.netty.handler.codec.spdy;
import static io.netty.util.internal.ObjectUtil.checkPositive;
/**
* The default {@link SpdyStreamFrame} implementation.
*/
@ -39,10 +41,7 @@ public abstract class DefaultSpdyStreamFrame implements SpdyStreamFrame {
@Override
public SpdyStreamFrame setStreamId(int streamId) {
if (streamId <= 0) {
throw new IllegalArgumentException(
"Stream-ID must be positive: " + streamId);
}
checkPositive(streamId, "streamId");
this.streamId = streamId;
return this;
}

View File

@ -20,8 +20,7 @@ import io.netty.util.internal.StringUtil;
/**
* The default {@link SpdySynReplyFrame} implementation.
*/
public class DefaultSpdySynReplyFrame extends DefaultSpdyHeadersFrame
implements SpdySynReplyFrame {
public class DefaultSpdySynReplyFrame extends DefaultSpdyHeadersFrame implements SpdySynReplyFrame {
/**
* Creates a new instance.

View File

@ -15,6 +15,8 @@
*/
package io.netty.handler.codec.spdy;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.internal.StringUtil;
/**
@ -77,11 +79,7 @@ public class DefaultSpdySynStreamFrame extends DefaultSpdyHeadersFrame
@Override
public SpdySynStreamFrame setAssociatedStreamId(int associatedStreamId) {
if (associatedStreamId < 0) {
throw new IllegalArgumentException(
"Associated-To-Stream-ID cannot be negative: " +
associatedStreamId);
}
checkPositiveOrZero(associatedStreamId, "associatedStreamId");
this.associatedStreamId = associatedStreamId;
return this;
}

View File

@ -15,6 +15,9 @@
*/
package io.netty.handler.codec.spdy;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.internal.StringUtil;
/**
@ -43,10 +46,7 @@ public class DefaultSpdyWindowUpdateFrame implements SpdyWindowUpdateFrame {
@Override
public SpdyWindowUpdateFrame setStreamId(int streamId) {
if (streamId < 0) {
throw new IllegalArgumentException(
"Stream-ID cannot be negative: " + streamId);
}
checkPositiveOrZero(streamId, "streamId");
this.streamId = streamId;
return this;
}
@ -58,11 +58,7 @@ public class DefaultSpdyWindowUpdateFrame implements SpdyWindowUpdateFrame {
@Override
public SpdyWindowUpdateFrame setDeltaWindowSize(int deltaWindowSize) {
if (deltaWindowSize <= 0) {
throw new IllegalArgumentException(
"Delta-Window-Size must be positive: " +
deltaWindowSize);
}
checkPositive(deltaWindowSize, "deltaWindowSize");
this.deltaWindowSize = deltaWindowSize;
return this;
}

View File

@ -38,6 +38,8 @@ import static io.netty.handler.codec.spdy.SpdyCodecUtil.getSignedInt;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.getUnsignedInt;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.getUnsignedMedium;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.getUnsignedShort;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
@ -95,10 +97,7 @@ public class SpdyFrameDecoder {
if (delegate == null) {
throw new NullPointerException("delegate");
}
if (maxChunkSize <= 0) {
throw new IllegalArgumentException(
"maxChunkSize must be a positive integer: " + maxChunkSize);
}
checkPositive(maxChunkSize, "maxChunkSize");
this.spdyVersion = spdyVersion.getVersion();
this.delegate = delegate;
this.maxChunkSize = maxChunkSize;

View File

@ -38,6 +38,7 @@ import java.util.List;
import java.util.Map;
import static io.netty.handler.codec.spdy.SpdyHeaders.HttpNames.*;
import static io.netty.util.internal.ObjectUtil.checkPositive;
/**
* Decodes {@link SpdySynStreamFrame}s, {@link SpdySynReplyFrame}s,
@ -103,10 +104,7 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder<SpdyFrame> {
if (version == null) {
throw new NullPointerException("version");
}
if (maxContentLength <= 0) {
throw new IllegalArgumentException(
"maxContentLength must be a positive integer: " + maxContentLength);
}
checkPositive(maxContentLength, "maxContentLength");
spdyVersion = version.getVersion();
this.maxContentLength = maxContentLength;
this.messageMap = messageMap;

View File

@ -26,6 +26,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.SPDY_SESSION_STREAM_ID;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.isServerId;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
/**
* Manages streams within a SPDY session.
@ -77,9 +78,7 @@ public class SpdySessionHandler extends ChannelDuplexHandler {
}
public void setSessionReceiveWindowSize(int sessionReceiveWindowSize) {
if (sessionReceiveWindowSize < 0) {
throw new IllegalArgumentException("sessionReceiveWindowSize");
}
checkPositiveOrZero(sessionReceiveWindowSize, "sessionReceiveWindowSize");
// This will not send a window update frame immediately.
// If this value increases the allowed receive window size,
// a WINDOW_UPDATE frame will be sent when only half of the

View File

@ -30,6 +30,7 @@ import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_PRIORITY_WEIGH
import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR;
import static io.netty.handler.codec.http2.Http2Exception.connectionError;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Math.min;
@ -543,9 +544,7 @@ public class DefaultHttp2ConnectionEncoder implements Http2ConnectionEncoder {
FlowControlledBase(final Http2Stream stream, int padding, boolean endOfStream,
final ChannelPromise promise) {
if (padding < 0) {
throw new IllegalArgumentException("padding must be >= 0");
}
checkPositiveOrZero(padding, "padding");
this.padding = padding;
this.endOfStream = endOfStream;
this.stream = stream;

View File

@ -61,6 +61,8 @@ import static io.netty.handler.codec.http2.Http2FrameTypes.RST_STREAM;
import static io.netty.handler.codec.http2.Http2FrameTypes.SETTINGS;
import static io.netty.handler.codec.http2.Http2FrameTypes.WINDOW_UPDATE;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Math.max;
import static java.lang.Math.min;
@ -614,15 +616,11 @@ public class DefaultHttp2FrameWriter implements Http2FrameWriter, Http2FrameSize
}
private static void verifyStreamId(int streamId, String argumentName) {
if (streamId <= 0) {
throw new IllegalArgumentException(argumentName + " must be > 0");
}
checkPositive(streamId, "streamId");
}
private static void verifyStreamOrConnectionId(int streamId, String argumentName) {
if (streamId < 0) {
throw new IllegalArgumentException(argumentName + " must be >= 0");
}
checkPositiveOrZero(streamId, "streamId");
}
private static void verifyWeight(short weight) {
@ -638,9 +636,7 @@ public class DefaultHttp2FrameWriter implements Http2FrameWriter, Http2FrameSize
}
private static void verifyWindowSizeIncrement(int windowSizeIncrement) {
if (windowSizeIncrement < 0) {
throw new IllegalArgumentException("WindowSizeIncrement must be >= 0");
}
checkPositiveOrZero(windowSizeIncrement, "windowSizeIncrement");
}
private static void verifyPingPayload(ByteBuf data) {

View File

@ -15,6 +15,8 @@
*/
package io.netty.handler.codec.http2;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.DefaultByteBufHolder;
import io.netty.buffer.Unpooled;
@ -98,9 +100,7 @@ public final class DefaultHttp2GoAwayFrame extends DefaultByteBufHolder implemen
@Override
public Http2GoAwayFrame setExtraStreamIds(int extraStreamIds) {
if (extraStreamIds < 0) {
throw new IllegalArgumentException("extraStreamIds must be non-negative");
}
checkPositiveOrZero(extraStreamIds, "extraStreamIds");
this.extraStreamIds = extraStreamIds;
return this;
}

View File

@ -24,6 +24,7 @@ import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR;
import static io.netty.handler.codec.http2.Http2Exception.connectionError;
import static io.netty.handler.codec.http2.Http2Exception.streamError;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Math.max;
import static java.lang.Math.min;
import io.netty.buffer.ByteBuf;
@ -173,9 +174,7 @@ public class DefaultHttp2LocalFlowController implements Http2LocalFlowController
@Override
public boolean consumeBytes(Http2Stream stream, int numBytes) throws Http2Exception {
assert ctx != null && ctx.executor().inEventLoop();
if (numBytes < 0) {
throw new IllegalArgumentException("numBytes must not be negative");
}
checkPositiveOrZero(numBytes, "numBytes");
if (numBytes == 0) {
return false;
}

View File

@ -31,6 +31,7 @@ import static io.netty.handler.codec.http2.Http2Error.STREAM_CLOSED;
import static io.netty.handler.codec.http2.Http2Exception.streamError;
import static io.netty.handler.codec.http2.Http2Stream.State.HALF_CLOSED_LOCAL;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Math.max;
import static java.lang.Math.min;
@ -635,9 +636,7 @@ public class DefaultHttp2RemoteFlowController implements Http2RemoteFlowControll
}
void initialWindowSize(int newWindowSize) throws Http2Exception {
if (newWindowSize < 0) {
throw new IllegalArgumentException("Invalid initial window size: " + newWindowSize);
}
checkPositiveOrZero(newWindowSize, "newWindowSize");
final int delta = newWindowSize - initialWindowSize;
initialWindowSize = newWindowSize;

View File

@ -33,6 +33,7 @@ import static io.netty.handler.codec.http.HttpHeaderValues.X_GZIP;
import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR;
import static io.netty.handler.codec.http2.Http2Exception.streamError;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
/**
* A HTTP2 frame listener that will decompress data frames according to the {@code content-encoding} header for each
@ -398,9 +399,7 @@ public class DelegatingDecompressorFrameListener extends Http2FrameListenerDecor
* @return The number of pre-decompressed bytes that have been consumed.
*/
int consumeBytes(int streamId, int decompressedBytes) throws Http2Exception {
if (decompressedBytes < 0) {
throw new IllegalArgumentException("decompressedBytes must not be negative: " + decompressedBytes);
}
checkPositiveOrZero(decompressedBytes, "decompressedBytes");
if (decompressed - decompressedBytes < 0) {
throw streamError(streamId, INTERNAL_ERROR,
"Attempting to return too many bytes for stream %d. decompressed: %d " +

View File

@ -24,6 +24,7 @@ import static io.netty.handler.codec.http2.Http2CodecUtil.streamableBytes;
import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR;
import static io.netty.handler.codec.http2.Http2Exception.connectionError;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import static java.lang.Math.max;
import static java.lang.Math.min;
@ -72,9 +73,7 @@ public final class UniformStreamByteDistributor implements StreamByteDistributor
* Must be > 0.
*/
public void minAllocationChunk(int minAllocationChunk) {
if (minAllocationChunk <= 0) {
throw new IllegalArgumentException("minAllocationChunk must be > 0");
}
checkPositive(minAllocationChunk, "minAllocationChunk");
this.minAllocationChunk = minAllocationChunk;
}

View File

@ -37,6 +37,8 @@ import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_PRIORITY_WEIGH
import static io.netty.handler.codec.http2.Http2CodecUtil.streamableBytes;
import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR;
import static io.netty.handler.codec.http2.Http2Exception.connectionError;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Math.max;
import static java.lang.Math.min;
@ -96,9 +98,8 @@ public final class WeightedFairQueueByteDistributor implements StreamByteDistrib
}
public WeightedFairQueueByteDistributor(Http2Connection connection, int maxStateOnlySize) {
if (maxStateOnlySize < 0) {
throw new IllegalArgumentException("maxStateOnlySize: " + maxStateOnlySize + " (expected: >0)");
} else if (maxStateOnlySize == 0) {
checkPositiveOrZero(maxStateOnlySize, "maxStateOnlySize");
if (maxStateOnlySize == 0) {
stateOnlyMap = IntCollections.emptyMap();
stateOnlyRemovalQueue = EmptyPriorityQueue.instance();
} else {
@ -281,9 +282,7 @@ public final class WeightedFairQueueByteDistributor implements StreamByteDistrib
* @param allocationQuantum the amount of bytes that will be allocated to each stream. Must be &gt; 0.
*/
public void allocationQuantum(int allocationQuantum) {
if (allocationQuantum <= 0) {
throw new IllegalArgumentException("allocationQuantum must be > 0");
}
checkPositive(allocationQuantum, "allocationQuantum");
this.allocationQuantum = allocationQuantum;
}

View File

@ -15,6 +15,8 @@
*/
package io.netty.handler.codec.memcache.binary;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
@ -59,9 +61,7 @@ public abstract class AbstractBinaryMemcacheDecoder<M extends BinaryMemcacheMess
* @param chunkSize the maximum chunk size of the payload.
*/
protected AbstractBinaryMemcacheDecoder(int chunkSize) {
if (chunkSize < 0) {
throw new IllegalArgumentException("chunkSize must be a positive integer: " + chunkSize);
}
checkPositiveOrZero(chunkSize, "chunkSize");
this.chunkSize = chunkSize;
}

View File

@ -30,6 +30,7 @@ import io.netty.util.internal.AppendableCharSequence;
import static io.netty.buffer.ByteBufUtil.indexOf;
import static io.netty.buffer.ByteBufUtil.readBytes;
import static io.netty.util.internal.ObjectUtil.checkPositive;
/**
* Decodes {@link ByteBuf}s into {@link StompHeadersSubframe}s and
@ -90,16 +91,8 @@ public class StompSubframeDecoder extends ReplayingDecoder<State> {
public StompSubframeDecoder(int maxLineLength, int maxChunkSize, boolean validateHeaders) {
super(State.SKIP_CONTROL_CHARACTERS);
if (maxLineLength <= 0) {
throw new IllegalArgumentException(
"maxLineLength must be a positive integer: " +
maxLineLength);
}
if (maxChunkSize <= 0) {
throw new IllegalArgumentException(
"maxChunkSize must be a positive integer: " +
maxChunkSize);
}
checkPositive(maxLineLength, "maxLineLength");
checkPositive(maxChunkSize, "maxChunkSize");
this.maxChunkSize = maxChunkSize;
this.maxLineLength = maxLineLength;
this.validateHeaders = validateHeaders;