Skip to content

Commit e003edc

Browse files
committed
Improve documentation of SpelQueryContext.
Close #2693
1 parent 1d7fc40 commit e003edc

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

src/main/java/org/springframework/data/repository/query/SpelQueryContext.java

+31-15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
*/
1616
package org.springframework.data.repository.query;
1717

18+
import org.springframework.data.domain.Range;
19+
import org.springframework.data.domain.Range.Bound;
20+
import org.springframework.lang.Nullable;
21+
import org.springframework.util.Assert;
22+
1823
import java.util.ArrayList;
1924
import java.util.Arrays;
2025
import java.util.Collection;
@@ -28,13 +33,24 @@
2833
import java.util.regex.Pattern;
2934
import java.util.stream.Stream;
3035

31-
import org.springframework.data.domain.Range;
32-
import org.springframework.data.domain.Range.Bound;
33-
import org.springframework.lang.Nullable;
34-
import org.springframework.util.Assert;
35-
3636
/**
37-
* Source of {@link SpelExtractor} encapsulating configuration often common for all queries.
37+
* A {@literal SpelQueryContext} is able to find SpEL expressions in a query string and to replace them with bind variables.
38+
* <p>
39+
* Result o the parse process is a {@link SpelExtractor} which offers the transformed query string.
40+
* Alternatively and preferred one may provide a {@link QueryMethodEvaluationContextProvider} via
41+
* {@link #withEvaluationContextProvider(QueryMethodEvaluationContextProvider)} which will yield the more powerful
42+
* {@link EvaluatingSpelQueryContext}.
43+
* <p>
44+
* Typical usage looks like
45+
* <pre><code>
46+
SpelQueryContext.EvaluatingSpelQueryContext queryContext = SpelQueryContext
47+
.of((counter, expression) -> String.format("__$synthetic$__%d", counter), String::concat)
48+
.withEvaluationContextProvider(evaluationContextProvider);
49+
50+
SpelEvaluator spelEvaluator = queryContext.parse(query, queryMethod.getParameters());
51+
52+
spelEvaluator.evaluate(objects).forEach(parameterMap::addValue);
53+
* </code></pre>
3854
*
3955
* @author Jens Schauder
4056
* @author Gerrit Meier
@@ -63,7 +79,7 @@ public class SpelQueryContext {
6379
private final BiFunction<String, String, String> replacementSource;
6480

6581
private SpelQueryContext(BiFunction<Integer, String, String> parameterNameSource,
66-
BiFunction<String, String, String> replacementSource) {
82+
BiFunction<String, String, String> replacementSource) {
6783

6884
Assert.notNull(parameterNameSource, "Parameter name source must not be null");
6985
Assert.notNull(replacementSource, "Replacement source must not be null");
@@ -73,7 +89,7 @@ private SpelQueryContext(BiFunction<Integer, String, String> parameterNameSource
7389
}
7490

7591
public static SpelQueryContext of(BiFunction<Integer, String, String> parameterNameSource,
76-
BiFunction<String, String, String> replacementSource) {
92+
BiFunction<String, String, String> replacementSource) {
7793
return new SpelQueryContext(parameterNameSource, replacementSource);
7894
}
7995

@@ -83,13 +99,13 @@ public static SpelQueryContext of(BiFunction<Integer, String, String> parameterN
8399
* <pre>
84100
* &lt;prefix&gt;#{&lt;spel&gt;}
85101
* </pre>
86-
*
102+
* <p>
87103
* with prefix being the character ':' or '?'. Parsing honors quoted {@literal String}s enclosed in single or double
88104
* quotation marks.
89105
*
90106
* @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}.
91107
* @return A {@link SpelExtractor} which makes the query with SpEL expressions replaced by bind parameters and a map
92-
* from bind parameter to SpEL expression available. Guaranteed to be not {@literal null}.
108+
* from bind parameter to SpEL expression available. Guaranteed to be not {@literal null}.
93109
*/
94110
public SpelExtractor parse(String query) {
95111
return new SpelExtractor(query);
@@ -125,11 +141,11 @@ public static class EvaluatingSpelQueryContext extends SpelQueryContext {
125141
* parameter name source and replacement source.
126142
*
127143
* @param evaluationContextProvider must not be {@literal null}.
128-
* @param parameterNameSource must not be {@literal null}.
129-
* @param replacementSource must not be {@literal null}.
144+
* @param parameterNameSource must not be {@literal null}.
145+
* @param replacementSource must not be {@literal null}.
130146
*/
131147
private EvaluatingSpelQueryContext(QueryMethodEvaluationContextProvider evaluationContextProvider,
132-
BiFunction<Integer, String, String> parameterNameSource, BiFunction<String, String, String> replacementSource) {
148+
BiFunction<Integer, String, String> parameterNameSource, BiFunction<String, String, String> replacementSource) {
133149

134150
super(parameterNameSource, replacementSource);
135151

@@ -142,11 +158,11 @@ private EvaluatingSpelQueryContext(QueryMethodEvaluationContextProvider evaluati
142158
* <pre>
143159
* &lt;prefix&gt;#{&lt;spel&gt;}
144160
* </pre>
145-
*
161+
* <p>
146162
* with prefix being the character ':' or '?'. Parsing honors quoted {@literal String}s enclosed in single or double
147163
* quotation marks.
148164
*
149-
* @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}.
165+
* @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}.
150166
* @param parameters a {@link Parameters} instance describing query method parameters
151167
* @return A {@link SpelEvaluator} which allows to evaluate the SpEL expressions. Will never be {@literal null}.
152168
*/

0 commit comments

Comments
 (0)