Consistently use instanceof pattern variable.

Closes #2967
Original pull request: #2971
This commit is contained in:
arefbehboudi
2024-08-18 10:45:30 +03:30
committed by Mark Paluch
parent 5a0897e6f7
commit 96a1b0c327
14 changed files with 49 additions and 56 deletions

View File

@@ -55,8 +55,7 @@ public abstract class AbstractConnectionUnitTestBase<T> {
private ParameterizedType resolveReturnedClassFromGernericType(Class<?> clazz) {
Object genericSuperclass = clazz.getGenericSuperclass();
if (genericSuperclass instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
if (genericSuperclass instanceof ParameterizedType parameterizedType) {
Type rawtype = parameterizedType.getRawType();
if (AbstractConnectionUnitTestBase.class.equals(rawtype)) {
return parameterizedType;

View File

@@ -52,12 +52,12 @@ public abstract class LettuceReactiveClusterTestSupport {
if (nativeCommands != null) {
nativeCommands.flushall();
if (nativeCommands instanceof RedisCommands) {
((RedisCommands) nativeCommands).getStatefulConnection().close();
if (nativeCommands instanceof RedisCommands redisCommands) {
redisCommands.getStatefulConnection().close();
}
if (nativeCommands instanceof RedisAdvancedClusterCommands) {
((RedisAdvancedClusterCommands) nativeCommands).getStatefulConnection().close();
if (nativeCommands instanceof RedisAdvancedClusterCommands redisAdvancedClusterCommands) {
redisAdvancedClusterCommands.getStatefulConnection().close();
}
}

View File

@@ -155,16 +155,16 @@ public abstract class LettuceReactiveCommandsTestSupport {
public void close() throws IOException {
try {
if (connectionProvider instanceof DisposableBean) {
((DisposableBean) connectionProvider).destroy();
if (connectionProvider instanceof DisposableBean disposableBean) {
disposableBean.destroy();
}
if (nativeConnectionProvider instanceof DisposableBean) {
((DisposableBean) nativeConnectionProvider).destroy();
if (nativeConnectionProvider instanceof DisposableBean disposableBean) {
disposableBean.destroy();
}
if (nativeBinaryConnectionProvider instanceof DisposableBean) {
((DisposableBean) nativeBinaryConnectionProvider).destroy();
if (nativeBinaryConnectionProvider instanceof DisposableBean disposableBean) {
disposableBean.destroy();
}
} catch (Exception ex) {
throw new RuntimeException(ex);
@@ -198,21 +198,21 @@ public abstract class LettuceReactiveCommandsTestSupport {
if (nativeCommands != null) {
flushAll();
if (nativeCommands instanceof RedisCommands) {
nativeConnectionProvider.release(((RedisCommands) nativeCommands).getStatefulConnection());
if (nativeCommands instanceof RedisCommands redisCommands) {
nativeConnectionProvider.release((redisCommands).getStatefulConnection());
}
if (nativeCommands instanceof RedisAdvancedClusterCommands) {
nativeConnectionProvider.release(((RedisAdvancedClusterCommands) nativeCommands).getStatefulConnection());
if (nativeCommands instanceof RedisAdvancedClusterCommands redisAdvancedClusterCommands) {
nativeConnectionProvider.release((redisAdvancedClusterCommands).getStatefulConnection());
}
if (nativeBinaryCommands instanceof RedisCommands) {
nativeBinaryConnectionProvider.release(((RedisCommands) nativeBinaryCommands).getStatefulConnection());
if (nativeBinaryCommands instanceof RedisCommands redisCommands) {
nativeBinaryConnectionProvider.release((redisCommands).getStatefulConnection());
}
if (nativeBinaryCommands instanceof RedisAdvancedClusterCommands) {
if (nativeBinaryCommands instanceof RedisAdvancedClusterCommands redisAdvancedClusterCommands) {
nativeBinaryConnectionProvider
.release(((RedisAdvancedClusterCommands) nativeBinaryCommands).getStatefulConnection());
.release((redisAdvancedClusterCommands).getStatefulConnection());
}
}

View File

@@ -215,7 +215,7 @@ class DefaultRedisTypeMapperUnitTests {
assertThat(bucket.keySet()).isEmpty();
} else {
byte[] expected = value instanceof Class ? ((Class) value).getName().getBytes() : value.toString().getBytes();
byte[] expected = value instanceof Class javaClass ? javaClass.getName().getBytes() : value.toString().getBytes();
assertThat(bucket.asMap()).containsKey(DefaultRedisTypeMapper.DEFAULT_TYPE_KEY);
assertThat(bucket.get(DefaultRedisTypeMapper.DEFAULT_TYPE_KEY)).isEqualTo(expected);

View File

@@ -87,8 +87,8 @@ class PubSubAwaitUtil {
private static long numPat(RedisConnection connection) {
if (connection instanceof LettuceConnection) {
return ((Number) ((LettuceConnection) connection).execute("PUBSUB", new IntegerOutput<>(ByteArrayCodec.INSTANCE),
if (connection instanceof LettuceConnection lettuceConnection) {
return ((Number) lettuceConnection.execute("PUBSUB", new IntegerOutput<>(ByteArrayCodec.INSTANCE),
"NUMPAT".getBytes())).longValue();
}
@@ -98,8 +98,8 @@ class PubSubAwaitUtil {
private static long numSub(RedisConnection connection, String channel) {
List<?> pubsub;
if (connection instanceof LettuceConnection) {
pubsub = (List<?>) ((LettuceConnection) connection).execute("PUBSUB", new ArrayOutput<>(ByteArrayCodec.INSTANCE),
if (connection instanceof LettuceConnection lettuceConnection) {
pubsub = (List<?>) lettuceConnection.execute("PUBSUB", new ArrayOutput<>(ByteArrayCodec.INSTANCE),
"NUMSUB".getBytes(), channel.getBytes());
} else {
pubsub = (List<?>) connection.execute("PUBSUB", "NUMSUB".getBytes(), channel.getBytes());

View File

@@ -236,10 +236,10 @@ public class PubSubResubscribeTests {
private static boolean isClusterAware(RedisConnectionFactory connectionFactory) {
if (connectionFactory instanceof LettuceConnectionFactory) {
return ((LettuceConnectionFactory) connectionFactory).isClusterAware();
} else if (connectionFactory instanceof JedisConnectionFactory) {
return ((JedisConnectionFactory) connectionFactory).isRedisClusterAware();
if (connectionFactory instanceof LettuceConnectionFactory lettuceConnectionFactory) {
return lettuceConnectionFactory.isClusterAware();
} else if (connectionFactory instanceof JedisConnectionFactory jedisConnectionFactory) {
return jedisConnectionFactory.isRedisClusterAware();
}
return false;
}

View File

@@ -170,10 +170,10 @@ public class PubSubTests<T> {
private static boolean isClusterAware(RedisConnectionFactory connectionFactory) {
if (connectionFactory instanceof LettuceConnectionFactory) {
return ((LettuceConnectionFactory) connectionFactory).isClusterAware();
} else if (connectionFactory instanceof JedisConnectionFactory) {
return ((JedisConnectionFactory) connectionFactory).isRedisClusterAware();
if (connectionFactory instanceof LettuceConnectionFactory lettuceConnectionFactory) {
return lettuceConnectionFactory.isClusterAware();
} else if (connectionFactory instanceof JedisConnectionFactory jedisConnectionFactory) {
return jedisConnectionFactory.isRedisClusterAware();
}
return false;
}

View File

@@ -54,8 +54,8 @@ public class Jackson2HashMapperIntegrationTests {
public Jackson2HashMapperIntegrationTests(RedisConnectionFactory factory) throws Exception {
this.factory = factory;
if (factory instanceof InitializingBean) {
((InitializingBean) factory).afterPropertiesSet();
if (factory instanceof InitializingBean initializingBean) {
initializingBean.afterPropertiesSet();
}
}

View File

@@ -50,11 +50,9 @@ class Person {
public boolean equals(@Nullable Object o) {
if (this == o)
return true;
if (!(o instanceof Person))
if (!(o instanceof Person person))
return false;
Person person = (Person) o;
if (id != null ? !id.equals(person.id) : person.id != null)
return false;
return name != null ? name.equals(person.name) : person.name == null;

View File

@@ -63,8 +63,8 @@ public class RedisCdiDependenciesProducer {
public void closeRedisOperations(@Disposes RedisOperations<byte[], byte[]> redisOperations) throws Exception {
if (redisOperations instanceof DisposableBean) {
((DisposableBean) redisOperations).destroy();
if (redisOperations instanceof DisposableBean disposableBean) {
disposableBean.destroy();
}
}

View File

@@ -614,10 +614,9 @@ class GenericJackson2JsonRedisSerializerUnitTests {
if (obj == null) {
return false;
}
if (!(obj instanceof SimpleObject)) {
if (!(obj instanceof SimpleObject other)) {
return false;
}
SimpleObject other = (SimpleObject) obj;
return nullSafeEquals(this.longValue, other.longValue);
}
}

View File

@@ -395,9 +395,9 @@ abstract class AbstractStreamMessageListenerContainerIntegrationTests {
RedisConnection connection = connectionFactory.getConnection();
if (connection instanceof LettuceConnection) {
if (connection instanceof LettuceConnection lettuceConnection) {
String value = ((List) ((LettuceConnection) connectionFactory.getConnection()).execute("XPENDING",
String value = ((List) lettuceConnection.execute("XPENDING",
new NestedMultiOutput<>(StringCodec.UTF8), new byte[][] { stream.getBytes(), group.getBytes() })).get(0)
.toString();
return NumberUtils.parseNumber(value, Integer.class);

View File

@@ -119,14 +119,14 @@ public class BoundKeyOperationsIntegrationTests {
@SuppressWarnings({ "unchecked", "rawtypes" })
private void populateBoundKey() {
if (keyOps instanceof Collection) {
((Collection) keyOps).add("dummy");
} else if (keyOps instanceof Map) {
((Map) keyOps).put("dummy", "dummy");
} else if (keyOps instanceof RedisAtomicInteger) {
((RedisAtomicInteger) keyOps).set(42);
} else if (keyOps instanceof RedisAtomicLong) {
((RedisAtomicLong) keyOps).set(42L);
if (keyOps instanceof Collection collection) {
collection.add("dummy");
} else if (keyOps instanceof Map map) {
map.put("dummy", "dummy");
} else if (keyOps instanceof RedisAtomicInteger redisAtomicInteger) {
redisAtomicInteger.set(42);
} else if (keyOps instanceof RedisAtomicLong redisAtomicLong) {
redisAtomicLong.set(42L);
}
}
}

View File

@@ -34,10 +34,7 @@ public enum CollectionAwareComparator implements Comparator<Object> {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Collection && o2 instanceof Collection) {
Collection<?> c1 = (Collection<?>) o1;
Collection<?> c2 = (Collection<?>) o2;
if (o1 instanceof Collection<?> c1 && o2 instanceof Collection<?> c2) {
if (c1.size() != c2.size()) {
return 1;