Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Reformat code.

See #4004
Original pull request: #4006.
  • Loading branch information
mp911de committed Sep 21, 2022
1 parent b5c40e7 commit 5116057
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
Expand Up @@ -38,7 +38,7 @@
*/
class CountQuery {

private Document source;
private final Document source;

private CountQuery(Document source) {
this.source = source;
Expand Down Expand Up @@ -101,7 +101,7 @@ private boolean requiresRewrite(Object valueToInspect) {
}

if (valueToInspect instanceof Collection) {
return requiresRewrite((Collection) valueToInspect);
return requiresRewrite((Collection<?>) valueToInspect);
}

return false;
Expand Down Expand Up @@ -157,6 +157,7 @@ private Collection<Object> rewriteCollection(Collection<?> source) {
* @param $and potentially existing {@code $and} condition.
* @return the rewritten query {@link Document}.
*/
@SuppressWarnings("unchecked")
private static Document createGeoWithin(String key, Document source, @Nullable Object $and) {

boolean spheric = source.containsKey("$nearSphere");
Expand All @@ -181,7 +182,7 @@ private static Document createGeoWithin(String key, Document source, @Nullable O

if ($and != null) {
if ($and instanceof Collection) {
Collection andElements = (Collection) $and;
Collection<Document> andElements = (Collection<Document>) $and;
criteria = new ArrayList<>(andElements.size() + 2);
criteria.addAll(andElements);
} else {
Expand All @@ -195,24 +196,32 @@ private static Document createGeoWithin(String key, Document source, @Nullable O

criteria.add(new Document("$nor", Collections.singletonList(new Document(key, $geoWithinMin))));
criteria.add(new Document(key, $geoWithinMax));

return new Document("$and", criteria);
}

private static Number getMaxDistance(Document source, Object $near, boolean spheric) {

Number maxDistance = Double.MAX_VALUE;
if(source.containsKey("$maxDistance")) { // legacy coordinate pair
maxDistance = (Number) source.get("$maxDistance");
} else if ($near instanceof Document) {
Document nearDoc = (Document)$near;
if(nearDoc.containsKey("$maxDistance")) {

if (source.containsKey("$maxDistance")) { // legacy coordinate pair
return (Number) source.get("$maxDistance");
}

if ($near instanceof Document) {

Document nearDoc = (Document) $near;

if (nearDoc.containsKey("$maxDistance")) {

maxDistance = (Number) nearDoc.get("$maxDistance");
// geojson is in Meters but we need radians x/(6378.1*1000)
if(spheric && nearDoc.containsKey("$geometry")) {
if (spheric && nearDoc.containsKey("$geometry")) {
maxDistance = MetricConversion.metersToRadians(maxDistance.doubleValue());
}
}
}

return maxDistance;
}

Expand All @@ -239,13 +248,14 @@ private static Object toCenterCoordinates(Object value) {
return Arrays.asList(((Point) value).getX(), ((Point) value).getY());
}

if (value instanceof Document ) {
if (value instanceof Document) {
Document document = (Document) value;
if(document.containsKey("x")) {
Document point = document;
return Arrays.asList(point.get("x"), point.get("y"));

if (document.containsKey("x")) {
return Arrays.asList(document.get("x"), document.get("y"));
}
else if (document.containsKey("$geometry")) {

if (document.containsKey("$geometry")) {
Document geoJsonPoint = document.get("$geometry", Document.class);
return geoJsonPoint.get("coordinates");
}
Expand Down
Expand Up @@ -67,8 +67,8 @@ public static double getDistanceInMeters(Distance distance) {
* Return {@code distance} in radians (on an earth like sphere).
*
* @param distance must not be {@literal null}.
* @return distance in rads.
* @since 3.4
* @return distance in radians.
* @since 3.4.4
*/
public static double toRadians(Distance distance) {
return metersToRadians(getDistanceInMeters(distance));
Expand All @@ -78,8 +78,8 @@ public static double toRadians(Distance distance) {
* Return {@code distance} in radians (on an earth like sphere).
*
* @param meters
* @return distance in rads.
* @since 3.4
* @return distance in radians.
* @since 3.4.4
*/
public static double metersToRadians(double meters) {
return BigDecimal.valueOf(meters).divide(METERS_MULTIPLIER, MathContext.DECIMAL64).doubleValue();
Expand Down

0 comments on commit 5116057

Please sign in to comment.