Skip to content

Commit 40fe2ad

Browse files
committed
DATAKV-115 - Polishing.
Introduced dedicated constructor on SpelCriteria to default the EvaluationContext to StandardEvaluationContext. Switched to ternary if in KeyValuePartTreeQuery for both application of Pageable and Sort. Tweaked unit test to actually compile in Eclipse. Original pull request: #16.
1 parent e22ee22 commit 40fe2ad

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

src/main/java/org/springframework/data/keyvalue/core/SpelCriteria.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,55 @@
1818
import org.springframework.expression.EvaluationContext;
1919
import org.springframework.expression.spel.standard.SpelExpression;
2020
import org.springframework.expression.spel.support.StandardEvaluationContext;
21+
import org.springframework.util.Assert;
2122

2223
/**
2324
* {@link SpelCriteria} allows to pass on a {@link SpelExpression} and {@link EvaluationContext} to the actual query
2425
* processor. This decouples the {@link SpelExpression} from the context it is used in.
2526
*
2627
* @author Christoph Strobl
28+
* @author Oliver Gierke
2729
*/
2830
public class SpelCriteria {
2931

3032
private final SpelExpression expression;
3133
private final EvaluationContext context;
3234

35+
/**
36+
* Creates a new {@link SpelCriteria} for the given {@link SpelExpression}.
37+
*
38+
* @param expression must not be {@literal null}.
39+
*/
40+
public SpelCriteria(SpelExpression expression) {
41+
this(expression, new StandardEvaluationContext());
42+
}
43+
3344
/**
3445
* Creates new {@link SpelCriteria}.
3546
*
3647
* @param expression must not be {@literal null}.
37-
* @param context can be {@literal null} and will be defaulted to {@link StandardEvaluationContext}.
48+
* @param context must not be {@literal null}.
3849
*/
3950
public SpelCriteria(SpelExpression expression, EvaluationContext context) {
4051

52+
Assert.notNull(expression, "SpEL expression must not be null!");
53+
Assert.notNull(context, "EvaluationContext must not be null!");
54+
4155
this.expression = expression;
42-
this.context = context == null ? new StandardEvaluationContext() : context;
56+
this.context = context;
4357
}
4458

4559
/**
46-
* @return never {@literal null}.
60+
* @return will never be {@literal null}.
4761
*/
4862
public EvaluationContext getContext() {
4963
return context;
5064
}
5165

5266
/**
53-
* @return never {@literal null}.
67+
* @return will never be {@literal null}.
5468
*/
5569
public SpelExpression getExpression() {
5670
return expression;
5771
}
58-
5972
}

src/main/java/org/springframework/data/keyvalue/core/SpelCriteriaAccessor.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
1919
import org.springframework.expression.spel.standard.SpelExpression;
2020
import org.springframework.expression.spel.standard.SpelExpressionParser;
21-
import org.springframework.expression.spel.support.StandardEvaluationContext;
2221
import org.springframework.util.Assert;
2322

2423
/**
@@ -32,14 +31,21 @@ class SpelCriteriaAccessor implements CriteriaAccessor<SpelCriteria> {
3231
private final SpelExpressionParser parser;
3332

3433
/**
34+
* Creates a new {@link SpelCriteriaAccessor} using the given {@link SpelExpressionParser}.
35+
*
3536
* @param parser must not be {@literal null}.
3637
*/
3738
public SpelCriteriaAccessor(SpelExpressionParser parser) {
3839

3940
Assert.notNull(parser, "SpelExpressionParser must not be null!");
41+
4042
this.parser = parser;
4143
}
4244

45+
/*
46+
* (non-Javadoc)
47+
* @see org.springframework.data.keyvalue.core.CriteriaAccessor#resolve(org.springframework.data.keyvalue.core.query.KeyValueQuery)
48+
*/
4349
@Override
4450
public SpelCriteria resolve(KeyValueQuery<?> query) {
4551

@@ -48,11 +54,11 @@ public SpelCriteria resolve(KeyValueQuery<?> query) {
4854
}
4955

5056
if (query.getCritieria() instanceof SpelExpression) {
51-
return new SpelCriteria((SpelExpression) query.getCritieria(), new StandardEvaluationContext());
57+
return new SpelCriteria((SpelExpression) query.getCritieria());
5258
}
5359

5460
if (query.getCritieria() instanceof String) {
55-
return new SpelCriteria(parser.parseRaw((String) query.getCritieria()), new StandardEvaluationContext());
61+
return new SpelCriteria(parser.parseRaw((String) query.getCritieria()));
5662
}
5763

5864
if (query.getCritieria() instanceof SpelCriteria) {
@@ -61,5 +67,4 @@ public SpelCriteria resolve(KeyValueQuery<?> query) {
6167

6268
throw new IllegalArgumentException("Cannot create SpelCriteria for " + query.getCritieria());
6369
}
64-
6570
}

src/main/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQuery.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ protected Object doExecute(Object[] parameters, KeyValueQuery<?> query) {
115115

116116
Iterable<?> result = this.keyValueOperations.find(query, queryMethod.getEntityInformation().getJavaType());
117117

118-
long count = queryMethod.isSliceQuery() ? 0 : keyValueOperations.count(query, queryMethod.getEntityInformation()
119-
.getJavaType());
118+
long count = queryMethod.isSliceQuery() ? 0
119+
: keyValueOperations.count(query, queryMethod.getEntityInformation().getJavaType());
120120

121121
return new PageImpl(IterableConverter.toList(result), page, count);
122122

@@ -136,35 +136,30 @@ protected Object doExecute(Object[] parameters, KeyValueQuery<?> query) {
136136
@SuppressWarnings({ "rawtypes", "unchecked" })
137137
protected KeyValueQuery<?> prepareQuery(Object[] parameters) {
138138

139-
ParametersParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters);
139+
ParametersParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(),
140+
parameters);
140141

141142
if (this.query == null) {
142143
this.query = createQuery(accessor);
143144
}
144145

145-
KeyValueQuery<?> q = new KeyValueQuery(this.query.getCritieria());
146+
KeyValueQuery<?> query = new KeyValueQuery(this.query.getCritieria());
146147

147148
if (this.query.getCritieria() instanceof SpelExpression) {
148149

149150
EvaluationContext context = this.evaluationContextProvider.getEvaluationContext(getQueryMethod().getParameters(),
150151
parameters);
151-
SpelCriteria spelCriteria = new SpelCriteria((SpelExpression) this.query.getCritieria(), context);
152-
q = new KeyValueQuery(spelCriteria);
153-
}
154-
155-
if (accessor.getPageable() != null) {
156-
q.setOffset(accessor.getPageable().getOffset());
157-
q.setRows(accessor.getPageable().getPageSize());
158-
} else {
159-
q.setOffset(-1);
160-
q.setRows(-1);
152+
query = new KeyValueQuery(new SpelCriteria((SpelExpression) this.query.getCritieria(), context));
161153
}
162154

155+
Pageable pageable = accessor.getPageable();
163156
Sort sort = accessor.getSort();
164157

165-
q.setSort(sort != null ? sort : query.getSort());
158+
query.setOffset(pageable == null ? -1 : pageable.getOffset());
159+
query.setRows(pageable == null ? -1 : pageable.getPageSize());
160+
query.setSort(sort == null ? this.query.getSort() : sort);
166161

167-
return q;
162+
return query;
168163
}
169164

170165
public KeyValueQuery<?> createQuery(ParameterAccessor accessor) {

src/test/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQueryUnitTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ public void spelExpressionAndContextShouldNotBeReused() throws NoSuchMethodExcep
6363

6464
Object[] args = new Object[] { "foo" };
6565

66-
assertThat(query.prepareQuery(args).getCritieria(), not(sameInstance(query.prepareQuery(args).getCritieria())));
66+
Object first = query.prepareQuery(args).getCritieria();
67+
Object second = query.prepareQuery(args).getCritieria();
68+
69+
assertThat(first, not(sameInstance(second)));
6770
}
6871

6972
static interface Repo {

0 commit comments

Comments
 (0)