38
38
*/
39
39
class CountQuery {
40
40
41
- private Document source ;
41
+ private final Document source ;
42
42
43
43
private CountQuery (Document source ) {
44
44
this .source = source ;
@@ -101,7 +101,7 @@ private boolean requiresRewrite(Object valueToInspect) {
101
101
}
102
102
103
103
if (valueToInspect instanceof Collection ) {
104
- return requiresRewrite ((Collection ) valueToInspect );
104
+ return requiresRewrite ((Collection <?> ) valueToInspect );
105
105
}
106
106
107
107
return false ;
@@ -157,6 +157,7 @@ private Collection<Object> rewriteCollection(Collection<?> source) {
157
157
* @param $and potentially existing {@code $and} condition.
158
158
* @return the rewritten query {@link Document}.
159
159
*/
160
+ @ SuppressWarnings ("unchecked" )
160
161
private static Document createGeoWithin (String key , Document source , @ Nullable Object $and ) {
161
162
162
163
boolean spheric = source .containsKey ("$nearSphere" );
@@ -181,7 +182,7 @@ private static Document createGeoWithin(String key, Document source, @Nullable O
181
182
182
183
if ($and != null ) {
183
184
if ($and instanceof Collection ) {
184
- Collection andElements = (Collection ) $and ;
185
+ Collection < Document > andElements = (Collection < Document > ) $and ;
185
186
criteria = new ArrayList <>(andElements .size () + 2 );
186
187
criteria .addAll (andElements );
187
188
} else {
@@ -195,24 +196,32 @@ private static Document createGeoWithin(String key, Document source, @Nullable O
195
196
196
197
criteria .add (new Document ("$nor" , Collections .singletonList (new Document (key , $geoWithinMin ))));
197
198
criteria .add (new Document (key , $geoWithinMax ));
199
+
198
200
return new Document ("$and" , criteria );
199
201
}
200
202
201
203
private static Number getMaxDistance (Document source , Object $near , boolean spheric ) {
202
204
203
205
Number maxDistance = Double .MAX_VALUE ;
204
- if (source .containsKey ("$maxDistance" )) { // legacy coordinate pair
205
- maxDistance = (Number ) source .get ("$maxDistance" );
206
- } else if ($near instanceof Document ) {
207
- Document nearDoc = (Document )$near ;
208
- if (nearDoc .containsKey ("$maxDistance" )) {
206
+
207
+ if (source .containsKey ("$maxDistance" )) { // legacy coordinate pair
208
+ return (Number ) source .get ("$maxDistance" );
209
+ }
210
+
211
+ if ($near instanceof Document ) {
212
+
213
+ Document nearDoc = (Document ) $near ;
214
+
215
+ if (nearDoc .containsKey ("$maxDistance" )) {
216
+
209
217
maxDistance = (Number ) nearDoc .get ("$maxDistance" );
210
218
// geojson is in Meters but we need radians x/(6378.1*1000)
211
- if (spheric && nearDoc .containsKey ("$geometry" )) {
219
+ if (spheric && nearDoc .containsKey ("$geometry" )) {
212
220
maxDistance = MetricConversion .metersToRadians (maxDistance .doubleValue ());
213
221
}
214
222
}
215
223
}
224
+
216
225
return maxDistance ;
217
226
}
218
227
@@ -239,13 +248,14 @@ private static Object toCenterCoordinates(Object value) {
239
248
return Arrays .asList (((Point ) value ).getX (), ((Point ) value ).getY ());
240
249
}
241
250
242
- if (value instanceof Document ) {
251
+ if (value instanceof Document ) {
243
252
Document document = (Document ) value ;
244
- if ( document . containsKey ( "x" )) {
245
- Document point = document ;
246
- return Arrays .asList (point .get ("x" ), point .get ("y" ));
253
+
254
+ if ( document . containsKey ( "x" )) {
255
+ return Arrays .asList (document .get ("x" ), document .get ("y" ));
247
256
}
248
- else if (document .containsKey ("$geometry" )) {
257
+
258
+ if (document .containsKey ("$geometry" )) {
249
259
Document geoJsonPoint = document .get ("$geometry" , Document .class );
250
260
return geoJsonPoint .get ("coordinates" );
251
261
}
0 commit comments