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 116f72db8d
commit db79983874
24 changed files with 71 additions and 115 deletions

View File

@ -20,6 +20,7 @@ import io.netty.util.internal.UnstableApi;
import java.net.IDN;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.util.Objects.requireNonNull;
/**
@ -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

@ -366,8 +366,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;
@ -167,16 +169,8 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
protected HttpObjectDecoder(
int maxInitialLineLength, int maxHeaderSize,
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);
}
checkPositive(maxInitialLineLength, "maxInitialLineLength");
checkPositive(maxHeaderSize, "maxHeaderSize");
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;
import static java.util.Objects.requireNonNull;
@ -539,10 +540,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");
requireNonNull(reasonPhrase, "reasonPhrase");

View File

@ -15,6 +15,7 @@
*/
package io.netty.handler.codec.http;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.util.Objects.requireNonNull;
import io.netty.buffer.ByteBuf;
@ -161,12 +162,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

@ -121,8 +121,7 @@ public abstract class AbstractMemoryHttpData extends AbstractHttpData {
requireNonNull(file, "file");
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,16 +78,14 @@ public class SpdySessionHandler extends ChannelDuplexHandler {
}
public void setSessionReceiveWindowSize(int sessionReceiveWindowSize) {
if (sessionReceiveWindowSize < 0) {
throw new IllegalArgumentException("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
// session window size remains during data frame processing.
// If this value decreases the allowed receive window size,
// the window will be reduced as data frames are processed.
initialSessionReceiveWindowSize = 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
// session window size remains during data frame processing.
// If this value decreases the allowed receive window size,
// the window will be reduced as data frames are processed.
initialSessionReceiveWindowSize = sessionReceiveWindowSize;
}
@Override

View File

@ -29,6 +29,7 @@ import static io.netty.handler.codec.http.HttpStatusClass.INFORMATIONAL;
import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_PRIORITY_WEIGHT;
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.checkPositiveOrZero;
import static java.lang.Integer.MAX_VALUE;
import static java.lang.Math.min;
import static java.util.Objects.requireNonNull;
@ -540,9 +541,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

@ -60,6 +60,8 @@ import static io.netty.handler.codec.http2.Http2FrameTypes.PUSH_PROMISE;
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.checkPositive;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Objects.requireNonNull;
@ -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

@ -23,6 +23,7 @@ import static io.netty.handler.codec.http2.Http2Error.FLOW_CONTROL_ERROR;
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.checkPositiveOrZero;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Objects.requireNonNull;
@ -174,9 +175,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

@ -30,6 +30,7 @@ import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR;
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.checkPositiveOrZero;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Objects.requireNonNull;
@ -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

@ -32,6 +32,7 @@ import static io.netty.handler.codec.http.HttpHeaderValues.X_DEFLATE;
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.checkPositiveOrZero;
import static java.util.Objects.requireNonNull;
/**
@ -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

@ -23,6 +23,7 @@ import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_MIN_ALLOCATION
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 java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Objects.requireNonNull;
@ -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

@ -36,6 +36,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;
@ -95,9 +97,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 {
@ -280,9 +281,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;