Apply "instanceof pattern matching" in spring-websocket

This commit also applies additional clean-up tasks such as the following.

- final fields
- diamond operator (<>) for anonymous inner classes

This has only been applied to `src/main/java`.
This commit is contained in:
Sam Brannen
2021-10-17 19:04:32 +02:00
parent 1f248b34f6
commit b51813e408
10 changed files with 41 additions and 49 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -209,10 +209,9 @@ public final class CloseStatus implements Serializable {
if (this == other) {
return true;
}
if (!(other instanceof CloseStatus)) {
if (!(other instanceof CloseStatus otherStatus)) {
return false;
}
CloseStatus otherStatus = (CloseStatus) other;
return (this.code == otherStatus.code && ObjectUtils.nullSafeEquals(this.reason, otherStatus.reason));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -311,10 +311,9 @@ public class WebSocketHttpHeaders extends HttpHeaders {
if (this == other) {
return true;
}
if (!(other instanceof WebSocketHttpHeaders)) {
if (!(other instanceof WebSocketHttpHeaders otherHeaders)) {
return false;
}
WebSocketHttpHeaders otherHeaders = (WebSocketHttpHeaders) other;
return this.headers.equals(otherHeaders.headers);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@ import org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator;
* this will be done automatically when the Spring ApplicationContext is refreshed.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 4.0
*/
public class WebSocketConnectionManager extends ConnectionManagerSupport {
@@ -46,7 +47,7 @@ public class WebSocketConnectionManager extends ConnectionManagerSupport {
@Nullable
private WebSocketSession webSocketSession;
private WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
private final WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
public WebSocketConnectionManager(WebSocketClient client,
@@ -116,16 +117,16 @@ public class WebSocketConnectionManager extends ConnectionManagerSupport {
@Override
public void startInternal() {
if (this.client instanceof Lifecycle && !((Lifecycle) this.client).isRunning()) {
((Lifecycle) this.client).start();
if (this.client instanceof Lifecycle lifecycle && !lifecycle.isRunning()) {
lifecycle.start();
}
super.startInternal();
}
@Override
public void stopInternal() throws Exception {
if (this.client instanceof Lifecycle && ((Lifecycle) this.client).isRunning()) {
((Lifecycle) this.client).stop();
if (this.client instanceof Lifecycle lifecycle && lifecycle.isRunning()) {
lifecycle.stop();
}
super.stopInternal();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@ import org.springframework.util.Assert;
* track of connected users and their subscriptions.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 4.2
*/
public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicationListener {
@@ -105,8 +106,8 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
return;
}
String name = user.getName();
if (user instanceof DestinationUserNameProvider) {
name = ((DestinationUserNameProvider) user).getDestinationUserName();
if (user instanceof DestinationUserNameProvider destinationUserNameProvider) {
name = destinationUserNameProvider.getDestinationUserName();
}
synchronized (this.sessionLock) {
LocalSimpUser simpUser = this.users.get(name);
@@ -238,7 +239,7 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
@Override
public boolean equals(@Nullable Object other) {
return (this == other ||
(other instanceof SimpUser && getName().equals(((SimpUser) other).getName())));
(other instanceof SimpUser otherSimpUser && getName().equals(otherSimpUser.getName())));
}
@Override
@@ -294,7 +295,7 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
@Override
public boolean equals(@Nullable Object other) {
return (this == other ||
(other instanceof SimpSubscription && getId().equals(((SimpSubscription) other).getId())));
(other instanceof SimpSubscription otherSubscription && getId().equals(otherSubscription.getId())));
}
@Override
@@ -346,10 +347,9 @@ public class DefaultSimpUserRegistry implements SimpUserRegistry, SmartApplicati
if (this == other) {
return true;
}
if (!(other instanceof SimpSubscription)) {
if (!(other instanceof SimpSubscription otherSubscription)) {
return false;
}
SimpSubscription otherSubscription = (SimpSubscription) other;
return (getId().equals(otherSubscription.getId()) &&
getSession().getId().equals(otherSubscription.getSession().getId()));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,9 +33,9 @@ import org.springframework.web.socket.server.HandshakeInterceptor;
/**
* An interceptor to copy information from the HTTP session to the "handshake
* attributes" map to made available via{@link WebSocketSession#getAttributes()}.
* attributes" map to be made available via {@link WebSocketSession#getAttributes()}.
*
* <p>Copies a subset or all HTTP session attributes and/or the HTTP session id
* <p>Copies a subset or all HTTP session attributes and/or the HTTP session ID
* under the key {@link #HTTP_SESSION_ID_ATTR_NAME}.
*
* @author Rossen Stoyanchev
@@ -164,8 +164,7 @@ public class HttpSessionHandshakeInterceptor implements HandshakeInterceptor {
@Nullable
private HttpSession getSession(ServerHttpRequest request) {
if (request instanceof ServletServerHttpRequest) {
ServletServerHttpRequest serverRequest = (ServletServerHttpRequest) request;
if (request instanceof ServletServerHttpRequest serverRequest) {
return serverRequest.getServletRequest().getSession(isCreateSession());
}
return null;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -59,7 +59,7 @@ class DefaultTransportRequest implements TransportRequest {
private final TransportType serverTransportType;
private SockJsMessageCodec codec;
private final SockJsMessageCodec codec;
@Nullable
private Principal user;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -56,6 +56,7 @@ import org.springframework.web.util.UriComponentsBuilder;
* the transports it is configured with.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 4.1
* @see <a href="https://github.com/sockjs/sockjs-client">https://github.com/sockjs/sockjs-client</a>
* @see org.springframework.web.socket.sockjs.client.Transport
@@ -114,8 +115,8 @@ public class SockJsClient implements WebSocketClient, Lifecycle {
private static InfoReceiver initInfoReceiver(List<Transport> transports) {
for (Transport transport : transports) {
if (transport instanceof InfoReceiver) {
return ((InfoReceiver) transport);
if (transport instanceof InfoReceiver infoReceiver) {
return infoReceiver;
}
}
return new RestTemplateXhrTransport();
@@ -202,11 +203,8 @@ public class SockJsClient implements WebSocketClient, Lifecycle {
if (!isRunning()) {
this.running = true;
for (Transport transport : this.transports) {
if (transport instanceof Lifecycle) {
Lifecycle lifecycle = (Lifecycle) transport;
if (!lifecycle.isRunning()) {
lifecycle.start();
}
if (transport instanceof Lifecycle lifecycle && !lifecycle.isRunning()) {
lifecycle.start();
}
}
}
@@ -217,11 +215,8 @@ public class SockJsClient implements WebSocketClient, Lifecycle {
if (isRunning()) {
this.running = false;
for (Transport transport : this.transports) {
if (transport instanceof Lifecycle) {
Lifecycle lifecycle = (Lifecycle) transport;
if (lifecycle.isRunning()) {
lifecycle.stop();
}
if (transport instanceof Lifecycle lifecycle && lifecycle.isRunning()) {
lifecycle.stop();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -148,7 +148,7 @@ public class UndertowXhrTransport extends AbstractXhrTransport {
logger.trace("Starting XHR receive request for " + url);
}
ClientCallback<ClientConnection> clientCallback = new ClientCallback<ClientConnection>() {
ClientCallback<ClientConnection> clientCallback = new ClientCallback<>() {
@Override
public void completed(ClientConnection connection) {
ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(url.getPath());
@@ -182,7 +182,7 @@ public class UndertowXhrTransport extends AbstractXhrTransport {
final URI url, final HttpHeaders headers, final XhrClientSockJsSession sockJsSession,
final SettableListenableFuture<WebSocketSession> connectFuture) {
return new ClientCallback<ClientExchange>() {
return new ClientCallback<>() {
@Override
public void completed(final ClientExchange exchange) {
exchange.setResponseListener(new ClientCallback<ClientExchange>() {
@@ -309,7 +309,7 @@ public class UndertowXhrTransport extends AbstractXhrTransport {
private ClientCallback<ClientExchange> createRequestCallback(final @Nullable String body,
final List<ClientResponse> responses, final CountDownLatch latch) {
return new ClientCallback<ClientExchange>() {
return new ClientCallback<>() {
@Override
public void completed(ClientExchange result) {
result.setResponseListener(new ClientCallback<ClientExchange>() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,9 +43,9 @@ public class XhrClientSockJsSession extends AbstractClientSockJsSession {
private final XhrTransport transport;
private HttpHeaders headers;
private final HttpHeaders headers;
private HttpHeaders sendHeaders;
private final HttpHeaders sendHeaders;
private final URI sendUrl;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -125,10 +125,9 @@ public class SockJsFrame {
if (this == other) {
return true;
}
if (!(other instanceof SockJsFrame)) {
if (!(other instanceof SockJsFrame otherFrame)) {
return false;
}
SockJsFrame otherFrame = (SockJsFrame) other;
return (this.type.equals(otherFrame.type) && this.content.equals(otherFrame.content));
}