|
| 1 | +/* |
| 2 | + * Copyright 2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.core; |
| 17 | + |
| 18 | +import org.springframework.util.Assert; |
| 19 | + |
| 20 | +import com.mongodb.BasicDBObject; |
| 21 | +import com.mongodb.DBObject; |
| 22 | + |
| 23 | +/** |
| 24 | + * Value object to mitigate different representations of geo command execution results in MongoDB. |
| 25 | + * |
| 26 | + * @author Oliver Gierke |
| 27 | + * @soundtrack Fruitcake - Jeff Coffin (The Inside of the Outside) |
| 28 | + */ |
| 29 | +class GeoCommandStatistics { |
| 30 | + |
| 31 | + private static final GeoCommandStatistics NONE = new GeoCommandStatistics(new BasicDBObject()); |
| 32 | + |
| 33 | + private final DBObject source; |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates a new {@link GeoCommandStatistics} instance with the given source document. |
| 37 | + * |
| 38 | + * @param source must not be {@literal null}. |
| 39 | + */ |
| 40 | + private GeoCommandStatistics(DBObject source) { |
| 41 | + |
| 42 | + Assert.notNull(source, "Source document must not be null!"); |
| 43 | + this.source = source; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a new {@link GeoCommandStatistics} from the given command result extracting the statistics. |
| 48 | + * |
| 49 | + * @param commandResult must not be {@literal null}. |
| 50 | + * @return |
| 51 | + */ |
| 52 | + public static GeoCommandStatistics from(DBObject commandResult) { |
| 53 | + |
| 54 | + Assert.notNull(commandResult, "Command result must not be null!"); |
| 55 | + |
| 56 | + Object stats = commandResult.get("stats"); |
| 57 | + return stats == null ? NONE : new GeoCommandStatistics((DBObject) stats); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns the average distance reported by the command result. Mitigating a removal of the field in case the command |
| 62 | + * didn't return any result introduced in MongoDB 3.2 RC1. |
| 63 | + * |
| 64 | + * @return |
| 65 | + * @see https://jira.mongodb.org/browse/SERVER-21024 |
| 66 | + */ |
| 67 | + public double getAverageDistance() { |
| 68 | + |
| 69 | + Object averageDistance = source.get("avgDistance"); |
| 70 | + return averageDistance == null ? Double.NaN : (Double) averageDistance; |
| 71 | + } |
| 72 | +} |
0 commit comments