Skip to content

Commit 7bfa3fe

Browse files
committed
DATAMONGO-1290 - Polishing.
Removed a level of indentation from ExpressionEvaluationParameterBinder.replacePlaceholders(…). Polished JavaDoc. Original pull request: spring-projects#332.
1 parent 143b0b7 commit 7bfa3fe

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

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

+37-36
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
*
3939
* @author Christoph Strobl
4040
* @author Thomas Darimont
41+
* @author Oliver Gierke
4142
* @since 1.9
4243
*/
4344
class ExpressionEvaluatingParameterBinder {
@@ -62,13 +63,13 @@ public ExpressionEvaluatingParameterBinder(SpelExpressionParser expressionParser
6263
}
6364

6465
/**
65-
* Bind values provided by {@link MongoParameterAccessor} to placeholders in {@literal raw} while consisdering
66+
* Bind values provided by {@link MongoParameterAccessor} to placeholders in {@literal raw} while considering
6667
* potential conversions and parameter types.
6768
*
68-
* @param raw
69-
* @param accessor
70-
* @param bindingContext
71-
* @return {@literal null} if given {@literal raw} value is empty.
69+
* @param raw can be {@literal null} or empty.
70+
* @param accessor must not be {@literal null}.
71+
* @param bindingContext must not be {@literal null}.
72+
* @return {@literal null} if given {@code raw} value is empty.
7273
*/
7374
public String bind(String raw, MongoParameterAccessor accessor, BindingContext bindingContext) {
7475

@@ -80,12 +81,11 @@ public String bind(String raw, MongoParameterAccessor accessor, BindingContext b
8081
}
8182

8283
/**
83-
* Replaced the parameter place-holders with the actual parameter values from the given {@link ParameterBinding}s.
84+
* Replaced the parameter placeholders with the actual parameter values from the given {@link ParameterBinding}s.
8485
*
85-
* @param input
86-
* @param accessor
87-
* @param parameters
88-
* @param bindings
86+
* @param input must not be {@literal null} or empty.
87+
* @param accessor must not be {@literal null}.
88+
* @param bindings must not be {@literal null}.
8989
* @return
9090
*/
9191
private String replacePlaceholders(String input, MongoParameterAccessor accessor, BindingContext bindingContext) {
@@ -95,40 +95,38 @@ private String replacePlaceholders(String input, MongoParameterAccessor accessor
9595
}
9696

9797
boolean isCompletlyParameterizedQuery = input.matches("^\\?\\d+$");
98-
9998
StringBuilder result = new StringBuilder(input);
10099

101100
for (ParameterBinding binding : bindingContext.getBindings()) {
102101

103102
String parameter = binding.getParameter();
104103
int idx = result.indexOf(parameter);
105104

106-
if (idx != -1) {
107-
String valueForBinding = getParameterValueForBinding(accessor, bindingContext.getParameters(), binding);
105+
if (idx == -1) {
106+
continue;
107+
}
108108

109-
// if the value to bind is an object literal we need to remove the quoting around
110-
// the expression insertion point.
111-
boolean shouldPotentiallyRemoveQuotes = valueForBinding.startsWith("{") && !isCompletlyParameterizedQuery;
109+
String valueForBinding = getParameterValueForBinding(accessor, bindingContext.getParameters(), binding);
112110

113-
int start = idx;
114-
int end = idx + parameter.length();
111+
int start = idx;
112+
int end = idx + parameter.length();
115113

116-
if (shouldPotentiallyRemoveQuotes) {
114+
// If the value to bind is an object literal we need to remove the quoting around the expression insertion point.
115+
if (valueForBinding.startsWith("{") && !isCompletlyParameterizedQuery) {
117116

118-
// is the insertion point actually surrounded by quotes?
119-
char beforeStart = result.charAt(start - 1);
120-
char afterEnd = result.charAt(end);
117+
// Is the insertion point actually surrounded by quotes?
118+
char beforeStart = result.charAt(start - 1);
119+
char afterEnd = result.charAt(end);
121120

122-
if ((beforeStart == '\'' || beforeStart == '"') && (afterEnd == '\'' || afterEnd == '"')) {
121+
if ((beforeStart == '\'' || beforeStart == '"') && (afterEnd == '\'' || afterEnd == '"')) {
123122

124-
// skip preceeding and following quote
125-
start -= 1;
126-
end += 1;
127-
}
123+
// Skip preceding and following quote
124+
start -= 1;
125+
end += 1;
128126
}
129-
130-
result.replace(start, end, valueForBinding);
131127
}
128+
129+
result.replace(start, end, valueForBinding);
132130
}
133131

134132
return result.toString();
@@ -137,16 +135,17 @@ private String replacePlaceholders(String input, MongoParameterAccessor accessor
137135
/**
138136
* Returns the serialized value to be used for the given {@link ParameterBinding}.
139137
*
140-
* @param accessor
138+
* @param accessor must not be {@literal null}.
141139
* @param parameters
142-
* @param binding
140+
* @param binding must not be {@literal null}.
143141
* @return
144142
*/
145143
private String getParameterValueForBinding(MongoParameterAccessor accessor, MongoParameters parameters,
146144
ParameterBinding binding) {
147145

148-
Object value = binding.isExpression() ? evaluateExpression(binding.getExpression(), parameters,
149-
accessor.getValues()) : accessor.getBindableValue(binding.getParameterIndex());
146+
Object value = binding.isExpression()
147+
? evaluateExpression(binding.getExpression(), parameters, accessor.getValues())
148+
: accessor.getBindableValue(binding.getParameterIndex());
150149

151150
if (value instanceof String && binding.isQuoted()) {
152151
return (String) value;
@@ -155,9 +154,11 @@ private String getParameterValueForBinding(MongoParameterAccessor accessor, Mong
155154
if (value instanceof byte[]) {
156155

157156
String base64representation = DatatypeConverter.printBase64Binary((byte[]) value);
157+
158158
if (!binding.isQuoted()) {
159159
return "{ '$binary' : '" + base64representation + "', '$type' : " + BSON.B_GENERAL + "}";
160160
}
161+
161162
return base64representation;
162163
}
163164

@@ -167,9 +168,9 @@ private String getParameterValueForBinding(MongoParameterAccessor accessor, Mong
167168
/**
168169
* Evaluates the given {@code expressionString}.
169170
*
170-
* @param expressionString
171-
* @param parameters
172-
* @param parameterValues
171+
* @param expressionString must not be {@literal null} or empty.
172+
* @param parameters must not be {@literal null}.
173+
* @param parameterValues must not be {@literal null}.
173174
* @return
174175
*/
175176
private Object evaluateExpression(String expressionString, MongoParameters parameters, Object[] parameterValues) {

0 commit comments

Comments
 (0)