Skip to content

Commit cd16375

Browse files
committed
Polishing.
Replace consecutive if-statements with loop. Original pull request: #4545 See #4542
1 parent e78e197 commit cd16375

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadataPostProcessor.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.support;
1717

18+
import java.lang.reflect.AnnotatedElement;
1819
import java.lang.reflect.Method;
1920
import java.util.HashSet;
2021
import java.util.Optional;
@@ -179,28 +180,25 @@ static class DefaultCrudMethodMetadata implements CrudMethodMetadata {
179180
*/
180181
DefaultCrudMethodMetadata(Class<?> repositoryInterface, Method method) {
181182

183+
Assert.notNull(repositoryInterface, "Repository interface must not be null");
182184
Assert.notNull(method, "Method must not be null");
183185

184-
this.readPreference = findReadPreference(repositoryInterface, method);
186+
this.readPreference = findReadPreference(method, repositoryInterface);
185187
}
186188

187-
private Optional<ReadPreference> findReadPreference(Class<?> repositoryInterface, Method method) {
189+
private static Optional<ReadPreference> findReadPreference(AnnotatedElement... annotatedElements) {
188190

189-
org.springframework.data.mongodb.repository.ReadPreference preference = AnnotatedElementUtils
190-
.findMergedAnnotation(method, org.springframework.data.mongodb.repository.ReadPreference.class);
191+
for (AnnotatedElement element : annotatedElements) {
191192

192-
if (preference == null) {
193+
org.springframework.data.mongodb.repository.ReadPreference preference = AnnotatedElementUtils
194+
.findMergedAnnotation(element, org.springframework.data.mongodb.repository.ReadPreference.class);
193195

194-
preference = AnnotatedElementUtils.findMergedAnnotation(repositoryInterface,
195-
org.springframework.data.mongodb.repository.ReadPreference.class);
196-
}
197-
198-
if (preference == null) {
199-
return Optional.empty();
196+
if (preference != null) {
197+
return Optional.of(com.mongodb.ReadPreference.valueOf(preference.value()));
198+
}
200199
}
201200

202-
return Optional.of(com.mongodb.ReadPreference.valueOf(preference.value()));
203-
201+
return Optional.empty();
204202
}
205203

206204
@Override

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/DefaultCrudMethodMetadataUnitTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.springframework.util.ReflectionUtils;
2828

2929
/**
30+
* Unit tests for {@link DefaultCrudMethodMetadata}.
31+
*
3032
* @author Christoph Strobl
3133
*/
3234
class DefaultCrudMethodMetadataUnitTests {

0 commit comments

Comments
 (0)