3838 */
3939class CountQuery {
4040
41- private Document source ;
41+ private final Document source ;
4242
4343 private CountQuery (Document source ) {
4444 this .source = source ;
@@ -101,7 +101,7 @@ private boolean requiresRewrite(Object valueToInspect) {
101101 }
102102
103103 if (valueToInspect instanceof Collection ) {
104- return requiresRewrite ((Collection ) valueToInspect );
104+ return requiresRewrite ((Collection <?> ) valueToInspect );
105105 }
106106
107107 return false ;
@@ -157,6 +157,7 @@ private Collection<Object> rewriteCollection(Collection<?> source) {
157157 * @param $and potentially existing {@code $and} condition.
158158 * @return the rewritten query {@link Document}.
159159 */
160+ @ SuppressWarnings ("unchecked" )
160161 private static Document createGeoWithin (String key , Document source , @ Nullable Object $and ) {
161162
162163 boolean spheric = source .containsKey ("$nearSphere" );
@@ -181,7 +182,7 @@ private static Document createGeoWithin(String key, Document source, @Nullable O
181182
182183 if ($and != null ) {
183184 if ($and instanceof Collection ) {
184- Collection andElements = (Collection ) $and ;
185+ Collection < Document > andElements = (Collection < Document > ) $and ;
185186 criteria = new ArrayList <>(andElements .size () + 2 );
186187 criteria .addAll (andElements );
187188 } else {
@@ -195,24 +196,32 @@ private static Document createGeoWithin(String key, Document source, @Nullable O
195196
196197 criteria .add (new Document ("$nor" , Collections .singletonList (new Document (key , $geoWithinMin ))));
197198 criteria .add (new Document (key , $geoWithinMax ));
199+
198200 return new Document ("$and" , criteria );
199201 }
200202
201203 private static Number getMaxDistance (Document source , Object $near , boolean spheric ) {
202204
203205 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+
209217 maxDistance = (Number ) nearDoc .get ("$maxDistance" );
210218 // geojson is in Meters but we need radians x/(6378.1*1000)
211- if (spheric && nearDoc .containsKey ("$geometry" )) {
219+ if (spheric && nearDoc .containsKey ("$geometry" )) {
212220 maxDistance = MetricConversion .metersToRadians (maxDistance .doubleValue ());
213221 }
214222 }
215223 }
224+
216225 return maxDistance ;
217226 }
218227
@@ -239,13 +248,14 @@ private static Object toCenterCoordinates(Object value) {
239248 return Arrays .asList (((Point ) value ).getX (), ((Point ) value ).getY ());
240249 }
241250
242- if (value instanceof Document ) {
251+ if (value instanceof Document ) {
243252 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" ));
247256 }
248- else if (document .containsKey ("$geometry" )) {
257+
258+ if (document .containsKey ("$geometry" )) {
249259 Document geoJsonPoint = document .get ("$geometry" , Document .class );
250260 return geoJsonPoint .get ("coordinates" );
251261 }
0 commit comments