diff --git a/src/main/java/io/lettuce/core/dynamic/codec/AnnotationRedisCodecResolver.java b/src/main/java/io/lettuce/core/dynamic/codec/AnnotationRedisCodecResolver.java index 5fa2a549b0..94d9f87a04 100644 --- a/src/main/java/io/lettuce/core/dynamic/codec/AnnotationRedisCodecResolver.java +++ b/src/main/java/io/lettuce/core/dynamic/codec/AnnotationRedisCodecResolver.java @@ -33,9 +33,10 @@ * determine a {@link RedisCodec} that is able to handle all involved types. * * @author Mark Paluch + * @author Manyanda Chitimbo + * @since 5.0 * @see Key * @see Value - * @since 5.0 */ public class AnnotationRedisCodecResolver implements RedisCodecResolver { @@ -71,7 +72,7 @@ public AnnotationRedisCodecResolver(List> codecs) { return codecs.get(0); } - if (methodHasAtMostOneCodec(keyTypes, valueTypes)) { + if ((keyTypes.size() == 1 && hasAtMostOne(valueTypes)) || (valueTypes.size() == 1 && hasAtMostOne(keyTypes))) { RedisCodec resolvedCodec = resolveCodec(keyTypes, valueTypes); if (resolvedCodec != null) { return resolvedCodec; @@ -81,11 +82,8 @@ public AnnotationRedisCodecResolver(List> codecs) { throw new IllegalStateException(String.format("Cannot resolve Codec for method %s", commandMethod.getMethod())); } - private boolean methodHasAtMostOneCodec(Set> keyTypes, Set> valueTypes) { - final int keyTypesSize = keyTypes.size(); - final int valueTypesSize = valueTypes.size(); - - return keyTypesSize == 1 && valueTypesSize <= 1 || valueTypesSize == 1 && keyTypesSize <= 1; + private boolean hasAtMostOne(Collection collection) { + return collection.size() <= 1; } private Voted> voteForTypeMajority(CommandMethod commandMethod) { diff --git a/src/test/java/io/lettuce/core/dynamic/codec/AnnotationRedisCodecResolverUnitTests.java b/src/test/java/io/lettuce/core/dynamic/codec/AnnotationRedisCodecResolverUnitTests.java index ffe51f61d0..8a7f6ab946 100644 --- a/src/test/java/io/lettuce/core/dynamic/codec/AnnotationRedisCodecResolverUnitTests.java +++ b/src/test/java/io/lettuce/core/dynamic/codec/AnnotationRedisCodecResolverUnitTests.java @@ -37,7 +37,10 @@ import io.lettuce.core.dynamic.support.ReflectionUtils; /** + * Unit tests for {@link AnnotationRedisCodecResolver}. + * * @author Mark Paluch + * @author Manyanda Chitimbo */ class AnnotationRedisCodecResolverUnitTests { @@ -91,13 +94,14 @@ void shouldResolveHintedByteArrayValue() { @Test void resolutionOfMethodWithMixedTypesShouldFail() { Method method = ReflectionUtils.findMethod(CommandMethods.class, "mixedTypes", String.class, byte[].class); - assertThatThrownBy(() -> resolve(method)).isInstanceOf(IllegalStateException. class); + assertThatThrownBy(() -> resolve(method)).isInstanceOf(IllegalStateException.class); } @Test void resolutionOfMethodWithMixedCodecsShouldFail() { - Method method = ReflectionUtils.findMethod(CommandMethods.class, "mixedCodecs", String.class, byte[].class, String.class); - assertThatThrownBy(() -> resolve(method)).isInstanceOf(IllegalStateException. class); + Method method = ReflectionUtils.findMethod(CommandMethods.class, "mixedCodecs", String.class, byte[].class, + String.class); + assertThatThrownBy(() -> resolve(method)).isInstanceOf(IllegalStateException.class); } @Test