Skip to content

Commit 74b07e5

Browse files
mp911dechristophstrobl
authored andcommitted
Polishing.
Tweak documentation wording. Refine constructor nullability rules. Fix javadoc references. Original Pull Request: #4503
1 parent 715231e commit 74b07e5

File tree

6 files changed

+79
-67
lines changed

6 files changed

+79
-67
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
/**
2929
* The {@link Aggregation} annotation can be used to annotate a {@link org.springframework.data.repository.Repository}
3030
* query method so that it runs the {@link Aggregation#pipeline()} on invocation.
31-
* <br />
31+
* <p>
3232
* Pipeline stages are mapped against the {@link org.springframework.data.repository.Repository} domain type to consider
3333
* {@link org.springframework.data.mongodb.core.mapping.Field field} mappings and may contain simple placeholders
3434
* {@code ?0} as well as {@link org.springframework.expression.spel.standard.SpelExpression SpelExpressions}.
35-
* <br />
35+
* <p>
3636
* Query method {@link org.springframework.data.domain.Sort} and {@link org.springframework.data.domain.Pageable}
3737
* arguments are applied at the end of the pipeline or can be defined manually as part of it.
3838
*
@@ -130,8 +130,8 @@
130130
String collation() default "";
131131

132132
/**
133-
* The mode of the read preference to use. <br />
134-
* {@code @Aggregation(pipeline = { ... }, readPreference = "secondary")} can be used as shortcut for:
133+
* The mode of the read preference to use. This attribute ({@code @Aggregation(pipeline = { ... }, readPreference =
134+
* "secondary")}) is an alias for:
135135
*
136136
* <pre class="code">
137137
* &#64;@Aggregation(pipeline = { ... })

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@
8585
boolean delete() default false;
8686

8787
/**
88-
* Defines a default sort order for the given query.<br />
89-
* <strong>NOTE:</strong> The so set defaults can be altered / overwritten using an explicit
90-
* {@link org.springframework.data.domain.Sort} argument of the query method.
88+
* Defines a default sort order for the given query. <strong>NOTE:</strong> The so set defaults can be altered /
89+
* overwritten using an explicit {@link org.springframework.data.domain.Sort} argument of the query method.
9190
*
9291
* <pre>
9392
* <code>
@@ -134,8 +133,7 @@
134133
String collation() default "";
135134

136135
/**
137-
* The name of the index to use. <br />
138-
* {@code @Query(value = "...", hint = "lastname-idx")} can be used as shortcut for:
136+
* The name of the index to use. {@code @Query(value = "...", hint = "lastname-idx")} can be used as shortcut for:
139137
*
140138
* <pre class="code">
141139
* &#64;Query(...)
@@ -151,8 +149,8 @@
151149
String hint() default "";
152150

153151
/**
154-
* The mode of the read preference to use. <br />
155-
* {@code @Query(value = "...", readPreference = "secondary")} can be used as shortcut for:
152+
* The mode of the read preference to use. This attribute
153+
* ({@code @Query(value = "...", readPreference = "secondary")}) is an alias for:
156154
*
157155
* <pre class="code">
158156
* &#64;Query(...)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@
2727
* @author Jorge Rodríguez
2828
* @author Christoph Strobl
2929
* @since 4.2
30+
* @see com.mongodb.ReadPreference
3031
*/
3132
@Retention(RetentionPolicy.RUNTIME)
32-
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
33+
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
3334
@Documented
3435
public @interface ReadPreference {
3536

3637
/**
37-
* Configure read preference mode
38-
* @return read preference mode
38+
* Configure the read preference mode.
39+
*
40+
* @return read preference mode.
3941
*/
4042
String value() default "";
4143

4244
}
43-

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,9 @@ public org.springframework.data.mongodb.core.query.Meta getQueryMetaAttributes()
293293
}
294294

295295
/**
296-
* Check if the query method is decorated with an non empty {@link Query#sort()}.
296+
* Check if the query method is decorated with a non-empty {@link Query#sort()}.
297297
*
298-
* @return true if method annotated with {@link Query} having an non empty sort attribute.
298+
* @return true if method annotated with {@link Query} having a non-empty sort attribute.
299299
* @since 2.1
300300
*/
301301
public boolean hasAnnotatedSort() {
@@ -316,9 +316,8 @@ public String getAnnotatedSort() {
316316
"Expected to find @Query annotation but did not; Make sure to check hasAnnotatedSort() before."));
317317
}
318318

319-
320319
/**
321-
* Check if the query method is decorated with an non empty {@link ReadPreference}.
320+
* Check if the query method is decorated with a non-empty {@link ReadPreference}.
322321
*
323322
* @return true if method annotated with {@link Query} or {@link Aggregation} having a non-empty collation attribute.
324323
* @since 4.2
@@ -332,18 +331,20 @@ public boolean hasAnnotatedReadPreference() {
332331
*
333332
* @return the name of the {@link ReadPreference()}.
334333
* @throws IllegalStateException if method not annotated with {@link Query}. Make sure to check
335-
* {@link #hasAnnotatedQuery()} first.
334+
* {@link #hasAnnotatedReadPreference()} first.
336335
* @since 4.2
337336
*/
338337
public String getAnnotatedReadPreference() {
339338

340339
return doFindReadPreferenceAnnotation().map(ReadPreference::value).orElseThrow(() -> new IllegalStateException(
341-
"Expected to find @ReadPreference annotation but did not; Make sure to check hasAnnotatedReadPreference() before."));
340+
"Expected to find @ReadPreference annotation but did not; Make sure to check hasAnnotatedReadPreference() before."));
342341
}
343342

344343
/**
345-
* Get {@link com.mongodb.ReadPreference#getName() name} from query. First check if the method is annotated. If not, check if the class is annotated.
346-
* So if the method and the class are annotated with @ReadPreference, the method annotation takes precedence.
344+
* Get {@link com.mongodb.ReadPreference#getName() name} from query. First check if the method is annotated. If not,
345+
* check if the class is annotated. So if the method and the class are annotated with @ReadPreference, the method
346+
* annotation takes precedence.
347+
*
347348
* @return the {@link ReadPreference}
348349
* @since 4.2
349350
*/
@@ -352,7 +353,7 @@ private Optional<ReadPreference> doFindReadPreferenceAnnotation() {
352353
}
353354

354355
/**
355-
* Check if the query method is decorated with an non empty {@link Query#collation()} or or
356+
* Check if the query method is decorated with a non-empty {@link Query#collation()} or
356357
* {@link Aggregation#collation()}.
357358
*
358359
* @return true if method annotated with {@link Query} or {@link Aggregation} having a non-empty collation attribute.
@@ -365,7 +366,7 @@ public boolean hasAnnotatedCollation() {
365366
/**
366367
* Get the collation value extracted from the {@link Query} or {@link Aggregation} annotation.
367368
*
368-
* @return the {@link Query#collation()} or or {@link Aggregation#collation()} value.
369+
* @return the {@link Query#collation()} or {@link Aggregation#collation()} value.
369370
* @throws IllegalStateException if method not annotated with {@link Query} or {@link Aggregation}. Make sure to check
370371
* {@link #hasAnnotatedQuery()} first.
371372
* @since 2.2
@@ -443,9 +444,10 @@ private <A extends Annotation> Optional<A> doFindAnnotation(Class<A> annotationT
443444
}
444445

445446
@SuppressWarnings("unchecked")
446-
private <A extends Annotation> Optional<A> doFindAnnotationInClass(Class<A> annotationType) {
447+
private <A extends Annotation> Optional<A> doFindAnnotationInClass(Class<A> annotationType) {
447448

448-
Optional<Annotation> mergedAnnotation = Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), annotationType));
449+
Optional<Annotation> mergedAnnotation = Optional
450+
.ofNullable(AnnotatedElementUtils.findMergedAnnotation(method.getDeclaringClass(), annotationType));
449451
annotationCache.put(annotationType, mergedAnnotation);
450452

451453
return (Optional<A>) mergedAnnotation;

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,22 @@
2727
import java.util.stream.Collectors;
2828
import java.util.stream.Stream;
2929

30-
import com.mongodb.ReadPreference;
3130
import org.springframework.core.annotation.AnnotatedElementUtils;
3231
import org.springframework.dao.OptimisticLockingFailureException;
3332
import org.springframework.data.domain.Example;
3433
import org.springframework.data.domain.Page;
3534
import org.springframework.data.domain.PageImpl;
3635
import org.springframework.data.domain.Pageable;
37-
import org.springframework.data.domain.Window;
3836
import org.springframework.data.domain.ScrollPosition;
3937
import org.springframework.data.domain.Sort;
38+
import org.springframework.data.domain.Window;
4039
import org.springframework.data.mongodb.core.ExecutableFindOperation;
4140
import org.springframework.data.mongodb.core.MongoOperations;
4241
import org.springframework.data.mongodb.core.MongoTemplate;
43-
import org.springframework.data.mongodb.core.ReadPreferenceAware;
4442
import org.springframework.data.mongodb.core.query.Criteria;
4543
import org.springframework.data.mongodb.core.query.Query;
4644
import org.springframework.data.mongodb.repository.MongoRepository;
45+
import org.springframework.data.mongodb.repository.ReadPreference;
4746
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
4847
import org.springframework.data.repository.core.RepositoryMetadata;
4948
import org.springframework.data.support.PageableExecutionUtils;
@@ -67,45 +66,51 @@
6766
*/
6867
public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
6968

70-
private final @Nullable RepositoryMetadata repositoryMetadata;
7169
private final MongoEntityInformation<T, ID> entityInformation;
7270
private final MongoOperations mongoOperations;
73-
private final Lazy<ReadPreference> readPreference;
71+
private final Lazy<com.mongodb.ReadPreference> readPreference;
72+
7473
/**
7574
* Creates a new {@link SimpleMongoRepository} for the given {@link MongoEntityInformation} and {@link MongoTemplate}.
7675
*
7776
* @param metadata must not be {@literal null}.
7877
* @param mongoOperations must not be {@literal null}.
7978
*/
8079
public SimpleMongoRepository(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
81-
this(null, metadata, mongoOperations);
80+
this(null, metadata, mongoOperations, null);
8281
}
8382

8483
/**
8584
* Creates a new {@link SimpleMongoRepository} for the given {@link MongoEntityInformation} and {@link MongoTemplate}.
8685
*
87-
* @param repositoryMetadata
86+
* @param repositoryMetadata must not be {@literal null}.
8887
* @param metadata must not be {@literal null}.
8988
* @param mongoOperations must not be {@literal null}.
9089
* @since 4.2
9190
*/
92-
public SimpleMongoRepository(@Nullable RepositoryMetadata repositoryMetadata, MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
91+
public SimpleMongoRepository(RepositoryMetadata repositoryMetadata, MongoEntityInformation<T, ID> metadata,
92+
MongoOperations mongoOperations) {
93+
this(repositoryMetadata, metadata, mongoOperations, null);
94+
}
95+
96+
private SimpleMongoRepository(@Nullable RepositoryMetadata repositoryMetadata, MongoEntityInformation<T, ID> metadata,
97+
MongoOperations mongoOperations, @Nullable Object marker) {
9398

9499
Assert.notNull(metadata, "MongoEntityInformation must not be null");
95100
Assert.notNull(mongoOperations, "MongoOperations must not be null");
96101

97-
this.repositoryMetadata = repositoryMetadata;
98102
this.entityInformation = metadata;
99103
this.mongoOperations = mongoOperations;
100104

101-
this.readPreference = repositoryMetadata == null ? Lazy.empty() : Lazy.of(() -> {
102-
org.springframework.data.mongodb.repository.ReadPreference preference = AnnotatedElementUtils.findMergedAnnotation(repositoryMetadata.getRepositoryInterface(), org.springframework.data.mongodb.repository.ReadPreference.class);
103-
if (preference == null) {
104-
return null;
105-
}
106-
return ReadPreference.valueOf(preference.value());
107-
}
108-
);
105+
this.readPreference = repositoryMetadata == null ? Lazy.empty() : Lazy.of(() -> {
106+
ReadPreference preference = AnnotatedElementUtils
107+
.findMergedAnnotation(repositoryMetadata.getRepositoryInterface(), ReadPreference.class);
108+
109+
if (preference == null) {
110+
return null;
111+
}
112+
return com.mongodb.ReadPreference.valueOf(preference.value());
113+
});
109114
}
110115

111116
// -------------------------------------------------------------------------

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

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717

1818
import static org.springframework.data.mongodb.core.query.Criteria.*;
1919

20-
import com.mongodb.ReadPreference;
21-
import org.springframework.core.annotation.AnnotatedElementUtils;
22-
import org.springframework.data.mongodb.core.MongoTemplate;
23-
import org.springframework.data.repository.core.RepositoryMetadata;
24-
import org.springframework.data.util.Lazy;
25-
import org.springframework.lang.Nullable;
2620
import reactor.core.publisher.Flux;
2721
import reactor.core.publisher.Mono;
2822

@@ -35,23 +29,29 @@
3529
import java.util.stream.Collectors;
3630

3731
import org.reactivestreams.Publisher;
32+
import org.springframework.core.annotation.AnnotatedElementUtils;
3833
import org.springframework.dao.IncorrectResultSizeDataAccessException;
3934
import org.springframework.dao.OptimisticLockingFailureException;
4035
import org.springframework.data.domain.Example;
4136
import org.springframework.data.domain.Page;
4237
import org.springframework.data.domain.Pageable;
43-
import org.springframework.data.domain.Window;
4438
import org.springframework.data.domain.ScrollPosition;
4539
import org.springframework.data.domain.Sort;
40+
import org.springframework.data.domain.Window;
41+
import org.springframework.data.mongodb.core.MongoTemplate;
4642
import org.springframework.data.mongodb.core.ReactiveFindOperation;
4743
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
4844
import org.springframework.data.mongodb.core.query.Criteria;
4945
import org.springframework.data.mongodb.core.query.Query;
5046
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
47+
import org.springframework.data.mongodb.repository.ReadPreference;
5148
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
49+
import org.springframework.data.repository.core.RepositoryMetadata;
5250
import org.springframework.data.repository.query.FluentQuery;
51+
import org.springframework.data.util.Lazy;
5352
import org.springframework.data.util.StreamUtils;
5453
import org.springframework.data.util.Streamable;
54+
import org.springframework.lang.Nullable;
5555
import org.springframework.util.Assert;
5656

5757
import com.mongodb.client.result.DeleteResult;
@@ -69,42 +69,48 @@
6969
*/
7070
public class SimpleReactiveMongoRepository<T, ID extends Serializable> implements ReactiveMongoRepository<T, ID> {
7171

72-
private final @Nullable RepositoryMetadata repositoryMetadata;
7372
private final MongoEntityInformation<T, ID> entityInformation;
7473
private final ReactiveMongoOperations mongoOperations;
75-
private final Lazy<ReadPreference> readPreference;
74+
private final Lazy<com.mongodb.ReadPreference> readPreference;
7675

7776
public SimpleReactiveMongoRepository(MongoEntityInformation<T, ID> entityInformation,
7877
ReactiveMongoOperations mongoOperations) {
79-
this(null, entityInformation, mongoOperations);
78+
this(null, entityInformation, mongoOperations, null);
8079
}
8180

8281
/**
83-
* Creates a new {@link SimpleReactiveMongoRepository} for the given {@link MongoEntityInformation} and {@link MongoTemplate}.
82+
* Creates a new {@link SimpleReactiveMongoRepository} for the given {@link MongoEntityInformation} and
83+
* {@link MongoTemplate}.
8484
*
85-
* @param repositoryMetadata
85+
* @param repositoryMetadata must not be {@literal null}.
8686
* @param entityInformation must not be {@literal null}.
8787
* @param mongoOperations must not be {@literal null}.
8888
* @since 4.2
8989
*/
90-
public SimpleReactiveMongoRepository(@Nullable RepositoryMetadata repositoryMetadata, MongoEntityInformation<T, ID> entityInformation,
91-
ReactiveMongoOperations mongoOperations) {
90+
public SimpleReactiveMongoRepository(RepositoryMetadata repositoryMetadata,
91+
MongoEntityInformation<T, ID> entityInformation, ReactiveMongoOperations mongoOperations) {
92+
this(repositoryMetadata, entityInformation, mongoOperations, null);
93+
}
94+
95+
private SimpleReactiveMongoRepository(@Nullable RepositoryMetadata repositoryMetadata,
96+
MongoEntityInformation<T, ID> entityInformation, ReactiveMongoOperations mongoOperations,
97+
@Nullable Object marker) {
9298

9399
Assert.notNull(entityInformation, "EntityInformation must not be null");
94100
Assert.notNull(mongoOperations, "MongoOperations must not be null");
95101

96-
this.repositoryMetadata = repositoryMetadata;
97102
this.entityInformation = entityInformation;
98103
this.mongoOperations = mongoOperations;
99104

100-
this.readPreference = repositoryMetadata == null ? Lazy.empty() : Lazy.of(() -> {
101-
org.springframework.data.mongodb.repository.ReadPreference preference = AnnotatedElementUtils.findMergedAnnotation(repositoryMetadata.getRepositoryInterface(), org.springframework.data.mongodb.repository.ReadPreference.class);
102-
if (preference == null) {
103-
return null;
104-
}
105-
return ReadPreference.valueOf(preference.value());
106-
}
107-
);
105+
this.readPreference = repositoryMetadata == null ? Lazy.empty() : Lazy.of(() -> {
106+
107+
ReadPreference preference = AnnotatedElementUtils
108+
.findMergedAnnotation(repositoryMetadata.getRepositoryInterface(), ReadPreference.class);
109+
if (preference == null) {
110+
return null;
111+
}
112+
return com.mongodb.ReadPreference.valueOf(preference.value());
113+
});
108114
}
109115

110116
// -------------------------------------------------------------------------

0 commit comments

Comments
 (0)