Skip to content

Commit 85434a3

Browse files
arefbehboudimp911de
authored andcommitted
Consistently use instanceof pattern variable.
Closes #2967 Original pull request: #2971
1 parent f547760 commit 85434a3

14 files changed

+49
-56
lines changed

src/test/java/org/springframework/data/redis/connection/AbstractConnectionUnitTestBase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ private Class<T> resolveReturnedClassFromGernericType() {
5555
private ParameterizedType resolveReturnedClassFromGernericType(Class<?> clazz) {
5656

5757
Object genericSuperclass = clazz.getGenericSuperclass();
58-
if (genericSuperclass instanceof ParameterizedType) {
59-
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
58+
if (genericSuperclass instanceof ParameterizedType parameterizedType) {
6059
Type rawtype = parameterizedType.getRawType();
6160
if (AbstractConnectionUnitTestBase.class.equals(rawtype)) {
6261
return parameterizedType;

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveClusterTestSupport.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ public void tearDown() {
5252
if (nativeCommands != null) {
5353
nativeCommands.flushall();
5454

55-
if (nativeCommands instanceof RedisCommands) {
56-
((RedisCommands) nativeCommands).getStatefulConnection().close();
55+
if (nativeCommands instanceof RedisCommands redisCommands) {
56+
redisCommands.getStatefulConnection().close();
5757
}
5858

59-
if (nativeCommands instanceof RedisAdvancedClusterCommands) {
60-
((RedisAdvancedClusterCommands) nativeCommands).getStatefulConnection().close();
59+
if (nativeCommands instanceof RedisAdvancedClusterCommands redisAdvancedClusterCommands) {
60+
redisAdvancedClusterCommands.getStatefulConnection().close();
6161
}
6262
}
6363

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveCommandsTestSupport.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,16 @@ public String toString() {
155155
public void close() throws IOException {
156156

157157
try {
158-
if (connectionProvider instanceof DisposableBean) {
159-
((DisposableBean) connectionProvider).destroy();
158+
if (connectionProvider instanceof DisposableBean disposableBean) {
159+
disposableBean.destroy();
160160
}
161161

162-
if (nativeConnectionProvider instanceof DisposableBean) {
163-
((DisposableBean) nativeConnectionProvider).destroy();
162+
if (nativeConnectionProvider instanceof DisposableBean disposableBean) {
163+
disposableBean.destroy();
164164
}
165165

166-
if (nativeBinaryConnectionProvider instanceof DisposableBean) {
167-
((DisposableBean) nativeBinaryConnectionProvider).destroy();
166+
if (nativeBinaryConnectionProvider instanceof DisposableBean disposableBean) {
167+
disposableBean.destroy();
168168
}
169169
} catch (Exception ex) {
170170
throw new RuntimeException(ex);
@@ -198,21 +198,21 @@ public void tearDown() {
198198
if (nativeCommands != null) {
199199
flushAll();
200200

201-
if (nativeCommands instanceof RedisCommands) {
202-
nativeConnectionProvider.release(((RedisCommands) nativeCommands).getStatefulConnection());
201+
if (nativeCommands instanceof RedisCommands redisCommands) {
202+
nativeConnectionProvider.release((redisCommands).getStatefulConnection());
203203
}
204204

205-
if (nativeCommands instanceof RedisAdvancedClusterCommands) {
206-
nativeConnectionProvider.release(((RedisAdvancedClusterCommands) nativeCommands).getStatefulConnection());
205+
if (nativeCommands instanceof RedisAdvancedClusterCommands redisAdvancedClusterCommands) {
206+
nativeConnectionProvider.release((redisAdvancedClusterCommands).getStatefulConnection());
207207
}
208208

209-
if (nativeBinaryCommands instanceof RedisCommands) {
210-
nativeBinaryConnectionProvider.release(((RedisCommands) nativeBinaryCommands).getStatefulConnection());
209+
if (nativeBinaryCommands instanceof RedisCommands redisCommands) {
210+
nativeBinaryConnectionProvider.release((redisCommands).getStatefulConnection());
211211
}
212212

213-
if (nativeBinaryCommands instanceof RedisAdvancedClusterCommands) {
213+
if (nativeBinaryCommands instanceof RedisAdvancedClusterCommands redisAdvancedClusterCommands) {
214214
nativeBinaryConnectionProvider
215-
.release(((RedisAdvancedClusterCommands) nativeBinaryCommands).getStatefulConnection());
215+
.release((redisAdvancedClusterCommands).getStatefulConnection());
216216
}
217217
}
218218

src/test/java/org/springframework/data/redis/core/convert/DefaultRedisTypeMapperUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private void writesTypeToField(Bucket bucket, Class<?> type, @Nullable Object va
215215
assertThat(bucket.keySet()).isEmpty();
216216
} else {
217217

218-
byte[] expected = value instanceof Class ? ((Class) value).getName().getBytes() : value.toString().getBytes();
218+
byte[] expected = value instanceof Class javaClass ? javaClass.getName().getBytes() : value.toString().getBytes();
219219

220220
assertThat(bucket.asMap()).containsKey(DefaultRedisTypeMapper.DEFAULT_TYPE_KEY);
221221
assertThat(bucket.get(DefaultRedisTypeMapper.DEFAULT_TYPE_KEY)).isEqualTo(expected);

src/test/java/org/springframework/data/redis/listener/PubSubAwaitUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ public static void runAndAwaitChannelSubscription(RedisConnectionFactory connect
8787

8888
private static long numPat(RedisConnection connection) {
8989

90-
if (connection instanceof LettuceConnection) {
91-
return ((Number) ((LettuceConnection) connection).execute("PUBSUB", new IntegerOutput<>(ByteArrayCodec.INSTANCE),
90+
if (connection instanceof LettuceConnection lettuceConnection) {
91+
return ((Number) lettuceConnection.execute("PUBSUB", new IntegerOutput<>(ByteArrayCodec.INSTANCE),
9292
"NUMPAT".getBytes())).longValue();
9393
}
9494

@@ -98,8 +98,8 @@ private static long numPat(RedisConnection connection) {
9898
private static long numSub(RedisConnection connection, String channel) {
9999

100100
List<?> pubsub;
101-
if (connection instanceof LettuceConnection) {
102-
pubsub = (List<?>) ((LettuceConnection) connection).execute("PUBSUB", new ArrayOutput<>(ByteArrayCodec.INSTANCE),
101+
if (connection instanceof LettuceConnection lettuceConnection) {
102+
pubsub = (List<?>) lettuceConnection.execute("PUBSUB", new ArrayOutput<>(ByteArrayCodec.INSTANCE),
103103
"NUMSUB".getBytes(), channel.getBytes());
104104
} else {
105105
pubsub = (List<?>) connection.execute("PUBSUB", "NUMSUB".getBytes(), channel.getBytes());

src/test/java/org/springframework/data/redis/listener/PubSubResubscribeTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ private static boolean clusterAvailable() {
236236

237237
private static boolean isClusterAware(RedisConnectionFactory connectionFactory) {
238238

239-
if (connectionFactory instanceof LettuceConnectionFactory) {
240-
return ((LettuceConnectionFactory) connectionFactory).isClusterAware();
241-
} else if (connectionFactory instanceof JedisConnectionFactory) {
242-
return ((JedisConnectionFactory) connectionFactory).isRedisClusterAware();
239+
if (connectionFactory instanceof LettuceConnectionFactory lettuceConnectionFactory) {
240+
return lettuceConnectionFactory.isClusterAware();
241+
} else if (connectionFactory instanceof JedisConnectionFactory jedisConnectionFactory) {
242+
return jedisConnectionFactory.isRedisClusterAware();
243243
}
244244
return false;
245245
}

src/test/java/org/springframework/data/redis/listener/PubSubTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ void testStartListenersToNoSpecificChannelTest() {
170170

171171
private static boolean isClusterAware(RedisConnectionFactory connectionFactory) {
172172

173-
if (connectionFactory instanceof LettuceConnectionFactory) {
174-
return ((LettuceConnectionFactory) connectionFactory).isClusterAware();
175-
} else if (connectionFactory instanceof JedisConnectionFactory) {
176-
return ((JedisConnectionFactory) connectionFactory).isRedisClusterAware();
173+
if (connectionFactory instanceof LettuceConnectionFactory lettuceConnectionFactory) {
174+
return lettuceConnectionFactory.isClusterAware();
175+
} else if (connectionFactory instanceof JedisConnectionFactory jedisConnectionFactory) {
176+
return jedisConnectionFactory.isRedisClusterAware();
177177
}
178178
return false;
179179
}

src/test/java/org/springframework/data/redis/mapping/Jackson2HashMapperIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public class Jackson2HashMapperIntegrationTests {
5454
public Jackson2HashMapperIntegrationTests(RedisConnectionFactory factory) throws Exception {
5555

5656
this.factory = factory;
57-
if (factory instanceof InitializingBean) {
58-
((InitializingBean) factory).afterPropertiesSet();
57+
if (factory instanceof InitializingBean initializingBean) {
58+
initializingBean.afterPropertiesSet();
5959
}
6060
}
6161

src/test/java/org/springframework/data/redis/repository/cdi/Person.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,9 @@ public void setName(String name) {
5050
public boolean equals(@Nullable Object o) {
5151
if (this == o)
5252
return true;
53-
if (!(o instanceof Person))
53+
if (!(o instanceof Person person))
5454
return false;
5555

56-
Person person = (Person) o;
57-
5856
if (id != null ? !id.equals(person.id) : person.id != null)
5957
return false;
6058
return name != null ? name.equals(person.name) : person.name == null;

src/test/java/org/springframework/data/redis/repository/cdi/RedisCdiDependenciesProducer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public RedisOperations<byte[], byte[]> redisOperationsProducerQualified(RedisOpe
6363

6464
public void closeRedisOperations(@Disposes RedisOperations<byte[], byte[]> redisOperations) throws Exception {
6565

66-
if (redisOperations instanceof DisposableBean) {
67-
((DisposableBean) redisOperations).destroy();
66+
if (redisOperations instanceof DisposableBean disposableBean) {
67+
disposableBean.destroy();
6868
}
6969
}
7070

src/test/java/org/springframework/data/redis/serializer/GenericJackson2JsonRedisSerializerUnitTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,10 +572,9 @@ public boolean equals(@Nullable Object obj) {
572572
if (obj == null) {
573573
return false;
574574
}
575-
if (!(obj instanceof SimpleObject)) {
575+
if (!(obj instanceof SimpleObject other)) {
576576
return false;
577577
}
578-
SimpleObject other = (SimpleObject) obj;
579578
return nullSafeEquals(this.longValue, other.longValue);
580579
}
581580
}

src/test/java/org/springframework/data/redis/stream/AbstractStreamMessageListenerContainerIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,9 @@ private int getNumberOfPending(String stream, String group) {
395395

396396
RedisConnection connection = connectionFactory.getConnection();
397397

398-
if (connection instanceof LettuceConnection) {
398+
if (connection instanceof LettuceConnection lettuceConnection) {
399399

400-
String value = ((List) ((LettuceConnection) connectionFactory.getConnection()).execute("XPENDING",
400+
String value = ((List) lettuceConnection.execute("XPENDING",
401401
new NestedMultiOutput<>(StringCodec.UTF8), new byte[][] { stream.getBytes(), group.getBytes() })).get(0)
402402
.toString();
403403
return NumberUtils.parseNumber(value, Integer.class);

src/test/java/org/springframework/data/redis/support/BoundKeyOperationsIntegrationTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,14 @@ void testPersist() throws Exception {
119119

120120
@SuppressWarnings({ "unchecked", "rawtypes" })
121121
private void populateBoundKey() {
122-
if (keyOps instanceof Collection) {
123-
((Collection) keyOps).add("dummy");
124-
} else if (keyOps instanceof Map) {
125-
((Map) keyOps).put("dummy", "dummy");
126-
} else if (keyOps instanceof RedisAtomicInteger) {
127-
((RedisAtomicInteger) keyOps).set(42);
128-
} else if (keyOps instanceof RedisAtomicLong) {
129-
((RedisAtomicLong) keyOps).set(42L);
122+
if (keyOps instanceof Collection collection) {
123+
collection.add("dummy");
124+
} else if (keyOps instanceof Map map) {
125+
map.put("dummy", "dummy");
126+
} else if (keyOps instanceof RedisAtomicInteger redisAtomicInteger) {
127+
redisAtomicInteger.set(42);
128+
} else if (keyOps instanceof RedisAtomicLong redisAtomicLong) {
129+
redisAtomicLong.set(42L);
130130
}
131131
}
132132
}

src/test/java/org/springframework/data/redis/test/util/CollectionAwareComparator.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ public enum CollectionAwareComparator implements Comparator<Object> {
3434
@Override
3535
public int compare(Object o1, Object o2) {
3636

37-
if (o1 instanceof Collection && o2 instanceof Collection) {
38-
39-
Collection<?> c1 = (Collection<?>) o1;
40-
Collection<?> c2 = (Collection<?>) o2;
37+
if (o1 instanceof Collection<?> c1 && o2 instanceof Collection<?> c2) {
4138

4239
if (c1.size() != c2.size()) {
4340
return 1;

0 commit comments

Comments
 (0)