Skip to content

Commit

Permalink
Polishing #1149
Browse files Browse the repository at this point in the history
Add author tags. Remove final from local variables. Extract atMostOne check into method.

Original pull request: #1150.
  • Loading branch information
mp911de committed Oct 16, 2019
1 parent b6156bb commit 1f924f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -71,7 +72,7 @@ public AnnotationRedisCodecResolver(List<RedisCodec<?, ?>> 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;
Expand All @@ -81,11 +82,8 @@ public AnnotationRedisCodecResolver(List<RedisCodec<?, ?>> codecs) {
throw new IllegalStateException(String.format("Cannot resolve Codec for method %s", commandMethod.getMethod()));
}

private boolean methodHasAtMostOneCodec(Set<Class<?>> keyTypes, Set<Class<?>> 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<RedisCodec<?, ?>> voteForTypeMajority(CommandMethod commandMethod) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
import io.lettuce.core.dynamic.support.ReflectionUtils;

/**
* Unit tests for {@link AnnotationRedisCodecResolver}.
*
* @author Mark Paluch
* @author Manyanda Chitimbo
*/
class AnnotationRedisCodecResolverUnitTests {

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1f924f1

Please sign in to comment.