Skip to content

Commit 7791456

Browse files
odrotbohmchristophstrobl
authored andcommitted
DATAMONGO-1565 - Polishing.
Formatting in ExpressionEvaluatingParameterBinder and StringBasedMongoQueryUnitTests. Turned Placeholder into value object.
1 parent c0f3255 commit 7791456

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.query;
1717

18-
import lombok.Data;
19-
import lombok.RequiredArgsConstructor;
18+
import lombok.Value;
2019

2120
import java.util.ArrayList;
2221
import java.util.LinkedHashMap;
@@ -144,19 +143,24 @@ private void postProcessQuotedBinding(StringBuffer buffer, String valueForBindin
144143
while (quotationMark != '\'' && quotationMark != '"') {
145144

146145
quotationMarkIndex--;
146+
147147
if (quotationMarkIndex < 0) {
148148
throw new IllegalArgumentException("Could not find opening quotes for quoted parameter");
149149
}
150+
150151
quotationMark = buffer.charAt(quotationMarkIndex);
151152
}
152153

153154
if (valueForBinding.startsWith("{")) { // remove quotation char before the complex object string
155+
154156
buffer.deleteCharAt(quotationMarkIndex);
157+
155158
} else {
156159

157160
if (quotationMark == '\'') {
158161
buffer.replace(quotationMarkIndex, quotationMarkIndex + 1, "\"");
159162
}
163+
160164
buffer.append("\"");
161165
}
162166
}
@@ -220,7 +224,9 @@ private Object evaluateExpression(String expressionString, MongoParameters param
220224
private Pattern createReplacementPattern(List<ParameterBinding> bindings) {
221225

222226
StringBuilder regex = new StringBuilder();
227+
223228
for (ParameterBinding binding : bindings) {
229+
224230
regex.append("|");
225231
regex.append(Pattern.quote(binding.getParameter()));
226232
regex.append("['\"]?"); // potential quotation char (as in { foo : '?0' }).
@@ -238,10 +244,9 @@ private Pattern createReplacementPattern(List<ParameterBinding> bindings) {
238244
*/
239245
private Placeholder extractPlaceholder(String groupName) {
240246

241-
if (!groupName.endsWith("'") && !groupName.endsWith("\"")) {
242-
return new Placeholder(groupName, false);
243-
}
244-
return new Placeholder(groupName.substring(0, groupName.length() - 1), true);
247+
return !groupName.endsWith("'") && !groupName.endsWith("\"") ? //
248+
Placeholder.of(groupName, false) : //
249+
Placeholder.of(groupName.substring(0, groupName.length() - 1), true);
245250
}
246251

247252
/**
@@ -311,9 +316,11 @@ public MongoParameters getParameters() {
311316
private static Map<Placeholder, ParameterBinding> mapBindings(List<ParameterBinding> bindings) {
312317

313318
Map<Placeholder, ParameterBinding> map = new LinkedHashMap<Placeholder, ParameterBinding>(bindings.size(), 1);
319+
314320
for (ParameterBinding binding : bindings) {
315-
map.put(new Placeholder(binding.getParameter(), binding.isQuoted()), binding);
321+
map.put(Placeholder.of(binding.getParameter(), binding.isQuoted()), binding);
316322
}
323+
317324
return map;
318325
}
319326
}
@@ -324,8 +331,7 @@ private static Map<Placeholder, ParameterBinding> mapBindings(List<ParameterBind
324331
* @author Mark Paluch
325332
* @since 1.9
326333
*/
327-
@Data
328-
@RequiredArgsConstructor
334+
@Value(staticConstructor = "of")
329335
static class Placeholder {
330336

331337
private final String parameter;

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,17 @@ public void shouldSupportNonQuotedBinaryDataReplacement() throws Exception {
367367
assertThat(query.getQueryObject().toJson(), is(reference.getQueryObject().toJson()));
368368
}
369369

370+
/**
371+
* @see DATAMONGO-1454
372+
*/
373+
@Test
374+
public void shouldSupportExistsProjection() throws Exception {
375+
376+
StringBasedMongoQuery mongoQuery = createQueryForMethod("existsByLastname", String.class);
377+
378+
assertThat(mongoQuery.isExistsQuery(), is(true));
379+
}
380+
370381
/**
371382
* @see DATAMONGO-1565
372383
*/
@@ -387,17 +398,6 @@ public void bindsPropertyReferenceMultipleTimesCorrectly() throws Exception {
387398
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
388399
}
389400

390-
/**
391-
* @see DATAMONGO-1454
392-
*/
393-
@Test
394-
public void shouldSupportExistsProjection() throws Exception {
395-
396-
StringBasedMongoQuery mongoQuery = createQueryForMethod("existsByLastname", String.class);
397-
398-
assertThat(mongoQuery.isExistsQuery(), is(true));
399-
}
400-
401401
/**
402402
* @see DATAMONGO-1565
403403
*/

0 commit comments

Comments
 (0)