Skip to content

Commit 9dd9f08

Browse files
committed
Use switch expression; some other clean up
1 parent fc3b95e commit 9dd9f08

File tree

13 files changed

+122
-218
lines changed

13 files changed

+122
-218
lines changed

spring-integration-amqp/src/main/java/org/springframework/integration/amqp/inbound/AmqpMessageSource.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2022 the original author or authors.
2+
* Copyright 2018-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -276,17 +276,11 @@ public void acknowledge(Status status) {
276276
try {
277277
long deliveryTag = this.ackInfo.getGetResponse().getEnvelope().getDeliveryTag();
278278
switch (status) {
279-
case ACCEPT:
280-
this.ackInfo.getChannel().basicAck(deliveryTag, false);
281-
break;
282-
case REJECT:
283-
this.ackInfo.getChannel().basicReject(deliveryTag, false);
284-
break;
285-
case REQUEUE:
286-
this.ackInfo.getChannel().basicReject(deliveryTag, true);
287-
break;
288-
default:
289-
break;
279+
case ACCEPT -> this.ackInfo.getChannel().basicAck(deliveryTag, false);
280+
case REJECT -> this.ackInfo.getChannel().basicReject(deliveryTag, false);
281+
case REQUEUE -> this.ackInfo.getChannel().basicReject(deliveryTag, true);
282+
default -> {
283+
}
290284
}
291285
if (this.ackInfo.isTransacted()) {
292286
this.ackInfo.getChannel().txCommit();

spring-integration-core/src/main/java/org/springframework/integration/channel/FluxMessageChannel.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -78,21 +78,15 @@ protected boolean doSend(Message<?> message, long timeout) {
7878
}
7979

8080
private boolean tryEmitMessage(Message<?> message) {
81-
switch (this.sink.tryEmitNext(message)) {
82-
case OK:
83-
return true;
84-
case FAIL_NON_SERIALIZED:
85-
case FAIL_OVERFLOW:
86-
return false;
87-
case FAIL_ZERO_SUBSCRIBER:
88-
throw new IllegalStateException("The [" + this + "] doesn't have subscribers to accept messages");
89-
case FAIL_TERMINATED:
90-
case FAIL_CANCELLED:
91-
throw new IllegalStateException("Cannot emit messages into the cancelled or terminated sink: "
92-
+ this.sink);
93-
default:
94-
throw new UnsupportedOperationException();
95-
}
81+
return switch (this.sink.tryEmitNext(message)) {
82+
case OK -> true;
83+
case FAIL_NON_SERIALIZED, FAIL_OVERFLOW -> false;
84+
case FAIL_ZERO_SUBSCRIBER ->
85+
throw new IllegalStateException("The [" + this + "] doesn't have subscribers to accept messages");
86+
case FAIL_TERMINATED, FAIL_CANCELLED ->
87+
throw new IllegalStateException("Cannot emit messages into the cancelled or terminated sink: "
88+
+ this.sink);
89+
};
9690
}
9791

9892
@Override

spring-integration-core/src/main/java/org/springframework/integration/handler/LoggingHandler.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -151,7 +151,7 @@ public void setLoggerName(String loggerName) {
151151

152152
/**
153153
* Specify whether to log the full Message. Otherwise, only the payload will be logged. This value is
154-
* <code>false</code> by default.
154+
* {@code false} by default.
155155
* @param shouldLogFullMessage true if the complete message should be logged.
156156
*/
157157
public void setShouldLogFullMessage(boolean shouldLogFullMessage) {
@@ -178,26 +178,13 @@ protected void onInit() {
178178
protected void handleMessageInternal(Message<?> message) {
179179
Supplier<CharSequence> logMessage = () -> createLogMessage(message);
180180
switch (this.level) {
181-
case FATAL:
182-
this.messageLogger.fatal(logMessage);
183-
break;
184-
case ERROR:
185-
this.messageLogger.error(logMessage);
186-
break;
187-
case WARN:
188-
this.messageLogger.warn(logMessage);
189-
break;
190-
case INFO:
191-
this.messageLogger.info(logMessage);
192-
break;
193-
case DEBUG:
194-
this.messageLogger.debug(logMessage);
195-
break;
196-
case TRACE:
197-
this.messageLogger.trace(logMessage);
198-
break;
199-
default:
200-
throw new IllegalStateException("Level '" + this.level + "' is not supported");
181+
case FATAL -> this.messageLogger.fatal(logMessage);
182+
case ERROR -> this.messageLogger.error(logMessage);
183+
case WARN -> this.messageLogger.warn(logMessage);
184+
case INFO -> this.messageLogger.info(logMessage);
185+
case DEBUG -> this.messageLogger.debug(logMessage);
186+
case TRACE -> this.messageLogger.trace(logMessage);
187+
default -> throw new IllegalStateException("Level '" + this.level + "' is not supported");
201188
}
202189
}
203190

@@ -209,7 +196,7 @@ private String createLogMessage(Message<?> message) {
209196
: Objects.toString(logMessage);
210197
}
211198

212-
private String createLogMessage(Throwable throwable) {
199+
private static String createLogMessage(Throwable throwable) {
213200
StringWriter stringWriter = new StringWriter();
214201
if (throwable instanceof AggregateMessageDeliveryException) {
215202
stringWriter.append(throwable.getMessage());
@@ -223,7 +210,7 @@ private String createLogMessage(Throwable throwable) {
223210
return stringWriter.toString();
224211
}
225212

226-
private void printStackTrace(Throwable throwable, Writer writer) {
213+
private static void printStackTrace(Throwable throwable, Writer writer) {
227214
throwable.printStackTrace(new PrintWriter(writer, true));
228215
}
229216

spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -579,24 +579,16 @@ private void setupLocalDirectory() {
579579
@Override
580580
protected Object handleRequestMessage(final Message<?> requestMessage) {
581581
if (this.command != null) {
582-
switch (this.command) {
583-
case LS:
584-
return doLs(requestMessage);
585-
case NLST:
586-
return doNlst(requestMessage);
587-
case GET:
588-
return doGet(requestMessage);
589-
case MGET:
590-
return doMget(requestMessage);
591-
case RM:
592-
return doRm(requestMessage);
593-
case MV:
594-
return doMv(requestMessage);
595-
case PUT:
596-
return doPut(requestMessage);
597-
case MPUT:
598-
return doMput(requestMessage);
599-
}
582+
return switch (this.command) {
583+
case LS -> doLs(requestMessage);
584+
case NLST -> doNlst(requestMessage);
585+
case GET -> doGet(requestMessage);
586+
case MGET -> doMget(requestMessage);
587+
case RM -> doRm(requestMessage);
588+
case MV -> doMv(requestMessage);
589+
case PUT -> doPut(requestMessage);
590+
case MPUT -> doMput(requestMessage);
591+
};
600592
}
601593
return this.remoteFileTemplate.execute(session ->
602594
AbstractRemoteFileOutboundGateway.this.messageSessionCallback.doInSession(session, requestMessage));

spring-integration-ftp/src/main/java/org/springframework/integration/ftp/session/AbstractFtpSessionFactory.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -216,18 +216,12 @@ private T createClient() throws IOException {
216216
}
217217

218218
/**
219-
* Sets the mode of the connection. Only local modes are supported.
219+
* Set the mode of the connection. Only local modes are supported.
220220
*/
221221
private void updateClientMode(FTPClient client) {
222222
switch (this.clientMode) {
223-
case FTPClient.ACTIVE_LOCAL_DATA_CONNECTION_MODE:
224-
client.enterLocalActiveMode();
225-
break;
226-
case FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE:
227-
client.enterLocalPassiveMode();
228-
break;
229-
default:
230-
break;
223+
case FTPClient.ACTIVE_LOCAL_DATA_CONNECTION_MODE -> client.enterLocalActiveMode();
224+
case FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE -> client.enterLocalPassiveMode();
231225
}
232226
}
233227

spring-integration-hazelcast/src/main/java/org/springframework/integration/hazelcast/inbound/HazelcastDistributedSQLMessageSource.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,8 @@
3232
* distributed query in the cluster and returns results in the light of iteration type.
3333
*
3434
* @author Eren Avsarogullari
35+
* @author Artem Bilan
36+
*
3537
* @since 6.0
3638
*/
3739
@SuppressWarnings("rawtypes")
@@ -63,30 +65,20 @@ public String getComponentType() {
6365
@Override
6466
@SuppressWarnings("unchecked")
6567
protected Collection<?> doReceive() {
66-
switch (this.iterationType) {
67-
case ENTRY:
68-
return getDistributedSQLResultSet(Collections
69-
.unmodifiableCollection(this.distributedMap.entrySet(new SqlPredicate(this.distributedSql))));
70-
71-
case KEY:
72-
return getDistributedSQLResultSet(Collections
73-
.unmodifiableCollection(this.distributedMap.keySet(new SqlPredicate(this.distributedSql))));
74-
75-
case LOCAL_KEY:
76-
return getDistributedSQLResultSet(Collections
77-
.unmodifiableCollection(this.distributedMap.localKeySet(new SqlPredicate(this.distributedSql))));
78-
79-
default:
80-
return getDistributedSQLResultSet(this.distributedMap.values(new SqlPredicate(this.distributedSql)));
81-
}
82-
}
68+
final SqlPredicate predicate = new SqlPredicate(this.distributedSql);
69+
Collection<?> collection =
70+
switch (this.iterationType) {
71+
case ENTRY -> this.distributedMap.entrySet(predicate);
72+
case KEY -> this.distributedMap.keySet(predicate);
73+
case LOCAL_KEY -> this.distributedMap.localKeySet(predicate);
74+
default -> this.distributedMap.values(predicate);
75+
};
8376

84-
private Collection<?> getDistributedSQLResultSet(Collection<?> collection) {
8577
if (CollectionUtils.isEmpty(collection)) {
8678
return null;
8779
}
8880

89-
return collection;
81+
return Collections.unmodifiableCollection(collection);
9082
}
9183

9284
}

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioConnection.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@
2222
import java.io.OutputStream;
2323
import java.io.UncheckedIOException;
2424
import java.net.SocketTimeoutException;
25-
import java.nio.Buffer;
2625
import java.nio.ByteBuffer;
2726
import java.nio.channels.ClosedChannelException;
2827
import java.nio.channels.SelectionKey;
@@ -224,14 +223,7 @@ protected InputStream inputStream() {
224223
* @return The buffer.
225224
*/
226225
protected ByteBuffer allocate(int length) {
227-
ByteBuffer buffer;
228-
if (this.usingDirectBuffers) {
229-
buffer = ByteBuffer.allocateDirect(length);
230-
}
231-
else {
232-
buffer = ByteBuffer.allocate(length);
233-
}
234-
return buffer;
226+
return this.usingDirectBuffers ? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length);
235227
}
236228

237229
/**
@@ -447,7 +439,7 @@ private void doRead() throws IOException {
447439
if (logger.isTraceEnabled()) {
448440
logger.trace("After read: " + this.rawBuffer.position() + '/' + this.rawBuffer.limit());
449441
}
450-
((Buffer) this.rawBuffer).flip();
442+
this.rawBuffer.flip();
451443
if (logger.isTraceEnabled()) {
452444
logger.trace("After flip: " + this.rawBuffer.position() + '/' + this.rawBuffer.limit());
453445
}
@@ -472,7 +464,7 @@ protected void sendToPipe(ByteBuffer rawBufferToSend) throws IOException {
472464
logger.trace(getConnectionId() + " Sending " + rawBufferToSend.limit() + " to pipe");
473465
}
474466
this.channelInputStream.write(rawBufferToSend);
475-
((Buffer) rawBufferToSend).clear();
467+
rawBufferToSend.clear();
476468
}
477469

478470
private void checkForAssembler() {

0 commit comments

Comments
 (0)