Skip to content

Commit 594e907

Browse files
committed
DATAMONGO-1244 - Polishing.
Minor reformattings and extracted a method to improve digestability. Original pull request: #306.
1 parent f2ab42c commit 594e907

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

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

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ protected boolean isDeleteQuery() {
161161
* @param bindings
162162
* @return
163163
*/
164-
private String replacePlaceholders(String input, ConvertingParameterAccessor accessor, List<ParameterBinding> bindings) {
164+
private String replacePlaceholders(String input, ConvertingParameterAccessor accessor,
165+
List<ParameterBinding> bindings) {
165166

166167
if (bindings.isEmpty()) {
167168
return input;
@@ -187,11 +188,11 @@ private String replacePlaceholders(String input, ConvertingParameterAccessor acc
187188
int end = idx + parameter.length();
188189

189190
if (shouldPotentiallyRemoveQuotes) {
190-
191+
191192
// is the insertion point actually surrounded by quotes?
192193
char beforeStart = result.charAt(start - 1);
193194
char afterEnd = result.charAt(end);
194-
195+
195196
if ((beforeStart == '\'' || beforeStart == '"') && (afterEnd == '\'' || afterEnd == '"')) {
196197

197198
// skip preceeding and following quote
@@ -235,9 +236,10 @@ private String getParameterValueForBinding(ConvertingParameterAccessor accessor,
235236
*/
236237
private Object evaluateExpression(String expressionString, Object[] parameterValues) {
237238

238-
EvaluationContext evaluationContext = evaluationContextProvider.getEvaluationContext(getQueryMethod()
239-
.getParameters(), parameterValues);
239+
EvaluationContext evaluationContext = evaluationContextProvider
240+
.getEvaluationContext(getQueryMethod().getParameters(), parameterValues);
240241
Expression expression = expressionParser.parseExpression(expressionString);
242+
241243
return expression.getValue(evaluationContext, Object.class);
242244
}
243245

@@ -287,7 +289,7 @@ public String parseAndCollectParameterBindingsFromQueryIntoBindings(String input
287289
return transformedInput;
288290
}
289291

290-
private String transformQueryAndCollectExpressionParametersIntoBindings(String input,
292+
private static String transformQueryAndCollectExpressionParametersIntoBindings(String input,
291293
List<ParameterBinding> bindings) {
292294

293295
StringBuilder result = new StringBuilder();
@@ -297,14 +299,11 @@ private String transformQueryAndCollectExpressionParametersIntoBindings(String i
297299
int exprIndex = 0;
298300

299301
while (currentPos < input.length()) {
300-
int indexOfExpressionParameter = input.indexOf(INDEX_BASED_EXPRESSION_PARAM_START, currentPos);
301302

302-
if (indexOfExpressionParameter < 0) {
303-
indexOfExpressionParameter = input.indexOf(NAME_BASED_EXPRESSION_PARAM_START, currentPos);
304-
}
303+
int indexOfExpressionParameter = getIndexOfExpressionParameter(input, currentPos);
305304

305+
// no expression parameter found
306306
if (indexOfExpressionParameter < 0) {
307-
// no expression parameter found
308307
break;
309308
}
310309

@@ -313,44 +312,41 @@ private String transformQueryAndCollectExpressionParametersIntoBindings(String i
313312

314313
// eat parameter expression
315314
int curlyBraceOpenCnt = 1;
315+
316316
while (curlyBraceOpenCnt > 0) {
317-
char c = input.charAt(currentPos++);
318-
switch (c) {
317+
switch (input.charAt(currentPos++)) {
319318
case CURRLY_BRACE_OPEN:
320319
curlyBraceOpenCnt++;
321320
break;
322321
case CURRLY_BRACE_CLOSE:
323322
curlyBraceOpenCnt--;
324323
break;
325324
default:
326-
;
327325
}
328326
}
329327

330328
result.append(input.subSequence(startIndex, indexOfExpressionParameter));
331-
result.append(EXPRESSION_PARAM_QUOTE).append(EXPRESSION_PARAM_PREFIX).append(exprIndex)
332-
.append(EXPRESSION_PARAM_QUOTE);
329+
result.append(EXPRESSION_PARAM_QUOTE).append(EXPRESSION_PARAM_PREFIX);
330+
result.append(exprIndex);
331+
result.append(EXPRESSION_PARAM_QUOTE);
332+
333333
bindings.add(new ParameterBinding(exprIndex, true, input.substring(exprStart, currentPos - 1)));
334334

335335
startIndex = currentPos;
336336

337337
exprIndex++;
338338
}
339339

340-
result.append(input.subSequence(currentPos, input.length()));
341-
342-
return result.toString();
340+
return result.append(input.subSequence(currentPos, input.length())).toString();
343341
}
344342

345-
private String makeParameterReferencesParseable(String input) {
343+
private static String makeParameterReferencesParseable(String input) {
346344

347345
Matcher matcher = PARAMETER_BINDING_PATTERN.matcher(input);
348-
String parseableInput = matcher.replaceAll(PARSEABLE_PARAMETER);
349-
350-
return parseableInput;
346+
return matcher.replaceAll(PARSEABLE_PARAMETER);
351347
}
352348

353-
private void collectParameterReferencesIntoBindings(List<ParameterBinding> bindings, Object value) {
349+
private static void collectParameterReferencesIntoBindings(List<ParameterBinding> bindings, Object value) {
354350

355351
if (value instanceof String) {
356352

@@ -393,7 +389,7 @@ private void collectParameterReferencesIntoBindings(List<ParameterBinding> bindi
393389
}
394390
}
395391

396-
private void potentiallyAddBinding(String source, List<ParameterBinding> bindings) {
392+
private static void potentiallyAddBinding(String source, List<ParameterBinding> bindings) {
397393

398394
Matcher valueMatcher = PARSEABLE_BINDING_PATTERN.matcher(source);
399395

@@ -406,6 +402,14 @@ private void potentiallyAddBinding(String source, List<ParameterBinding> binding
406402
bindings.add(new ParameterBinding(paramIndex, quoted));
407403
}
408404
}
405+
406+
private static int getIndexOfExpressionParameter(String input, int position) {
407+
408+
int indexOfExpressionParameter = input.indexOf(INDEX_BASED_EXPRESSION_PARAM_START, position);
409+
410+
return indexOfExpressionParameter < 0 ? input.indexOf(NAME_BASED_EXPRESSION_PARAM_START, position)
411+
: indexOfExpressionParameter;
412+
}
409413
}
410414

411415
/**

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQueryUnitTests.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,15 @@ public void shouldSupportExpressionsInCustomQueries() throws Exception {
310310
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
311311
}
312312

313-
314313
/**
315314
* @see DATAMONGO-1244
316315
*/
317316
@Test
318317
public void shouldSupportExpressionsInCustomQueriesWithNestedObject() throws Exception {
319318

320319
ConvertingParameterAccessor accesor = StubParameterAccessor.getAccessor(converter, true, "param1", "param2");
321-
StringBasedMongoQuery mongoQuery = createQueryForMethod("findByQueryWithExpressionAndNestedObject", boolean.class, String.class);
320+
StringBasedMongoQuery mongoQuery = createQueryForMethod("findByQueryWithExpressionAndNestedObject", boolean.class,
321+
String.class);
322322

323323
org.springframework.data.mongodb.core.query.Query query = mongoQuery.createQuery(accesor);
324324
org.springframework.data.mongodb.core.query.Query reference = new BasicQuery("{ \"id\" : { \"$exists\" : true}}");
@@ -333,15 +333,16 @@ public void shouldSupportExpressionsInCustomQueriesWithNestedObject() throws Exc
333333
public void shouldSupportExpressionsInCustomQueriesWithMultipleNestedObjects() throws Exception {
334334

335335
ConvertingParameterAccessor accesor = StubParameterAccessor.getAccessor(converter, true, "param1", "param2");
336-
StringBasedMongoQuery mongoQuery = createQueryForMethod("findByQueryWithExpressionAndMultipleNestedObjects", boolean.class, String.class, String.class);
336+
StringBasedMongoQuery mongoQuery = createQueryForMethod("findByQueryWithExpressionAndMultipleNestedObjects",
337+
boolean.class, String.class, String.class);
337338

338339
org.springframework.data.mongodb.core.query.Query query = mongoQuery.createQuery(accesor);
339-
org.springframework.data.mongodb.core.query.Query reference = new BasicQuery("{ \"id\" : { \"$exists\" : true} , \"foo\" : 42 , \"bar\" : { \"$exists\" : false}}");
340+
org.springframework.data.mongodb.core.query.Query reference = new BasicQuery(
341+
"{ \"id\" : { \"$exists\" : true} , \"foo\" : 42 , \"bar\" : { \"$exists\" : false}}");
340342

341343
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
342344
}
343345

344-
345346
private StringBasedMongoQuery createQueryForMethod(String name, Class<?>... parameters) throws Exception {
346347

347348
Method method = SampleRepository.class.getMethod(name, parameters);
@@ -389,11 +390,11 @@ private interface SampleRepository {
389390

390391
@Query("{'lastname': ?#{[0]} }")
391392
List<Person> findByQueryWithExpression(String param0);
392-
393+
393394
@Query("{'id':?#{ [0] ? { $exists :true} : [1] }}")
394395
List<Person> findByQueryWithExpressionAndNestedObject(boolean param0, String param1);
395-
396+
396397
@Query("{'id':?#{ [0] ? { $exists :true} : [1] }, 'foo':42, 'bar': ?#{ [0] ? { $exists :false} : [1] }}")
397-
List<Person> findByQueryWithExpressionAndMultipleNestedObjects(boolean param0, String param1, String param2);
398+
List<Person> findByQueryWithExpressionAndMultipleNestedObjects(boolean param0, String param1, String param2);
398399
}
399400
}

0 commit comments

Comments
 (0)