Skip to content

Commit 2d487b2

Browse files
committed
Polishing.
Tweak reference documentation wording. Extract self/target source dereferencing into utility methods. See: #3806 Original pull request: #3810.
1 parent 9158be9 commit 2d487b2

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DocumentReferenceSource.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public class DocumentReferenceSource {
2828

2929
private final Object self;
3030

31-
@Nullable private final Object targetSource;
31+
private final @Nullable Object targetSource;
3232

3333
/**
3434
* Create a new instance of {@link DocumentReferenceSource}.
35-
*
35+
*
3636
* @param self the entire wrapper object holding references. Must not be {@literal null}.
3737
* @param targetSource the reference value source.
3838
*/
@@ -60,4 +60,25 @@ public Object getSelf() {
6060
public Object getTargetSource() {
6161
return targetSource;
6262
}
63+
64+
/**
65+
* Dereference a {@code targetSource} if it is a {@link DocumentReferenceSource} or return {@code source} otherwise.
66+
*
67+
* @param source
68+
* @return
69+
*/
70+
@Nullable
71+
static Object getTargetSource(Object source) {
72+
return source instanceof DocumentReferenceSource ? ((DocumentReferenceSource) source).getTargetSource() : source;
73+
}
74+
75+
/**
76+
* Dereference a {@code self} object if it is a {@link DocumentReferenceSource} or return {@code self} otherwise.
77+
*
78+
* @param self
79+
* @return
80+
*/
81+
static Object getSelf(Object self) {
82+
return self instanceof DocumentReferenceSource ? ((DocumentReferenceSource) self).getSelf() : self;
83+
}
6384
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReferenceLookupDelegate.java

+4-13
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ private ReferenceCollection computeReferenceContext(MongoPersistentProperty prop
174174
* @param <T>
175175
* @return can be {@literal null}.
176176
*/
177-
@Nullable
178177
@SuppressWarnings("unchecked")
179178
private <T> T parseValueOrGet(String value, ParameterBindingContext bindingContext, Supplier<T> defaultValue) {
180179

@@ -199,16 +198,10 @@ private <T> T parseValueOrGet(String value, ParameterBindingContext bindingConte
199198

200199
ParameterBindingContext bindingContext(MongoPersistentProperty property, Object source, SpELContext spELContext) {
201200

202-
ValueProvider valueProvider;
203-
if (source instanceof DocumentReferenceSource) {
204-
valueProvider = valueProviderFor(((DocumentReferenceSource) source).getTargetSource());
205-
} else {
206-
valueProvider = valueProviderFor(source);
207-
}
201+
ValueProvider valueProvider = valueProviderFor(DocumentReferenceSource.getTargetSource(source));
208202

209203
return new ParameterBindingContext(valueProvider, spELContext.getParser(),
210204
() -> evaluationContextFor(property, source, spELContext));
211-
212205
}
213206

214207
ValueProvider valueProviderFor(Object source) {
@@ -232,8 +225,7 @@ EvaluationContext evaluationContextFor(MongoPersistentProperty property, Object
232225

233226
EvaluationContext ctx = spELContext.getEvaluationContext(target);
234227
ctx.setVariable("target", target);
235-
ctx.setVariable("self",
236-
source instanceof DocumentReferenceSource ? ((DocumentReferenceSource) source).getSelf() : source);
228+
ctx.setVariable("self", DocumentReferenceSource.getSelf(source));
237229
ctx.setVariable(property.getName(), target);
238230

239231
return ctx;
@@ -255,11 +247,10 @@ DocumentReferenceQuery computeFilter(MongoPersistentProperty property, Object so
255247

256248
String lookup = documentReference.lookup();
257249

258-
Object value = source instanceof DocumentReferenceSource ? ((DocumentReferenceSource) source).getTargetSource()
259-
: source;
250+
Object value = DocumentReferenceSource.getTargetSource(source);
260251

261252
Document sort = parseValueOrGet(documentReference.sort(), bindingContext(property, source, spELContext),
262-
() -> new Document());
253+
Document::new);
263254

264255
if (property.isCollectionLike() && (value instanceof Collection || value == null)) {
265256

src/main/asciidoc/reference/document-references.adoc

+4-3
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class Publisher {
263263
====
264264

265265
It is also possible to model relational style _One-To-Many_ references using a combination of `@ReadonlyProperty` and `@DocumentReference`.
266-
This approach allows to link types without explicitly storing the linking values within the document itself as shown in the snipped below.
266+
This approach allows link types without storing the linking values within the owning document but rather on the referencing document as shown in the example below.
267267

268268
====
269269
[source,java]
@@ -313,8 +313,9 @@ class Publisher {
313313
"name" : "Del Rey"
314314
}
315315
----
316-
<1> Set up the link from `Book` to `Publisher` by storing the `Publisher.id` within the `Book` document.
317-
<2> Mark the property holding the references to be read only. This prevents storing references to individual ``Book``s with the `Publisher` document.
316+
<1> Set up the link from `Book` (reference) to `Publisher` (owner) by storing the `Publisher.id` within the `Book` document.
317+
<2> Mark the property holding the references to be readonly.
318+
This prevents storing references to individual ``Book``s with the `Publisher` document.
318319
<3> Use the `#self` variable to access values within the `Publisher` document and in this retrieve `Books` with matching `publisherId`.
319320
====
320321

0 commit comments

Comments
 (0)