Skip to content

Commit

Permalink
Javadoc and reduce boilerplate
Browse files Browse the repository at this point in the history
- Use for-each loop instead of iterator()
- Use forEach() API
- Use lambdas instead of anonymous inner class
- Use method reference
- Use final
- Use blocks
  • Loading branch information
garydgregory committed Sep 16, 2024
1 parent aec14c4 commit 5ccfeca
Show file tree
Hide file tree
Showing 36 changed files with 174 additions and 281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ private int findFullMatch(final List<HPackEntry> entries, final String value) {
if (entries == null || entries.isEmpty()) {
return 0;
}
for (int i = 0; i < entries.size(); i++) {
final HPackEntry entry = entries.get(i);
for (final HPackEntry entry : entries) {
if (Objects.equals(value, entry.getHeader().getValue())) {
return entry.getIndex();
}
Expand Down Expand Up @@ -303,8 +302,8 @@ void encodeHeader(
void encodeHeaders(
final ByteArrayBuffer dst, final List<? extends Header> headers,
final boolean noIndexing, final boolean useHuffman) throws CharacterCodingException {
for (int i = 0; i < headers.size(); i++) {
encodeHeader(dst, headers.get(i), noIndexing, useHuffman);
for (final Header header : headers) {
encodeHeader(dst, header, noIndexing, useHuffman);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ public HttpRequest convert(final List<Header> headers) throws HttpException {
throw new ProtocolException(ex.getMessage(), ex);
}
httpRequest.setPath(path);
for (int i = 0; i < messageHeaders.size(); i++) {
httpRequest.addHeader(messageHeaders.get(i));
}
messageHeaders.forEach(httpRequest::addHeader);
return httpRequest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ public HttpResponse convert(final List<Header> headers) throws HttpException {
}
final HttpResponse response = new BasicHttpResponse(statusCode, null);
response.setVersion(HttpVersion.HTTP_2);
for (int i = 0; i < messageHeaders.size(); i++) {
response.addHeader(messageHeaders.get(i));
}
messageHeaders.forEach(response::addHeader);
return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
Expand Down Expand Up @@ -478,8 +479,7 @@ public final void onOutput() throws HttpException, IOException {
final int pendingOutputRequests = outputRequests.get();
boolean outputPending = false;
if (!streamMap.isEmpty() && connOutputWindow.get() > 0) {
for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
final Map.Entry<Integer, H2Stream> entry = it.next();
for (final Entry<Integer, H2Stream> entry : streamMap.entrySet()) {
final H2Stream stream = entry.getValue();
if (!stream.isLocalClosed()
&& stream.getOutputWindow().get() > 0
Expand Down Expand Up @@ -553,8 +553,7 @@ public final void onTimeout(final Timeout timeout) throws HttpException, IOExcep
"Timeout due to inactivity (" + timeout + ")");
}
commitFrame(goAway);
for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
final Map.Entry<Integer, H2Stream> entry = it.next();
for (final Entry<Integer, H2Stream> entry : streamMap.entrySet()) {
final H2Stream stream = entry.getValue();
stream.reset(new H2StreamResetException(H2Error.NO_ERROR, "Timeout due to inactivity (" + timeout + ")"));
}
Expand All @@ -570,8 +569,7 @@ public final void onDisconnect() {
break;
}
}
for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
final Map.Entry<Integer, H2Stream> entry = it.next();
for (final Entry<Integer, H2Stream> entry : streamMap.entrySet()) {
final H2Stream stream = entry.getValue();
stream.cancel();
}
Expand All @@ -598,8 +596,7 @@ private void processPendingCommands() throws IOException, HttpException {
if (command instanceof ShutdownCommand) {
final ShutdownCommand shutdownCommand = (ShutdownCommand) command;
if (shutdownCommand.getType() == CloseMode.IMMEDIATE) {
for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
final Map.Entry<Integer, H2Stream> entry = it.next();
for (final Entry<Integer, H2Stream> entry : streamMap.entrySet()) {
final H2Stream stream = entry.getValue();
stream.cancel();
}
Expand Down Expand Up @@ -673,8 +670,7 @@ public final void onException(final Exception cause) {
break;
}
}
for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
final Map.Entry<Integer, H2Stream> entry = it.next();
for (final Entry<Integer, H2Stream> entry : streamMap.entrySet()) {
final H2Stream stream = entry.getValue();
stream.reset(cause);
}
Expand Down Expand Up @@ -1009,8 +1005,7 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio
}
connState = streamMap.isEmpty() ? ConnectionHandshake.SHUTDOWN : ConnectionHandshake.GRACEFUL_SHUTDOWN;
} else {
for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
final Map.Entry<Integer, H2Stream> entry = it.next();
for (final Entry<Integer, H2Stream> entry : streamMap.entrySet()) {
final H2Stream stream = entry.getValue();
stream.reset(new H2StreamResetException(errorCode, "Connection terminated by the peer (" + errorCode + ")"));
}
Expand Down Expand Up @@ -1233,8 +1228,7 @@ private void applyRemoteSettings(final H2Config config) throws H2ConnectionExcep

if (delta != 0) {
if (!streamMap.isEmpty()) {
for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
final Map.Entry<Integer, H2Stream> entry = it.next();
for (final Entry<Integer, H2Stream> entry : streamMap.entrySet()) {
final H2Stream stream = entry.getValue();
try {
updateOutputWindow(stream.getId(), stream.getOutputWindow(), delta);
Expand All @@ -1254,8 +1248,7 @@ private void applyLocalSettings() throws H2ConnectionException {
initInputWinSize = localConfig.getInitialWindowSize();

if (delta != 0 && !streamMap.isEmpty()) {
for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
final Map.Entry<Integer, H2Stream> entry = it.next();
for (final Entry<Integer, H2Stream> entry : streamMap.entrySet()) {
final H2Stream stream = entry.getValue();
try {
updateInputWindow(stream.getId(), stream.getInputWindow(), delta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public void inputReady(final IOSession session, final ByteBuffer src) throws IOE
}
final ByteBuffer data = inBuf.data();
if (data.remaining() >= PREFACE.length) {
for (int i = 0; i < PREFACE.length; i++) {
if (data.get() != PREFACE[i]) {
for (final byte element : PREFACE) {
if (data.get() != element) {
throw new ProtocolNegotiationException("Unexpected HTTP/2 preface");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public ByteBuffer getData() {
@Override
public void consumeResponse(final ByteBuffer feedback) throws HttpException, IOException {
boolean result = true;
for (int i = 0; i < PING_MESSAGE.length; i++) {
if (!feedback.hasRemaining() || PING_MESSAGE[i] != feedback.get()) {
for (final byte element : PING_MESSAGE) {
if (!feedback.hasRemaining() || element != feedback.get()) {
result = false;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,12 @@ public static void main(final String[] args) throws Exception {

@Override
public void onHeaderInput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header));
}

@Override
public void onHeaderOutput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,12 @@ public static void main(final String[] args) throws Exception {

@Override
public void onHeaderInput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header));
}

@Override
public void onHeaderOutput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,12 @@ public static void main(final String[] args) throws Exception {

@Override
public void onHeaderInput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header));
}

@Override
public void onHeaderOutput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,12 @@ public static void main(final String[] args) throws Exception {

@Override
public void onHeaderInput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header));
}

@Override
public void onHeaderOutput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,12 @@ public static void main(final String[] args) throws Exception {

@Override
public void onHeaderInput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header));
}

@Override
public void onHeaderOutput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,12 @@ public static void main(final String[] args) throws Exception {

@Override
public void onHeaderInput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header));
}

@Override
public void onHeaderOutput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,12 @@ public static void main(final String[] args) throws Exception {

@Override
public void onHeaderInput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header));
}

@Override
public void onHeaderOutput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,12 @@ public void onExchangeComplete(final HttpConnection connection, final boolean ke

@Override
public void onHeaderInput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header));
}

@Override
public void onHeaderOutput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) {
for (int i = 0; i < headers.size(); i++) {
System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i));
}
headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,16 @@ void testEnsureCapacity() throws Exception {
};

private static String constructHelloString(final int[] raw, final int n) {
final StringBuilder str = new StringBuilder(raw.length);
for (final int element : raw) {
str.append((char) element);
}
final StringBuilder buffer = new StringBuilder();
for (int j = 0; j < n; j++) {
if (j > 0) {
buffer.append("; ");
}
for (int i = 0; i < raw.length; i++) {
buffer.append((char) raw[i]);
}
buffer.append(str);
}
return buffer.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static void main(final String[] args) throws Exception {
.build();

final HttpAsyncServer server = AsyncServerBootstrap.bootstrap()
.setExceptionCallback(e -> e.printStackTrace())
.setExceptionCallback(Exception::printStackTrace)
.setIOReactorConfig(config)
.setStreamListener(new Http1StreamListener() {
@Override
Expand Down
Loading

0 comments on commit 5ccfeca

Please sign in to comment.