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,30 @@ 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 nearDoc ) {
212
+
213
+ if (nearDoc .containsKey ("$maxDistance" )) {
214
+
209
215
maxDistance = (Number ) nearDoc .get ("$maxDistance" );
210
216
// geojson is in Meters but we need radians x/(6378.1*1000)
211
- if (spheric && nearDoc .containsKey ("$geometry" )) {
217
+ if (spheric && nearDoc .containsKey ("$geometry" )) {
212
218
maxDistance = MetricConversion .metersToRadians (maxDistance .doubleValue ());
213
219
}
214
220
}
215
221
}
222
+
216
223
return maxDistance ;
217
224
}
218
225
@@ -239,13 +246,13 @@ private static Object toCenterCoordinates(Object value) {
239
246
return Arrays .asList (((Point ) value ).getX (), ((Point ) value ).getY ());
240
247
}
241
248
242
- if (value instanceof Document ) {
243
- Document document = (Document ) value ;
244
- if (document .containsKey ("x" )) {
245
- Document point = document ;
246
- return Arrays .asList (point .get ("x" ), point .get ("y" ));
249
+ if (value instanceof Document document ) {
250
+
251
+ if (document .containsKey ("x" )) {
252
+ return Arrays .asList (document .get ("x" ), document .get ("y" ));
247
253
}
248
- else if (document .containsKey ("$geometry" )) {
254
+
255
+ if (document .containsKey ("$geometry" )) {
249
256
Document geoJsonPoint = document .get ("$geometry" , Document .class );
250
257
return geoJsonPoint .get ("coordinates" );
251
258
}
0 commit comments