Skip to content

Commit 9af357b

Browse files
jbertramgemmellr
authored andcommitted
ARTEMIS-5278 use pattern matching for instanceof
1 parent 134f1a9 commit 9af357b

File tree

241 files changed

+1477
-1633
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

241 files changed

+1477
-1633
lines changed

artemis-cli/src/main/java/org/apache/activemq/artemis/cli/Artemis.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ public static Object internalExecute(boolean shellEnabled, File artemisHome, Fil
210210
Objects.requireNonNull(userObject, "Picocli action command should never be null");
211211
assert userObject != null;
212212

213-
if (userObject instanceof Action) {
214-
Action action = (Action) userObject;
213+
if (userObject instanceof Action action) {
215214
action.setHomeValues(artemisHome, artemisInstance, etcFolder);
216215
if (action.isVerbose()) {
217216
context.out.print("Executing " + action.getClass().getName() + " ");
@@ -224,8 +223,8 @@ public static Object internalExecute(boolean shellEnabled, File artemisHome, Fil
224223

225224
return action.execute(context);
226225
} else {
227-
if (userObject instanceof Runnable) {
228-
((Runnable) userObject).run();
226+
if (userObject instanceof Runnable runnable) {
227+
runnable.run();
229228
} else {
230229
throw new IllegalArgumentException(userObject.getClass() + " should implement either " + Action.class.getName() + " or " + Runnable.class.getName());
231230
}

artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/HelpAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public static void help(CommandLine commandLine, String... args) {
3434
Object subCommand = theLIn.getSubcommands().get(i);
3535
if (subCommand == null) {
3636
commandLine.usage(System.out);
37-
} else if (subCommand instanceof CommandLine) {
38-
theLIn = (CommandLine) subCommand;
37+
} else if (subCommand instanceof CommandLine subCommandLine) {
38+
theLIn = subCommandLine;
3939
} else {
4040
commandLine.usage(System.out);
4141
}

artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/activation/ActivationSequenceList.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,9 @@ public static ListResult execute(final ActivationSequenceList command,
8686
final HAPolicyConfiguration policyConfig = config.getHAPolicyConfiguration();
8787
final DistributedLockManagerConfiguration managerConfiguration;
8888
String coordinationId = null;
89-
if (policyConfig instanceof ReplicationBackupPolicyConfiguration) {
90-
ReplicationBackupPolicyConfiguration backupPolicyConfig = (ReplicationBackupPolicyConfiguration) policyConfig;
89+
if (policyConfig instanceof ReplicationBackupPolicyConfiguration backupPolicyConfig) {
9190
managerConfiguration = backupPolicyConfig.getDistributedManagerConfiguration();
92-
} else if (policyConfig instanceof ReplicationPrimaryPolicyConfiguration) {
93-
ReplicationPrimaryPolicyConfiguration primaryPolicyConfig = (ReplicationPrimaryPolicyConfiguration) policyConfig;
91+
} else if (policyConfig instanceof ReplicationPrimaryPolicyConfiguration primaryPolicyConfig) {
9492
managerConfiguration = primaryPolicyConfig.getDistributedManagerConfiguration();
9593
if (primaryPolicyConfig.getCoordinationId() != null) {
9694
coordinationId = primaryPolicyConfig.getCoordinationId();

artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/activation/ActivationSequenceSet.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,9 @@ public static void execute(final ActivationSequenceSet command,
8080
final HAPolicyConfiguration policyConfig = config.getHAPolicyConfiguration();
8181
final DistributedLockManagerConfiguration managerConfiguration;
8282
String coordinationId = nodeId;
83-
if (policyConfig instanceof ReplicationBackupPolicyConfiguration) {
84-
ReplicationBackupPolicyConfiguration backupPolicyConfig = (ReplicationBackupPolicyConfiguration) policyConfig;
83+
if (policyConfig instanceof ReplicationBackupPolicyConfiguration backupPolicyConfig) {
8584
managerConfiguration = backupPolicyConfig.getDistributedManagerConfiguration();
86-
} else if (policyConfig instanceof ReplicationPrimaryPolicyConfiguration) {
87-
ReplicationPrimaryPolicyConfiguration primaryPolicyConfig = (ReplicationPrimaryPolicyConfiguration) policyConfig;
85+
} else if (policyConfig instanceof ReplicationPrimaryPolicyConfiguration primaryPolicyConfig) {
8886
managerConfiguration = primaryPolicyConfig.getDistributedManagerConfiguration();
8987
if (primaryPolicyConfig.getCoordinationId() != null) {
9088
if (nodeId != null) {

artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/check/CheckAbstract.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ public Object execute(ActionContext context) throws Exception {
132132
return checkTask.get(timeout, TimeUnit.MILLISECONDS);
133133
} catch (ExecutionException e) {
134134
Throwable cause = e.getCause();
135-
if (cause instanceof CLIException) {
136-
throw (CLIException)cause;
135+
if (cause instanceof CLIException cliException) {
136+
throw cliException;
137137
} else {
138138
fail(cause.toString());
139139
}

artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConsumerThread.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,29 @@ private void handle(Message msg, boolean browse) throws JMSException {
8080
if (verbose) {
8181
context.out.println("..." + msg);
8282
}
83-
if (bytesAsText && (msg instanceof BytesMessage)) {
84-
long length = ((BytesMessage) msg).getBodyLength();
83+
if (bytesAsText && (msg instanceof BytesMessage bytesMessage)) {
84+
long length = bytesMessage.getBodyLength();
8585
byte[] bytes = new byte[(int) length];
86-
((BytesMessage) msg).readBytes(bytes);
86+
bytesMessage.readBytes(bytes);
8787
context.out.println("Message:" + msg);
8888
}
8989
} else {
9090
if (verbose) {
9191
context.out.println("JMS Message ID:" + msg.getJMSMessageID());
92-
if (bytesAsText && (msg instanceof BytesMessage)) {
93-
long length = ((BytesMessage) msg).getBodyLength();
92+
if (bytesAsText && (msg instanceof BytesMessage bytesMessage)) {
93+
long length = bytesMessage.getBodyLength();
9494
byte[] bytes = new byte[(int) length];
95-
((BytesMessage) msg).readBytes(bytes);
95+
bytesMessage.readBytes(bytes);
9696
context.out.println("Received a message with " + bytes.length);
9797
}
9898

99-
if (msg instanceof TextMessage) {
100-
String text = ((TextMessage) msg).getText();
99+
if (msg instanceof TextMessage textMessage) {
100+
String text = textMessage.getText();
101101
context.out.println("Received text sized at " + text.length());
102102
}
103103

104-
if (msg instanceof ObjectMessage) {
105-
Object obj = ((ObjectMessage) msg).getObject();
104+
if (msg instanceof ObjectMessage objectMessage) {
105+
Object obj = objectMessage.getObject();
106106
context.out.println("Received object " + obj.toString().length());
107107
}
108108
}
@@ -126,7 +126,7 @@ public void browse() {
126126
while (enumBrowse.hasMoreElements()) {
127127
Message msg = enumBrowse.nextElement();
128128
if (msg != null) {
129-
context.out.println(threadName + " browsing " + (msg instanceof TextMessage ? ((TextMessage) msg).getText() : msg.getJMSMessageID()));
129+
context.out.println(threadName + " browsing " + (msg instanceof TextMessage tm ? tm.getText() : msg.getJMSMessageID()));
130130
handle(msg, true);
131131
received++;
132132

@@ -169,11 +169,11 @@ public void consume() {
169169
String threadName = Thread.currentThread().getName();
170170
context.out.println(threadName + " wait " + (receiveTimeOut == -1 ? "forever" : receiveTimeOut + "ms") + " until " + messageCount + " messages are consumed");
171171
try {
172-
if (durable && destination instanceof Topic) {
172+
if (durable && destination instanceof Topic topic) {
173173
if (filter != null) {
174-
consumer = session.createDurableSubscriber((Topic) destination, getName(), filter, false);
174+
consumer = session.createDurableSubscriber(topic, getName(), filter, false);
175175
} else {
176-
consumer = session.createDurableSubscriber((Topic) destination, getName());
176+
consumer = session.createDurableSubscriber(topic, getName());
177177
}
178178
} else {
179179
if (filter != null) {
@@ -193,7 +193,7 @@ public void consume() {
193193
}
194194
if (msg != null) {
195195
if (verbose) {
196-
context.out.println(threadName + " Received " + (msg instanceof TextMessage ? ((TextMessage) msg).getText() : msg.getJMSMessageID()));
196+
context.out.println(threadName + " Received " + (msg instanceof TextMessage tm ? tm.getText() : msg.getJMSMessageID()));
197197
} else {
198198
if (++count % 1000 == 0) {
199199
context.out.println("Received " + count);

artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ProducerThread.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private void sendMessage(MessageProducer producer, String threadName) throws Exc
132132

133133
producer.send(message);
134134
if (verbose) {
135-
context.out.println(threadName + " Sent: " + (message instanceof TextMessage ? ((TextMessage) message).getText() : message.getJMSMessageID()));
135+
context.out.println(threadName + " Sent: " + (message instanceof TextMessage tm ? tm.getText() : message.getJMSMessageID()));
136136
}
137137

138138
if (transactionBatchSize > 0 && sentCount.get() > 0 && sentCount.get() % transactionBatchSize == 0) {

artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/Transfer.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,7 @@ private int doTransfer(ActionContext context) throws Exception {
353353
} else {
354354
consumer = sourceSession.createConsumer(sourceDestination);
355355
}
356-
} else if (sourceDestination instanceof Topic) {
357-
358-
Topic topic = (Topic) sourceDestination;
356+
} else if (sourceDestination instanceof Topic topic) {
359357

360358
if (durableConsumer != null) {
361359
if (filter != null) {

artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/xml/XMLMessageExporter.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ public void printLargeMessageBody(Message message) throws XMLStreamException {
8484
LargeBodyReader encoder = null;
8585

8686
try {
87-
if (message instanceof LargeServerMessage) {
88-
encoder = ((LargeServerMessage)message).toMessage().toCore().getLargeBodyReader();
89-
} else if (message instanceof ClientLargeMessageImpl) {
90-
encoder = ((ClientLargeMessageImpl)message).getLargeBodyReader();
87+
if (message instanceof LargeServerMessage largeServerMessage) {
88+
encoder = largeServerMessage.toMessage().toCore().getLargeBodyReader();
89+
} else if (message instanceof ClientLargeMessageImpl clientLargeMessage) {
90+
encoder = clientLargeMessage.getLargeBodyReader();
9191
} else {
9292
throw new RuntimeException("Unrecognized message implementation: " + message.getClass().getName());
9393
}

artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/xml/XmlDataExporterUtil.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
public class XmlDataExporterUtil {
3030

3131
public static String convertProperty(final Object value) {
32-
if (value instanceof byte[]) {
33-
return encode((byte[]) value);
32+
if (value instanceof byte[] bytes) {
33+
return encode(bytes);
3434
} else {
3535
return value == null ? XmlDataConstants.NULL : value.toString();
3636
}

artemis-cli/src/main/java/org/apache/activemq/artemis/integration/FileBroker.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public void stop(boolean isShutdown) throws Exception {
9191
ActiveMQComponent[] mqComponents = new ActiveMQComponent[components.size()];
9292
components.values().toArray(mqComponents);
9393
for (int i = mqComponents.length - 1; i >= 0; i--) {
94-
if (mqComponents[i] instanceof ServiceComponent) {
95-
((ServiceComponent) mqComponents[i]).stop(isShutdown);
94+
if (mqComponents[i] instanceof ServiceComponent serviceComponent) {
95+
serviceComponent.stop(isShutdown);
9696
} else {
9797
mqComponents[i].stop();
9898
}

artemis-cli/src/test/java/org/apache/activemq/cli/test/MessageSerializerTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ private void checkSentMessages(Session session, List<Message> messages, String a
113113
List<Message> received = consumeMessages(session, address, TEST_MESSAGE_COUNT, CompositeAddress.isFullyQualified(address));
114114
for (int i = 0; i < TEST_MESSAGE_COUNT; i++) {
115115
Message m = messages.get(i);
116-
if (m instanceof TextMessage) {
117-
assertEquals(((TextMessage) m).getText(), ((TextMessage) received.get(i)).getText());
118-
} else if (m instanceof ObjectMessage) {
119-
assertEquals(((ObjectMessage) m).getObject(), ((ObjectMessage) received.get(i)).getObject());
120-
} else if (m instanceof MapMessage) {
121-
assertEquals(((MapMessage) m).getString(key), ((MapMessage) received.get(i)).getString(key));
116+
if (m instanceof TextMessage textMessage) {
117+
assertEquals(textMessage.getText(), ((TextMessage) received.get(i)).getText());
118+
} else if (m instanceof ObjectMessage objectMessage) {
119+
assertEquals(objectMessage.getObject(), ((ObjectMessage) received.get(i)).getObject());
120+
} else if (m instanceof MapMessage mapMessage) {
121+
assertEquals(mapMessage.getString(key), ((MapMessage) received.get(i)).getString(key));
122122
}
123123
}
124124
}

artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/SimpleString.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,8 @@ public boolean equals(final Object other) {
395395
return true;
396396
}
397397

398-
if (other instanceof SimpleString) {
399-
SimpleString s = (SimpleString) other;
400-
401-
return ByteUtil.equals(data, s.data);
398+
if (other instanceof SimpleString simpleString) {
399+
return ByteUtil.equals(data, simpleString.data);
402400
} else {
403401
return false;
404402
}

artemis-commons/src/main/java/org/apache/activemq/artemis/json/impl/JsonArrayImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ public boolean isEmpty() {
112112

113113
@Override
114114
public boolean contains(Object o) {
115-
if (o instanceof JsonValueImpl) {
116-
return rawArray.contains(((JsonValueImpl)o).getRawValue());
115+
if (o instanceof JsonValueImpl jsonValue) {
116+
return rawArray.contains(jsonValue.getRawValue());
117117
} else {
118118
return rawArray.contains(o);
119119
}

artemis-commons/src/main/java/org/apache/activemq/artemis/json/impl/JsonValueImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public String toString() {
105105

106106
@Override
107107
public boolean equals(Object obj) {
108-
if (obj instanceof JsonValueImpl) {
109-
return rawValue.equals(((JsonValueImpl)obj).getRawValue());
108+
if (obj instanceof JsonValueImpl jsonValue) {
109+
return rawValue.equals(jsonValue.getRawValue());
110110
}
111111
return super.equals(obj);
112112
}

artemis-commons/src/main/java/org/apache/activemq/artemis/logs/AuditLogger.java

+18-18
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,24 @@ static String parametersList(Object value) {
129129

130130
final String prefix = "with parameters: ";
131131

132-
if (value instanceof long[]) {
133-
return prefix + Arrays.toString((long[])value);
134-
} else if (value instanceof int[]) {
135-
return prefix + Arrays.toString((int[])value);
136-
} else if (value instanceof char[]) {
137-
return prefix + Arrays.toString((char[])value);
138-
} else if (value instanceof byte[]) {
139-
return prefix + Arrays.toString((byte[])value);
140-
} else if (value instanceof float[]) {
141-
return prefix + Arrays.toString((float[])value);
142-
} else if (value instanceof short[]) {
143-
return prefix + Arrays.toString((short[])value);
144-
} else if (value instanceof double[]) {
145-
return prefix + Arrays.toString((double[])value);
146-
} else if (value instanceof boolean[]) {
147-
return prefix + Arrays.toString((boolean[])value);
148-
} else if (value instanceof Object[]) {
149-
return prefix + Arrays.toString((Object[])value);
132+
if (value instanceof long[] longs) {
133+
return prefix + Arrays.toString(longs);
134+
} else if (value instanceof int[] ints) {
135+
return prefix + Arrays.toString(ints);
136+
} else if (value instanceof char[] chars) {
137+
return prefix + Arrays.toString(chars);
138+
} else if (value instanceof byte[] bytes) {
139+
return prefix + Arrays.toString(bytes);
140+
} else if (value instanceof float[] floats) {
141+
return prefix + Arrays.toString(floats);
142+
} else if (value instanceof short[] shorts) {
143+
return prefix + Arrays.toString(shorts);
144+
} else if (value instanceof double[] doubles) {
145+
return prefix + Arrays.toString(doubles);
146+
} else if (value instanceof boolean[] booleans) {
147+
return prefix + Arrays.toString(booleans);
148+
} else if (value instanceof Object[] objects) {
149+
return prefix + Arrays.toString(objects);
150150
} else {
151151
return prefix + value.toString();
152152
}

artemis-commons/src/main/java/org/apache/activemq/artemis/utils/bean/MetaBean.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ public void parseToJSON(T object, JsonObjectBuilder builder, boolean ignoreNullA
114114
} else if (type == String.class || type == SimpleString.class) {
115115
logger.trace("Setting {} as String {}", name, value);
116116
builder.add(name, String.valueOf(value));
117-
} else if (Number.class.isAssignableFrom(type) && value instanceof Number) {
117+
} else if (Number.class.isAssignableFrom(type) && value instanceof Number number) {
118118
if (value instanceof Double || value instanceof Float) {
119119
logger.trace("Setting {} as double {}", name, value);
120-
builder.add(name, ((Number) value).doubleValue());
120+
builder.add(name, number.doubleValue());
121121
} else {
122122
logger.trace("Setting {} as long {}", name, value);
123-
builder.add(name, ((Number) value).longValue());
123+
builder.add(name, number.longValue());
124124
}
125125
} else if (type == Boolean.class) {
126126
builder.add(name, (Boolean) value);

artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/LinkedListImpl.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,7 @@ public static class Node<E> {
492492

493493
private static <E> Node<E> with(final E o) {
494494
Objects.requireNonNull(o, "Only HEAD nodes are allowed to hold null values");
495-
if (o instanceof Node) {
496-
final Node node = (Node) o;
495+
if (o instanceof Node node) {
497496
//only a node that not belong already to a list is allowed to be reused
498497
if (node.prev == null && node.next == null) {
499498
//reset the iterCount

artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/LongHashSet.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private void rehash(final int newCapacity) {
204204
*/
205205
@Override
206206
public boolean remove(final Object value) {
207-
return value instanceof Long && remove(((Long) value).longValue());
207+
return value instanceof Long l && remove(l.longValue());
208208
}
209209

210210
/**
@@ -279,7 +279,7 @@ public void compact() {
279279
*/
280280
@Override
281281
public boolean contains(final Object value) {
282-
return value instanceof Long && contains(((Long) value).longValue());
282+
return value instanceof Long l && contains(l.longValue());
283283
}
284284

285285
/**
@@ -468,8 +468,7 @@ public boolean equals(final Object other) {
468468
return true;
469469
}
470470

471-
if (other instanceof LongHashSet) {
472-
final LongHashSet otherSet = (LongHashSet) other;
471+
if (other instanceof LongHashSet otherSet) {
473472

474473
return otherSet.containsMissingValue == containsMissingValue && otherSet.sizeOfArrayValues == sizeOfArrayValues && containsAll(otherSet);
475474
}

artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/NoOpMap.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public Set<Entry<K, V>> entrySet() {
9090

9191
@Override
9292
public boolean equals(Object o) {
93-
return (o instanceof Map) && ((Map)o).size() == 0;
93+
return (o instanceof Map m) && m.size() == 0;
9494
}
9595

9696
@Override

artemis-commons/src/main/java/org/apache/activemq/artemis/utils/collections/PriorityCollection.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ public synchronized boolean remove(Object o) {
183183
}
184184

185185
private boolean removeInternal(Object o) {
186-
if (o instanceof PriorityAware) {
187-
PriorityAware priorityAware = (PriorityAware) o;
186+
if (o instanceof PriorityAware priorityAware) {
188187
Collection<E> priority = getCollection(priorityAware.getPriority(), false);
189188
boolean result = priority != null && priority.remove(priorityAware);
190189
if (priority != null && priority.isEmpty()) {
@@ -283,7 +282,7 @@ public synchronized void clear() {
283282

284283
@Override
285284
public boolean contains(Object o) {
286-
return o instanceof PriorityAware && contains((PriorityAware) o);
285+
return o instanceof PriorityAware pa && contains(pa);
287286
}
288287

289288
public boolean contains(PriorityAware priorityAware) {

0 commit comments

Comments
 (0)