Skip to content

Commit

Permalink
remove Preconditions
Browse files Browse the repository at this point in the history
  • Loading branch information
mostroverkhov committed May 2, 2024
1 parent a1ae3af commit 6d9aba6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.netty.handler.codec.http.websocketx.WebSocketDecoderConfig;
import io.netty.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateClientExtensionHandshaker;
import java.util.Objects;

/** Builder for {@link Http2WebSocketClientHandler} */
public final class Http2WebSocketClientBuilder {
Expand Down Expand Up @@ -45,7 +46,7 @@ public static Http2WebSocketClientBuilder create() {
* @return this {@link Http2WebSocketClientBuilder} instance
*/
public Http2WebSocketClientBuilder codec(Http1WebSocketCodec webSocketCodec) {
this.webSocketCodec = Preconditions.requireNonNull(webSocketCodec, "webSocketCodec");
this.webSocketCodec = Objects.requireNonNull(webSocketCodec, "webSocketCodec");
return this;
}

Expand All @@ -55,7 +56,7 @@ public Http2WebSocketClientBuilder codec(Http1WebSocketCodec webSocketCodec) {
*/
public Http2WebSocketClientBuilder decoderConfig(WebSocketDecoderConfig webSocketDecoderConfig) {
this.webSocketDecoderConfig =
Preconditions.requireNonNull(webSocketDecoderConfig, "webSocketDecoderConfig");
Objects.requireNonNull(webSocketDecoderConfig, "webSocketDecoderConfig");
return this;
}

Expand All @@ -74,7 +75,7 @@ public Http2WebSocketClientBuilder maskPayload(boolean maskPayload) {
*/
public Http2WebSocketClientBuilder handshakeTimeoutMillis(long handshakeTimeoutMillis) {
this.handshakeTimeoutMillis =
Preconditions.requirePositive(handshakeTimeoutMillis, "handshakeTimeoutMillis");
Http2WebSocketProtocol.requirePositive(handshakeTimeoutMillis, "handshakeTimeoutMillis");
return this;
}

Expand All @@ -87,7 +88,7 @@ public Http2WebSocketClientBuilder handshakeTimeoutMillis(long handshakeTimeoutM
public Http2WebSocketClientBuilder closedWebSocketRemoveTimeoutMillis(
long closedWebSocketRemoveTimeoutMillis) {
this.closedWebSocketRemoveTimeoutMillis =
Preconditions.requirePositive(
Http2WebSocketProtocol.requirePositive(
closedWebSocketRemoveTimeoutMillis, "closedWebSocketRemoveTimeoutMillis");
return this;
}
Expand Down Expand Up @@ -144,7 +145,7 @@ public Http2WebSocketClientBuilder compression(
* @return this {@link Http2WebSocketClientBuilder} instance
*/
public Http2WebSocketClientBuilder streamWeight(int weight) {
this.streamWeight = Preconditions.requireRange(weight, 1, 256, "streamWeight");
this.streamWeight = Http2WebSocketProtocol.requireRange(weight, 1, 256, "streamWeight");
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public final class Http2WebSocketClientHandshaker {
*/
public static Http2WebSocketClientHandshaker create(Channel channel) {
Objects.requireNonNull(channel, "channel");
return Preconditions.requireHandler(channel, Http2WebSocketClientHandler.class).handShaker();
return Http2WebSocketHandler.requireChannelHandler(channel, Http2WebSocketClientHandler.class)
.handShaker();
}

/**
Expand Down Expand Up @@ -162,10 +163,10 @@ public ChannelFuture handshake(
String subprotocol,
Http2Headers requestHeaders,
ChannelHandler webSocketHandler) {
Preconditions.requireNonEmpty(path, "path");
Preconditions.requireNonNull(subprotocol, "subprotocol");
Preconditions.requireNonNull(requestHeaders, "requestHeaders");
Preconditions.requireNonNull(webSocketHandler, "webSocketHandler");
requireNonEmpty(path, "path");
Objects.requireNonNull(subprotocol, "subprotocol");
Objects.requireNonNull(requestHeaders, "requestHeaders");
Objects.requireNonNull(webSocketHandler, "webSocketHandler");

long startNanos = System.nanoTime();
ChannelHandlerContext ctx = webSocketsParent.context();
Expand Down Expand Up @@ -469,6 +470,13 @@ private static boolean isEqual(String str, @Nullable CharSequence seq) {
return str.contentEquals(seq);
}

private static String requireNonEmpty(String string, String message) {
if (string == null || string.isEmpty()) {
throw new IllegalArgumentException(message + " must be non empty");
}
return string;
}

static class Handshake extends Http2WebSocketServerHandshaker.Handshake {
private final Http2WebSocketChannel webSocketChannel;
private final Http2Headers requestHeaders;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public static final class Http2WebSocketStreamWeightUpdateEvent extends Http2Web

Http2WebSocketStreamWeightUpdateEvent(short streamWeight) {
super(Type.WEIGHT_UPDATE);
this.streamWeight = Preconditions.requireRange(streamWeight, 1, 256, "streamWeight");
this.streamWeight = Http2WebSocketProtocol.requireRange(streamWeight, 1, 256, "streamWeight");
}

public short streamWeight() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package com.jauntsdn.netty.handler.codec.http2.websocketx;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http2.Http2ConnectionDecoder;
import io.netty.handler.codec.http2.Http2ConnectionHandler;
Expand All @@ -44,8 +46,7 @@ public abstract class Http2WebSocketHandler extends ChannelDuplexHandler
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
Http2ConnectionHandler http2Handler =
this.http2Handler =
Preconditions.requireHandler(ctx.channel(), Http2ConnectionHandler.class);
this.http2Handler = requireChannelHandler(ctx.channel(), Http2ConnectionHandler.class);
HandlerListener listener = handlerListener;
if (listener == null) {
Http2ConnectionDecoder decoder = http2Handler.decoder();
Expand Down Expand Up @@ -183,6 +184,15 @@ static AsciiString endOfStreamValue(boolean endOfStream) {
: HEADER_WEBSOCKET_ENDOFSTREAM_VALUE_FALSE;
}

static <T extends ChannelHandler> T requireChannelHandler(Channel channel, Class<T> handler) {
T h = channel.pipeline().get(handler);
if (h == null) {
throw new IllegalArgumentException(
handler.getSimpleName() + " is absent in the channel pipeline");
}
return h;
}

static final class HandlerListener implements Http2FrameListener {
Http2FrameListener cur;
Http2FrameListener next;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,21 @@ public static boolean isValid(final Http2Headers requestHeaders, boolean endOfSt
}
}
}

/*preconditions*/

static long requirePositive(long value, String message) {
if (value <= 0) {
throw new IllegalArgumentException(message + " must be positive: " + value);
}
return value;
}

static short requireRange(int value, int from, int to, String message) {
if (value >= from && value <= to) {
return (short) value;
}
throw new IllegalArgumentException(
String.format("%s must belong to range [%d, %d]: ", message, from, to));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static Http2ConnectionHandlerBuilder configureHttp2Server(
* @return this {@link Http2WebSocketClientBuilder} instance
*/
public Http2WebSocketServerBuilder codec(Http1WebSocketCodec webSocketCodec) {
this.webSocketCodec = Preconditions.requireNonNull(webSocketCodec, "webSocketCodec");
this.webSocketCodec = Objects.requireNonNull(webSocketCodec, "webSocketCodec");
return this;
}

Expand All @@ -107,7 +107,7 @@ public Http2WebSocketServerBuilder codec(Http1WebSocketCodec webSocketCodec) {
*/
public Http2WebSocketServerBuilder decoderConfig(WebSocketDecoderConfig webSocketDecoderConfig) {
this.webSocketDecoderConfig =
Preconditions.requireNonNull(webSocketDecoderConfig, "webSocketDecoderConfig");
Objects.requireNonNull(webSocketDecoderConfig, "webSocketDecoderConfig");
return this;
}

Expand All @@ -120,7 +120,7 @@ public Http2WebSocketServerBuilder decoderConfig(WebSocketDecoderConfig webSocke
public Http2WebSocketServerBuilder closedWebSocketRemoveTimeout(
long closedWebSocketRemoveTimeoutMillis) {
this.closedWebSocketRemoveTimeoutMillis =
Preconditions.requirePositive(
Http2WebSocketProtocol.requirePositive(
closedWebSocketRemoveTimeoutMillis, "closedWebSocketRemoveTimeoutMillis");
return this;
}
Expand Down

This file was deleted.

0 comments on commit 6d9aba6

Please sign in to comment.