DATAMONGO-858 - Add support for common geospatial structures.
Backed the geo spatial structures of SD MongoDB by the new geo spatial structures in SD commons. Deprecated the MongoDB geo spatial types to make users aware that we're going to remove them in one of the following development iterations. Added custom conversions for basic geo spatial types. We deliberately choose not to let Circle extends CMNS geo.Circle since it would break clients that use the legacy Circle API (getRadius() returns a Distance in CMNS where as it returns a plain double in Mongo).
This commit is contained in:
committed by
Oliver Gierke
parent
75194730e9
commit
d5ed4e0ac2
@@ -23,7 +23,6 @@ import org.springframework.data.mongodb.core.aggregation.Aggregation;
|
||||
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
|
||||
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverter;
|
||||
import org.springframework.data.mongodb.core.geo.GeoResult;
|
||||
import org.springframework.data.mongodb.core.geo.GeoResults;
|
||||
import org.springframework.data.mongodb.core.mapreduce.GroupBy;
|
||||
import org.springframework.data.mongodb.core.mapreduce.GroupByResults;
|
||||
@@ -51,6 +50,7 @@ import com.mongodb.WriteResult;
|
||||
* @author Tobias Trelle
|
||||
* @author Chuong Ngo
|
||||
* @author Christoph Strobl
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public interface MongoOperations {
|
||||
|
||||
|
||||
@@ -51,6 +51,9 @@ import org.springframework.dao.support.PersistenceExceptionTranslator;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.authentication.UserCredentials;
|
||||
import org.springframework.data.convert.EntityReader;
|
||||
import org.springframework.data.geo.Distance;
|
||||
import org.springframework.data.geo.GeoResult;
|
||||
import org.springframework.data.geo.Metric;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.mapping.model.BeanWrapper;
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
@@ -68,10 +71,7 @@ import org.springframework.data.mongodb.core.convert.MongoConverter;
|
||||
import org.springframework.data.mongodb.core.convert.MongoWriter;
|
||||
import org.springframework.data.mongodb.core.convert.QueryMapper;
|
||||
import org.springframework.data.mongodb.core.convert.UpdateMapper;
|
||||
import org.springframework.data.mongodb.core.geo.Distance;
|
||||
import org.springframework.data.mongodb.core.geo.GeoResult;
|
||||
import org.springframework.data.mongodb.core.geo.GeoResults;
|
||||
import org.springframework.data.mongodb.core.geo.Metric;
|
||||
import org.springframework.data.mongodb.core.index.MongoMappingEventPublisher;
|
||||
import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
@@ -115,7 +115,6 @@ import com.mongodb.WriteConcern;
|
||||
import com.mongodb.WriteResult;
|
||||
import com.mongodb.util.JSON;
|
||||
import com.mongodb.util.JSONParseException;
|
||||
|
||||
/**
|
||||
* Primary implementation of {@link MongoOperations}.
|
||||
*
|
||||
|
||||
@@ -96,6 +96,7 @@ public class CustomConversions {
|
||||
|
||||
List<Object> toRegister = new ArrayList<Object>();
|
||||
|
||||
// Add user provided converters to make sure they can override the defaults
|
||||
toRegister.addAll(converters);
|
||||
toRegister.add(CustomToStringConverter.INSTANCE);
|
||||
toRegister.add(BigDecimalToStringConverter.INSTANCE);
|
||||
@@ -107,7 +108,7 @@ public class CustomConversions {
|
||||
toRegister.add(DBObjectToStringConverter.INSTANCE);
|
||||
toRegister.addAll(JodaTimeConverters.getConvertersToRegister());
|
||||
|
||||
// Add user provided converters to make sure they can override the defaults
|
||||
toRegister.addAll(GeoConverters.getConvertersToRegister());
|
||||
|
||||
for (Object c : toRegister) {
|
||||
registerConversion(c);
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.convert;
|
||||
|
||||
import org.springframework.data.geo.Box;
|
||||
import org.springframework.data.geo.Circle;
|
||||
import org.springframework.data.geo.Polygon;
|
||||
import org.springframework.data.geo.Shape;
|
||||
import org.springframework.data.mongodb.core.geo.Sphere;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
/**
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public enum GeoCommandUtils {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
/**
|
||||
* Wraps the given {@link Shape} in an appropriate MongoDB command.
|
||||
*
|
||||
* @param shape must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public DBObject wrapInCommand(Shape shape) {
|
||||
|
||||
Assert.notNull(shape, "Shape must not be null!");
|
||||
|
||||
return new BasicDBObject(getCommand(shape), shape);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the MongoDB command for the given {@link Shape}.
|
||||
*
|
||||
* @param shape must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
private String getCommand(Shape shape) {
|
||||
|
||||
Assert.notNull(shape, "Shape must not be null!");
|
||||
|
||||
if (shape instanceof Box) {
|
||||
return org.springframework.data.mongodb.core.geo.Box.COMMAND;
|
||||
} else if (shape instanceof Circle || shape instanceof org.springframework.data.mongodb.core.geo.Circle) {
|
||||
return org.springframework.data.mongodb.core.geo.Circle.COMMAND;
|
||||
} else if (shape instanceof Polygon) {
|
||||
return org.springframework.data.mongodb.core.geo.Polygon.COMMAND;
|
||||
} else if (shape instanceof Sphere) {
|
||||
return org.springframework.data.mongodb.core.geo.Sphere.COMMAND;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Unknown shape: " + shape);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,381 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.convert;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.convert.ReadingConverter;
|
||||
import org.springframework.data.convert.WritingConverter;
|
||||
import org.springframework.data.geo.Box;
|
||||
import org.springframework.data.geo.Circle;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.data.geo.Polygon;
|
||||
import org.springframework.data.mongodb.core.geo.Sphere;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.mongodb.BasicDBList;
|
||||
|
||||
/**
|
||||
* Wrapper class to contain useful geo structure converters for the usage with Mongo.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
abstract class GeoConverters {
|
||||
|
||||
/**
|
||||
* Private constructor to prevent instantiation.
|
||||
*/
|
||||
private GeoConverters() {}
|
||||
|
||||
/**
|
||||
* Converts a {@link List} of {@link Double}s into a {@link Point}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
@ReadingConverter
|
||||
public static enum ListToPointConverter implements Converter<List<Double>, Point> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public Point convert(List<Double> source) {
|
||||
|
||||
Assert.notEmpty(source, "Source must not be empty!");
|
||||
Assert.isTrue(source.size() == 2, "Source must contain 2 elements");
|
||||
|
||||
return source == null ? null : new org.springframework.data.mongodb.core.geo.Point(source.get(0), source.get(1));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a {@link Point} into a {@link List} of {@link Double}s.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
public static enum PointToListConverter implements Converter<Point, List<Double>> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public List<Double> convert(Point source) {
|
||||
return source == null ? null : Arrays.asList(source.getX(), source.getY());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a {@link Box} into a {@link BasicDBList}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
@WritingConverter
|
||||
public static enum BoxToDbObjectConverter implements Converter<Box, BasicDBList> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public BasicDBList convert(Box source) {
|
||||
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
BasicDBList result = new BasicDBList();
|
||||
result.add(toList(source.getFirst()));
|
||||
result.add(toList(source.getSecond()));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a {@link BasicDBList} into a {@link org.springframework.data.mongodb.core.geo.Box}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
@ReadingConverter
|
||||
public static enum DbObjectToBoxConverter implements Converter<BasicDBList, Box> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public Box convert(BasicDBList source) {
|
||||
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new org.springframework.data.mongodb.core.geo.Box(toPoint(source.get(0)), toPoint(source.get(1)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a {@link Circle} into a {@link BasicDBList}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
public static enum CircleToDbObjectConverter implements Converter<Circle, BasicDBList> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public BasicDBList convert(Circle source) {
|
||||
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
BasicDBList result = new BasicDBList();
|
||||
result.add(toList(source.getCenter()));
|
||||
result.add(source.getRadius().getNormalizedValue());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a {@link BasicDBList} into a {@link org.springframework.data.mongodb.core.geo.Circle}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
@ReadingConverter
|
||||
public static enum DbObjectToCircleConverter implements Converter<BasicDBList, Circle> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public Circle convert(BasicDBList source) {
|
||||
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Circle(toPoint(source.get(0)), (Double) source.get(1));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a {@link Circle} into a {@link BasicDBList}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public static enum LegacyCircleToDbObjectConverter implements
|
||||
Converter<org.springframework.data.mongodb.core.geo.Circle, BasicDBList> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public BasicDBList convert(org.springframework.data.mongodb.core.geo.Circle source) {
|
||||
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
BasicDBList result = new BasicDBList();
|
||||
result.add(toList(source.getCenter()));
|
||||
result.add(source.getRadius());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a {@link BasicDBList} into a {@link org.springframework.data.mongodb.core.geo.Circle}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
@ReadingConverter
|
||||
@SuppressWarnings("deprecation")
|
||||
public static enum DbObjectToLegacyCircleConverter implements
|
||||
Converter<BasicDBList, org.springframework.data.mongodb.core.geo.Circle> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public org.springframework.data.mongodb.core.geo.Circle convert(BasicDBList source) {
|
||||
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new org.springframework.data.mongodb.core.geo.Circle(toPoint(source.get(0)), (Double) source.get(1));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a {@link Sphere} into a {@link BasicDBList}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
public static enum SphereToDbObjectConverter implements Converter<Sphere, BasicDBList> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public BasicDBList convert(Sphere source) {
|
||||
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
BasicDBList result = new BasicDBList();
|
||||
result.add(toList(source.getCenter()));
|
||||
result.add(source.getRadius().getNormalizedValue());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a {@link BasicDBList} into a {@link Sphere}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
@ReadingConverter
|
||||
public static enum DbObjectToSphereConverter implements Converter<BasicDBList, Sphere> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public Sphere convert(BasicDBList source) {
|
||||
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Sphere(toPoint(source.get(0)), (Double) source.get(1));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a {@link Polygon} into a {@link BasicDBList}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
public static enum PolygonToDbObjectConverter implements Converter<Polygon, BasicDBList> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public BasicDBList convert(Polygon source) {
|
||||
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Point> points = source.getPoints();
|
||||
List<List<Double>> pointTuples = new ArrayList<List<Double>>(points.size());
|
||||
for (Point point : points) {
|
||||
pointTuples.add(toList(point));
|
||||
}
|
||||
|
||||
BasicDBList result = new BasicDBList();
|
||||
result.addAll(pointTuples);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a {@link BasicDBList} into a {@link org.springframework.data.mongodb.core.geo.Polygon}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
@ReadingConverter
|
||||
public static enum DbObjectToPolygonConverter implements Converter<BasicDBList, Polygon> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public Polygon convert(BasicDBList source) {
|
||||
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Point> points = new ArrayList<Point>(source.size());
|
||||
for (Object element : source) {
|
||||
|
||||
Assert.notNull(element, "point elements of polygon must not be null!");
|
||||
|
||||
points.add(toPoint(element));
|
||||
}
|
||||
|
||||
return new org.springframework.data.mongodb.core.geo.Polygon(points);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given item into a {@link Point}.
|
||||
*
|
||||
* @param item
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Point toPoint(Object item) {
|
||||
|
||||
Assert.isInstanceOf(List.class, item);
|
||||
|
||||
return ListToPointConverter.INSTANCE.convert((List<Double>) item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given {@link Point} into a {@link List} representation with X,Y coordinates as {@link Double}s.
|
||||
*
|
||||
* @param point
|
||||
* @return
|
||||
*/
|
||||
private static List<Double> toList(Point point) {
|
||||
return PointToListConverter.INSTANCE.convert(point);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the geo converters to be registered.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Collection<? extends Object> getConvertersToRegister() {
|
||||
return Arrays.asList( //
|
||||
BoxToDbObjectConverter.INSTANCE //
|
||||
, PolygonToDbObjectConverter.INSTANCE //
|
||||
, CircleToDbObjectConverter.INSTANCE //
|
||||
, LegacyCircleToDbObjectConverter.INSTANCE //
|
||||
, SphereToDbObjectConverter.INSTANCE //
|
||||
, DbObjectToBoxConverter.INSTANCE //
|
||||
, DbObjectToPolygonConverter.INSTANCE //
|
||||
, DbObjectToCircleConverter.INSTANCE //
|
||||
, DbObjectToLegacyCircleConverter.INSTANCE //
|
||||
, DbObjectToSphereConverter.INSTANCE //
|
||||
, ListToPointConverter.INSTANCE //
|
||||
, PointToListConverter.INSTANCE //
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2012 the original author or authors.
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -33,15 +33,14 @@ import com.mongodb.DBObject;
|
||||
* Wrapper class to contain useful converters for the usage with Mongo.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
abstract class MongoConverters {
|
||||
|
||||
/**
|
||||
* Private constructor to prevent instantiation.
|
||||
*/
|
||||
private MongoConverters() {
|
||||
|
||||
}
|
||||
private MongoConverters() {}
|
||||
|
||||
/**
|
||||
* Simple singleton to convert {@link ObjectId}s to their {@link String} representation.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2011 the original author or authors.
|
||||
* Copyright 2010-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,44 +16,31 @@
|
||||
package org.springframework.data.mongodb.core.geo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.mongodb.core.mapping.Field;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.data.geo.Point;
|
||||
|
||||
/**
|
||||
* Represents a geospatial box value
|
||||
* Represents a geospatial box value.
|
||||
*
|
||||
* @deprecated As of release 1.5, replaced by {@link org.springframework.data.geo.Box}. This class is scheduled to be
|
||||
* removed in the next major release.
|
||||
* @author Mark Pollack
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class Box implements Shape {
|
||||
@Deprecated
|
||||
public class Box extends org.springframework.data.geo.Box implements Shape {
|
||||
|
||||
@Field(order = 10)
|
||||
private final Point first;
|
||||
@Field(order = 20)
|
||||
private final Point second;
|
||||
public static final String COMMAND = "$box";
|
||||
|
||||
public Box(Point lowerLeft, Point upperRight) {
|
||||
Assert.notNull(lowerLeft);
|
||||
Assert.notNull(upperRight);
|
||||
this.first = lowerLeft;
|
||||
this.second = upperRight;
|
||||
super(lowerLeft, upperRight);
|
||||
}
|
||||
|
||||
public Box(double[] lowerLeft, double[] upperRight) {
|
||||
Assert.isTrue(lowerLeft.length == 2, "Point array has to have 2 elements!");
|
||||
Assert.isTrue(upperRight.length == 2, "Point array has to have 2 elements!");
|
||||
this.first = new Point(lowerLeft[0], lowerLeft[1]);
|
||||
this.second = new Point(upperRight[0], upperRight[1]);
|
||||
}
|
||||
|
||||
public Point getLowerLeft() {
|
||||
return first;
|
||||
}
|
||||
|
||||
public Point getUpperRight() {
|
||||
return second;
|
||||
super(lowerLeft, upperRight);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -61,46 +48,28 @@ public class Box implements Shape {
|
||||
* @see org.springframework.data.mongodb.core.geo.Shape#asList()
|
||||
*/
|
||||
public List<? extends Object> asList() {
|
||||
|
||||
List<List<Double>> list = new ArrayList<List<Double>>();
|
||||
list.add(getLowerLeft().asList());
|
||||
list.add(getUpperRight().asList());
|
||||
|
||||
list.add(Arrays.asList(getFirst().getX(), getFirst().getY()));
|
||||
list.add(Arrays.asList(getSecond().getX(), getSecond().getY()));
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public org.springframework.data.mongodb.core.geo.Point getLowerLeft() {
|
||||
return new org.springframework.data.mongodb.core.geo.Point(getFirst());
|
||||
}
|
||||
|
||||
public org.springframework.data.mongodb.core.geo.Point getUpperRight() {
|
||||
return new org.springframework.data.mongodb.core.geo.Point(getSecond());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.geo.Shape#getCommand()
|
||||
*/
|
||||
public String getCommand() {
|
||||
return "$box";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Box [%s, %s]", first, second);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = 31;
|
||||
result += 17 * first.hashCode();
|
||||
result += 17 * second.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Box that = (Box) obj;
|
||||
return this.first.equals(that.first) && this.second.equals(that.second);
|
||||
return COMMAND;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2011 the original author or authors.
|
||||
* Copyright 2010-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,19 +16,32 @@
|
||||
package org.springframework.data.mongodb.core.geo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.annotation.PersistenceConstructor;
|
||||
import org.springframework.data.geo.Distance;
|
||||
import org.springframework.data.geo.Metrics;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Represents a geospatial circle value
|
||||
* Represents a geospatial circle value.
|
||||
* <p>
|
||||
* Note: We deliberately do not extend org.springframework.data.geo.Circle because introducing it's distance concept
|
||||
* would break the clients that use the old Circle API.
|
||||
*
|
||||
* @author Mark Pollack
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @deprecated As of release 1.5, replaced by {@link org.springframework.data.geo.Circle}. This class is scheduled to be
|
||||
* removed in the next major release.
|
||||
*/
|
||||
@Deprecated
|
||||
public class Circle implements Shape {
|
||||
|
||||
public static final String COMMAND = "$center";
|
||||
|
||||
private final Point center;
|
||||
private final double radius;
|
||||
|
||||
@@ -49,7 +62,8 @@ public class Circle implements Shape {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Circle} from the given coordinates and radius.
|
||||
* Creates a new {@link Circle} from the given coordinates and radius as {@link Distance} with a
|
||||
* {@link Metrics#NEUTRAL}.
|
||||
*
|
||||
* @param centerX
|
||||
* @param centerY
|
||||
@@ -82,9 +96,11 @@ public class Circle implements Shape {
|
||||
* @see org.springframework.data.mongodb.core.geo.Shape#asList()
|
||||
*/
|
||||
public List<Object> asList() {
|
||||
|
||||
List<Object> result = new ArrayList<Object>();
|
||||
result.add(getCenter().asList());
|
||||
result.add(Arrays.asList(getCenter().getX(), getCenter().getY()));
|
||||
result.add(getRadius());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -93,7 +109,7 @@ public class Circle implements Shape {
|
||||
* @see org.springframework.data.mongodb.core.geo.Shape#getCommand()
|
||||
*/
|
||||
public String getCommand() {
|
||||
return "$center";
|
||||
return COMMAND;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,11 +18,13 @@ package org.springframework.data.mongodb.core.geo;
|
||||
/**
|
||||
* Value object to create custom {@link Metric}s on the fly.
|
||||
*
|
||||
* @deprecated As of release 1.5, replaced by {@link org.springframework.data.geo.Metric}. This class is scheduled to be
|
||||
* removed in the next major release.
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class CustomMetric implements Metric {
|
||||
|
||||
private final double multiplier;
|
||||
@Deprecated
|
||||
public class CustomMetric extends org.springframework.data.geo.CustomMetric implements Metric {
|
||||
|
||||
/**
|
||||
* Creates a custom {@link Metric} using the given multiplier.
|
||||
@@ -30,14 +32,6 @@ public class CustomMetric implements Metric {
|
||||
* @param multiplier
|
||||
*/
|
||||
public CustomMetric(double multiplier) {
|
||||
this.multiplier = multiplier;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.geo.Metric#getMultiplier()
|
||||
*/
|
||||
public double getMultiplier() {
|
||||
return multiplier;
|
||||
super(multiplier);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2011 the original author or authors.
|
||||
* Copyright 2010-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,17 +15,19 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.geo;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.data.geo.Metric;
|
||||
import org.springframework.data.geo.Metrics;
|
||||
|
||||
/**
|
||||
* Value object to represent distances in a given metric.
|
||||
*
|
||||
* @deprecated As of release 1.5, replaced by {@link org.springframework.data.geo.Distance}. This class is scheduled to
|
||||
* be removed in the next major release.
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class Distance {
|
||||
|
||||
private final double value;
|
||||
private final Metric metric;
|
||||
@Deprecated
|
||||
public class Distance extends org.springframework.data.geo.Distance {
|
||||
|
||||
/**
|
||||
* Creates a new {@link Distance}.
|
||||
@@ -36,110 +38,7 @@ public class Distance {
|
||||
this(value, Metrics.NEUTRAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Distance} with the given {@link Metric}.
|
||||
*
|
||||
* @param value
|
||||
* @param metric
|
||||
*/
|
||||
public Distance(double value, Metric metric) {
|
||||
this.value = value;
|
||||
this.metric = metric == null ? Metrics.NEUTRAL : metric;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the value
|
||||
*/
|
||||
public double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the normalized value regarding the underlying {@link Metric}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public double getNormalizedValue() {
|
||||
return value / metric.getMultiplier();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the metric
|
||||
*/
|
||||
public Metric getMetric() {
|
||||
return metric;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given distance to the current one. The resulting {@link Distance} will be in the same metric as the
|
||||
* current one.
|
||||
*
|
||||
* @param other
|
||||
* @return
|
||||
*/
|
||||
public Distance add(Distance other) {
|
||||
double newNormalizedValue = getNormalizedValue() + other.getNormalizedValue();
|
||||
return new Distance(newNormalizedValue * metric.getMultiplier(), metric);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given {@link Distance} to the current one and forces the result to be in a given {@link Metric}.
|
||||
*
|
||||
* @param other
|
||||
* @param metric
|
||||
* @return
|
||||
*/
|
||||
public Distance add(Distance other, Metric metric) {
|
||||
double newLeft = getNormalizedValue() * metric.getMultiplier();
|
||||
double newRight = other.getNormalizedValue() * metric.getMultiplier();
|
||||
return new Distance(newLeft + newRight, metric);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (obj == null || !getClass().equals(obj.getClass())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Distance that = (Distance) obj;
|
||||
|
||||
return this.value == that.value && ObjectUtils.nullSafeEquals(this.metric, that.metric);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 17;
|
||||
result += 31 * Double.doubleToLongBits(value);
|
||||
result += 31 * ObjectUtils.nullSafeHashCode(metric);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(value);
|
||||
|
||||
if (metric != Metrics.NEUTRAL) {
|
||||
builder.append(" ").append(metric.toString());
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
super(value, metric);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,19 +16,21 @@
|
||||
package org.springframework.data.mongodb.core.geo;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
/**
|
||||
* Custom {@link Page} to carry the average distance retrieved from the {@link GeoResults} the {@link GeoPage} is set up
|
||||
* from.
|
||||
*
|
||||
* @deprecated As of release 1.5, replaced by {@link org.springframework.data.geo.GeoPage}. This class is scheduled to
|
||||
* be removed in the next major release.
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class GeoPage<T> extends PageImpl<GeoResult<T>> {
|
||||
@Deprecated
|
||||
public class GeoPage<T> extends org.springframework.data.geo.GeoPage<T> {
|
||||
|
||||
private static final long serialVersionUID = 23421312312412L;
|
||||
private final Distance averageDistance;
|
||||
|
||||
/**
|
||||
* Creates a new {@link GeoPage} from the given {@link GeoResults}.
|
||||
@@ -36,8 +38,7 @@ public class GeoPage<T> extends PageImpl<GeoResult<T>> {
|
||||
* @param content must not be {@literal null}.
|
||||
*/
|
||||
public GeoPage(GeoResults<T> results) {
|
||||
super(results.getContent());
|
||||
this.averageDistance = results.getAverageDistance();
|
||||
super(results);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,16 +49,6 @@ public class GeoPage<T> extends PageImpl<GeoResult<T>> {
|
||||
* @param total
|
||||
*/
|
||||
public GeoPage(GeoResults<T> results, Pageable pageable, long total) {
|
||||
super(results.getContent(), pageable, total);
|
||||
this.averageDistance = results.getAverageDistance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the average distance of the underlying results.
|
||||
*
|
||||
* @return the averageDistance
|
||||
*/
|
||||
public Distance getAverageDistance() {
|
||||
return averageDistance;
|
||||
super(results, pageable, total);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,17 +15,16 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.geo;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Calue object capturing some arbitrary object plus a distance.
|
||||
*
|
||||
* @deprecated As of release 1.5, replaced by {@link org.springframework.data.geo.GeoResult}. This class is scheduled to
|
||||
* be removed in the next major release.
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class GeoResult<T> {
|
||||
|
||||
private final T content;
|
||||
private final Distance distance;
|
||||
@Deprecated
|
||||
public class GeoResult<T> extends org.springframework.data.geo.GeoResult<T> {
|
||||
|
||||
/**
|
||||
* Creates a new {@link GeoResult} for the given content and distance.
|
||||
@@ -34,69 +33,6 @@ public class GeoResult<T> {
|
||||
* @param distance must not be {@literal null}.
|
||||
*/
|
||||
public GeoResult(T content, Distance distance) {
|
||||
Assert.notNull(content);
|
||||
Assert.notNull(distance);
|
||||
this.content = content;
|
||||
this.distance = distance;
|
||||
super(content, distance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the actual content object.
|
||||
*
|
||||
* @return the content
|
||||
*/
|
||||
public T getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the distance the actual content object has from the origin.
|
||||
*
|
||||
* @return the distance
|
||||
*/
|
||||
public Distance getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (obj == null || !getClass().equals(obj.getClass())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GeoResult<?> that = (GeoResult<?>) obj;
|
||||
|
||||
return this.content.equals(that.content) && this.distance.equals(that.distance);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
||||
int result = 17;
|
||||
result += 31 * distance.hashCode();
|
||||
result += 31 * content.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("GeoResult [content: %s, distance: %s, ]", content.toString(), distance.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,23 +15,23 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.geo;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.annotation.PersistenceConstructor;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.data.geo.Distance;
|
||||
import org.springframework.data.geo.GeoResult;
|
||||
import org.springframework.data.geo.Metric;
|
||||
|
||||
/**
|
||||
* Value object to capture {@link GeoResult}s as well as the average distance they have.
|
||||
*
|
||||
* @deprecated As of release 1.5, replaced by {@link org.springframework.data.geo.GeoResults}. This class is scheduled
|
||||
* to be removed in the next major release.
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class GeoResults<T> implements Iterable<GeoResult<T>> {
|
||||
|
||||
private final List<GeoResult<T>> results;
|
||||
private final Distance averageDistance;
|
||||
@Deprecated
|
||||
public class GeoResults<T> extends org.springframework.data.geo.GeoResults<T> {
|
||||
|
||||
/**
|
||||
* Creates a new {@link GeoResults} instance manually calculating the average distance from the distance values of the
|
||||
@@ -39,12 +39,12 @@ public class GeoResults<T> implements Iterable<GeoResult<T>> {
|
||||
*
|
||||
* @param results must not be {@literal null}.
|
||||
*/
|
||||
public GeoResults(List<GeoResult<T>> results) {
|
||||
this(results, (Metric) null);
|
||||
public GeoResults(List<? extends GeoResult<T>> results) {
|
||||
super(results);
|
||||
}
|
||||
|
||||
public GeoResults(List<GeoResult<T>> results, Metric metric) {
|
||||
this(results, calculateAverageDistance(results, metric));
|
||||
public GeoResults(List<? extends GeoResult<T>> results, Metric metric) {
|
||||
super(results, metric);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,92 +54,7 @@ public class GeoResults<T> implements Iterable<GeoResult<T>> {
|
||||
* @param averageDistance
|
||||
*/
|
||||
@PersistenceConstructor
|
||||
public GeoResults(List<GeoResult<T>> results, Distance averageDistance) {
|
||||
Assert.notNull(results);
|
||||
this.results = results;
|
||||
this.averageDistance = averageDistance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the average distance of all {@link GeoResult}s in this list.
|
||||
*
|
||||
* @return the averageDistance
|
||||
*/
|
||||
public Distance getAverageDistance() {
|
||||
return averageDistance;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Iterable#iterator()
|
||||
*/
|
||||
public Iterator<GeoResult<T>> iterator() {
|
||||
return results.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the actual
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<GeoResult<T>> getContent() {
|
||||
return Collections.unmodifiableList(results);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (obj == null || !getClass().equals(obj.getClass())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
GeoResults<?> that = (GeoResults<?>) obj;
|
||||
|
||||
return this.results.equals(that.results) && this.averageDistance == that.averageDistance;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 17;
|
||||
result += 31 * results.hashCode();
|
||||
result += 31 * averageDistance.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("GeoResults: [averageDistance: %s, results: %s]", averageDistance.toString(),
|
||||
StringUtils.collectionToCommaDelimitedString(results));
|
||||
}
|
||||
|
||||
private static Distance calculateAverageDistance(List<? extends GeoResult<?>> results, Metric metric) {
|
||||
|
||||
if (results.isEmpty()) {
|
||||
return new Distance(0, metric);
|
||||
}
|
||||
|
||||
double averageDistance = 0;
|
||||
|
||||
for (GeoResult<?> result : results) {
|
||||
averageDistance += result.getDistance().getValue();
|
||||
}
|
||||
|
||||
return new Distance(averageDistance / results.size(), metric);
|
||||
public GeoResults(List<? extends GeoResult<T>> results, Distance averageDistance) {
|
||||
super(results, averageDistance);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,27 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.geo;
|
||||
|
||||
/**
|
||||
* Interface for {@link Metric}s that can be applied to a base scale.
|
||||
*
|
||||
* @deprecated As of release 1.5, replaced by {@link org.springframework.data.geo.Metric}. This class is scheduled to be
|
||||
* removed in the next major release.
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public interface Metric {
|
||||
|
||||
/**
|
||||
* Returns the multiplier to calculate metrics values from a base scale.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
double getMultiplier();
|
||||
}
|
||||
@Deprecated
|
||||
public interface Metric extends org.springframework.data.geo.Metric {}
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.geo;
|
||||
|
||||
import org.springframework.data.mongodb.core.query.NearQuery;
|
||||
@@ -5,11 +20,17 @@ import org.springframework.data.mongodb.core.query.NearQuery;
|
||||
/**
|
||||
* Commonly used {@link Metrics} for {@link NearQuery}s.
|
||||
*
|
||||
* @deprecated As of release 1.5, replaced by {@link org.springframework.data.geo.Metrics}. This class is scheduled to
|
||||
* be removed in the next major release.
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@Deprecated
|
||||
public enum Metrics implements Metric {
|
||||
|
||||
KILOMETERS(6378.137), MILES(3963.191), NEUTRAL(1);
|
||||
KILOMETERS(org.springframework.data.geo.Metrics.KILOMETERS.getMultiplier()), //
|
||||
MILES(org.springframework.data.geo.Metrics.MILES.getMultiplier()), //
|
||||
NEUTRAL(org.springframework.data.geo.Metrics.NEUTRAL.getMultiplier()); //
|
||||
|
||||
private final double multiplier;
|
||||
|
||||
@@ -24,4 +45,4 @@ public enum Metrics implements Metric {
|
||||
public double getMultiplier() {
|
||||
return multiplier;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2011 the original author or authors.
|
||||
* Copyright 2010-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,85 +19,37 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.annotation.PersistenceConstructor;
|
||||
import org.springframework.data.mongodb.core.mapping.Field;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Represents a geospatial point value.
|
||||
*
|
||||
* @deprecated As of release 1.5, replaced by {@link org.springframework.data.geo.Point}. This class is scheduled to be
|
||||
* removed in the next major release.
|
||||
* @author Mark Pollack
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class Point {
|
||||
|
||||
@Field(order = 10)
|
||||
private final double x;
|
||||
@Field(order = 20)
|
||||
private final double y;
|
||||
@Deprecated
|
||||
public class Point extends org.springframework.data.geo.Point {
|
||||
|
||||
@PersistenceConstructor
|
||||
public Point(double x, double y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
super(x, y);
|
||||
}
|
||||
|
||||
public Point(Point point) {
|
||||
Assert.notNull(point);
|
||||
this.x = point.x;
|
||||
this.y = point.y;
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public double getY() {
|
||||
return y;
|
||||
public Point(org.springframework.data.geo.Point point) {
|
||||
super(point);
|
||||
}
|
||||
|
||||
public double[] asArray() {
|
||||
return new double[] { x, y };
|
||||
return new double[] { getX(), getY() };
|
||||
}
|
||||
|
||||
public List<Double> asList() {
|
||||
return Arrays.asList(x, y);
|
||||
return asList(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(x);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
temp = Double.doubleToLongBits(y);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Point other = (Point) obj;
|
||||
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x)) {
|
||||
return false;
|
||||
}
|
||||
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Point [latitude=%f, longitude=%f]", x, y);
|
||||
public static List<Double> asList(org.springframework.data.geo.Point point) {
|
||||
return Arrays.asList(point.getX(), point.getY());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,19 +17,22 @@ package org.springframework.data.mongodb.core.geo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.data.geo.Point;
|
||||
|
||||
/**
|
||||
* Simple value object to represent a {@link Polygon}.
|
||||
*
|
||||
* @deprecated As of release 1.5, replaced by {@link org.springframework.data.geo.Point}. This class is scheduled to be
|
||||
* removed in the next major release.
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class Polygon implements Shape, Iterable<Point> {
|
||||
@Deprecated
|
||||
public class Polygon extends org.springframework.data.geo.Polygon implements Shape {
|
||||
|
||||
private final List<Point> points;
|
||||
public static final String COMMAND = "$polygon";
|
||||
|
||||
/**
|
||||
* Creates a new {@link Polygon} for the given Points.
|
||||
@@ -39,31 +42,17 @@ public class Polygon implements Shape, Iterable<Point> {
|
||||
* @param z
|
||||
* @param others
|
||||
*/
|
||||
public Polygon(Point x, Point y, Point z, Point... others) {
|
||||
|
||||
Assert.notNull(x);
|
||||
Assert.notNull(y);
|
||||
Assert.notNull(z);
|
||||
Assert.notNull(others);
|
||||
|
||||
this.points = new ArrayList<Point>(3 + others.length);
|
||||
this.points.addAll(Arrays.asList(x, y, z));
|
||||
this.points.addAll(Arrays.asList(others));
|
||||
public <P extends Point> Polygon(P x, P y, P z, P... others) {
|
||||
super(x, y, z, others);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.geo.Shape#asList()
|
||||
/**
|
||||
* Creates a new {@link Polygon} for the given Points.
|
||||
*
|
||||
* @param points
|
||||
*/
|
||||
public List<List<Double>> asList() {
|
||||
|
||||
List<List<Double>> result = new ArrayList<List<Double>>();
|
||||
|
||||
for (Point point : points) {
|
||||
result.add(point.asList());
|
||||
}
|
||||
|
||||
return result;
|
||||
public <P extends Point> Polygon(List<P> points) {
|
||||
super(points);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -71,43 +60,33 @@ public class Polygon implements Shape, Iterable<Point> {
|
||||
* @see org.springframework.data.mongodb.core.geo.Shape#getCommand()
|
||||
*/
|
||||
public String getCommand() {
|
||||
return "$polygon";
|
||||
return COMMAND;
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Iterable#iterator()
|
||||
*/
|
||||
public Iterator<Point> iterator() {
|
||||
return this.points.iterator();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
* @see org.springframework.data.mongodb.core.geo.Shape#asList()
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (obj == null || !getClass().equals(obj.getClass())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Polygon that = (Polygon) obj;
|
||||
|
||||
return this.points.equals(that.points);
|
||||
public List<? extends Object> asList() {
|
||||
return asList(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
/**
|
||||
* Returns a {@link List} of x,y-coordinate tuples of {@link Point}s from the given {@link Polygon}.
|
||||
*
|
||||
* @param polygon
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return points.hashCode();
|
||||
public static List<? extends Object> asList(org.springframework.data.geo.Polygon polygon) {
|
||||
|
||||
List<Point> points = polygon.getPoints();
|
||||
List<List<Double>> tuples = new ArrayList<List<Double>>(points.size());
|
||||
|
||||
for (Point point : points) {
|
||||
tuples.add(Arrays.asList(point.getX(), point.getY()));
|
||||
}
|
||||
|
||||
return tuples;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,9 +20,13 @@ import java.util.List;
|
||||
/**
|
||||
* Common interface for all shapes. Allows building MongoDB representations of them.
|
||||
*
|
||||
* @deprecated As of release 1.5, replaced by {@link org.springframework.data.geo.Shape}. This class is scheduled to be
|
||||
* removed in the next major release.
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public interface Shape {
|
||||
@Deprecated
|
||||
public interface Shape extends org.springframework.data.geo.Shape {
|
||||
|
||||
/**
|
||||
* Returns the {@link Shape} as a list of usually {@link Double} or {@link List}s of {@link Double}s. Wildcard bound
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.geo;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.annotation.PersistenceConstructor;
|
||||
import org.springframework.data.geo.Circle;
|
||||
import org.springframework.data.geo.Distance;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Represents a geospatial sphere value.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
public class Sphere implements Shape {
|
||||
|
||||
public static final String COMMAND = "$centerSphere";
|
||||
private final Point center;
|
||||
private final Distance radius;
|
||||
|
||||
/**
|
||||
* Creates a Sphere around the given center {@link Point} with the given radius.
|
||||
*
|
||||
* @param center must not be {@literal null}.
|
||||
* @param radius must not be {@literal null}.
|
||||
*/
|
||||
@PersistenceConstructor
|
||||
public Sphere(Point center, Distance radius) {
|
||||
|
||||
Assert.notNull(center);
|
||||
Assert.notNull(radius);
|
||||
Assert.isTrue(radius.getValue() >= 0, "Radius must not be negative!");
|
||||
|
||||
this.center = center;
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Sphere around the given center {@link Point} with the given radius.
|
||||
*
|
||||
* @param center
|
||||
* @param radius
|
||||
*/
|
||||
public Sphere(Point center, double radius) {
|
||||
this(center, new Distance(radius));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Sphere from the given {@link Circle}.
|
||||
*
|
||||
* @param circle
|
||||
*/
|
||||
public Sphere(Circle circle) {
|
||||
this(circle.getCenter(), circle.getRadius());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Sphere from the given {@link Circle}.
|
||||
*
|
||||
* @param circle
|
||||
*/
|
||||
@Deprecated
|
||||
public Sphere(org.springframework.data.mongodb.core.geo.Circle circle) {
|
||||
this(circle.getCenter(), circle.getRadius());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the center of the {@link Circle}.
|
||||
*
|
||||
* @return will never be {@literal null}.
|
||||
*/
|
||||
public org.springframework.data.mongodb.core.geo.Point getCenter() {
|
||||
return new org.springframework.data.mongodb.core.geo.Point(this.center);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the radius of the {@link Circle}.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Distance getRadius() {
|
||||
return radius;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Sphere [center=%s, radius=%s]", center, radius);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (obj == null || !(obj instanceof Sphere)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Sphere that = (Sphere) obj;
|
||||
|
||||
return this.center.equals(that.center) && this.radius.equals(that.radius);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 17;
|
||||
result += 31 * center.hashCode();
|
||||
result += 31 * radius.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.geo.Shape#asList()
|
||||
*/
|
||||
@Override
|
||||
public List<? extends Object> asList() {
|
||||
return Arrays.asList(Arrays.asList(center.getX(), center.getY()), this.radius.getValue());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.geo.Shape#getCommand()
|
||||
*/
|
||||
@Override
|
||||
public String getCommand() {
|
||||
return COMMAND;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/**
|
||||
* Support for MongoDB geo-spatial queries.
|
||||
*/
|
||||
|
||||
@@ -25,10 +25,12 @@ import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.bson.BSON;
|
||||
import org.springframework.data.geo.Circle;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.data.geo.Shape;
|
||||
import org.springframework.data.mongodb.InvalidMongoDbApiUsageException;
|
||||
import org.springframework.data.mongodb.core.geo.Circle;
|
||||
import org.springframework.data.mongodb.core.geo.Point;
|
||||
import org.springframework.data.mongodb.core.geo.Shape;
|
||||
import org.springframework.data.mongodb.core.convert.GeoCommandUtils;
|
||||
import org.springframework.data.mongodb.core.geo.Sphere;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
@@ -383,7 +385,21 @@ public class Criteria implements CriteriaDefinition {
|
||||
*/
|
||||
public Criteria withinSphere(Circle circle) {
|
||||
Assert.notNull(circle);
|
||||
criteria.put("$within", new BasicDBObject("$centerSphere", circle.asList()));
|
||||
criteria.put("$within", GeoCommandUtils.INSTANCE.wrapInCommand(new Sphere(circle)));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Criteria#withinSphere(Circle)
|
||||
* @param circle
|
||||
* @return
|
||||
* @deprecated As of 1.5, Use {@link #withinSphere(Circle)}. This method is scheduled to be removed in the next major
|
||||
* release.
|
||||
*/
|
||||
@Deprecated
|
||||
public Criteria withinSphere(org.springframework.data.mongodb.core.geo.Circle circle) {
|
||||
Assert.notNull(circle);
|
||||
criteria.put("$within", GeoCommandUtils.INSTANCE.wrapInCommand(new Sphere(circle)));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -397,7 +413,7 @@ public class Criteria implements CriteriaDefinition {
|
||||
public Criteria within(Shape shape) {
|
||||
|
||||
Assert.notNull(shape);
|
||||
criteria.put("$within", new BasicDBObject(shape.getCommand(), shape.asList()));
|
||||
criteria.put("$within", GeoCommandUtils.INSTANCE.wrapInCommand(shape));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -410,7 +426,7 @@ public class Criteria implements CriteriaDefinition {
|
||||
*/
|
||||
public Criteria near(Point point) {
|
||||
Assert.notNull(point);
|
||||
criteria.put("$near", point.asList());
|
||||
criteria.put("$near", point);
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -424,7 +440,7 @@ public class Criteria implements CriteriaDefinition {
|
||||
*/
|
||||
public Criteria nearSphere(Point point) {
|
||||
Assert.notNull(point);
|
||||
criteria.put("$nearSphere", point.asList());
|
||||
criteria.put("$nearSphere", point);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,12 +15,14 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.query;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.mongodb.core.geo.CustomMetric;
|
||||
import org.springframework.data.mongodb.core.geo.Distance;
|
||||
import org.springframework.data.mongodb.core.geo.Metric;
|
||||
import org.springframework.data.mongodb.core.geo.Metrics;
|
||||
import org.springframework.data.mongodb.core.geo.Point;
|
||||
import org.springframework.data.geo.CustomMetric;
|
||||
import org.springframework.data.geo.Distance;
|
||||
import org.springframework.data.geo.Metric;
|
||||
import org.springframework.data.geo.Metrics;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
@@ -361,7 +363,8 @@ public final class NearQuery {
|
||||
dbObject.put("num", num);
|
||||
}
|
||||
|
||||
dbObject.put("near", point.asList());
|
||||
dbObject.put("near", Arrays.asList(point.getX(), point.getY()));
|
||||
|
||||
dbObject.put("spherical", spherical);
|
||||
|
||||
return dbObject;
|
||||
|
||||
@@ -23,8 +23,7 @@ public class PersonWithVersionPropertyOfTypeLong {
|
||||
String firstName;
|
||||
int age;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
@Version Long version;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.convert;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.geo.Box;
|
||||
import org.springframework.data.geo.Circle;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.data.geo.Polygon;
|
||||
import org.springframework.data.mongodb.core.convert.GeoConverters.BoxToDbObjectConverter;
|
||||
import org.springframework.data.mongodb.core.convert.GeoConverters.CircleToDbObjectConverter;
|
||||
import org.springframework.data.mongodb.core.convert.GeoConverters.DbObjectToBoxConverter;
|
||||
import org.springframework.data.mongodb.core.convert.GeoConverters.DbObjectToCircleConverter;
|
||||
import org.springframework.data.mongodb.core.convert.GeoConverters.DbObjectToLegacyCircleConverter;
|
||||
import org.springframework.data.mongodb.core.convert.GeoConverters.DbObjectToPolygonConverter;
|
||||
import org.springframework.data.mongodb.core.convert.GeoConverters.DbObjectToSphereConverter;
|
||||
import org.springframework.data.mongodb.core.convert.GeoConverters.LegacyCircleToDbObjectConverter;
|
||||
import org.springframework.data.mongodb.core.convert.GeoConverters.ListToPointConverter;
|
||||
import org.springframework.data.mongodb.core.convert.GeoConverters.PointToListConverter;
|
||||
import org.springframework.data.mongodb.core.convert.GeoConverters.PolygonToDbObjectConverter;
|
||||
import org.springframework.data.mongodb.core.convert.GeoConverters.SphereToDbObjectConverter;
|
||||
import org.springframework.data.mongodb.core.geo.Sphere;
|
||||
|
||||
import com.mongodb.BasicDBList;
|
||||
|
||||
/**
|
||||
* @author Thomas Darimont
|
||||
* @since 1.5
|
||||
*/
|
||||
public class GeoConvertersUnitTests {
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void convertsBoxToDbObjectAndBackCorrectly() {
|
||||
|
||||
Box box = new Box(new Point(1, 2), new Point(3, 4));
|
||||
|
||||
BasicDBList dbo = BoxToDbObjectConverter.INSTANCE.convert(box);
|
||||
Box result = DbObjectToBoxConverter.INSTANCE.convert(dbo);
|
||||
|
||||
assertThat(result, is(box));
|
||||
assertThat(result.getClass().equals(org.springframework.data.mongodb.core.geo.Box.class), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void convertsCircleToDbObjectAndBackCorrectly() {
|
||||
|
||||
Circle circle = new Circle(new Point(1, 2), 3);
|
||||
|
||||
BasicDBList dbo = CircleToDbObjectConverter.INSTANCE.convert(circle);
|
||||
Circle result = DbObjectToCircleConverter.INSTANCE.convert(dbo);
|
||||
|
||||
assertThat(result, is(circle));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void convertsLegacyCircleToDbObjectAndBackCorrectly() {
|
||||
|
||||
org.springframework.data.mongodb.core.geo.Circle circle = new org.springframework.data.mongodb.core.geo.Circle(
|
||||
new Point(1, 2), 3);
|
||||
|
||||
BasicDBList dbo = LegacyCircleToDbObjectConverter.INSTANCE.convert(circle);
|
||||
org.springframework.data.mongodb.core.geo.Circle result = DbObjectToLegacyCircleConverter.INSTANCE.convert(dbo);
|
||||
|
||||
assertThat(result, is(circle));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void convertsPolygonToDbObjectAndBackCorrectly() {
|
||||
|
||||
Polygon polygon = new Polygon(new Point(1, 2), new Point(2, 3), new Point(3, 4), new Point(5, 6));
|
||||
|
||||
BasicDBList dbo = PolygonToDbObjectConverter.INSTANCE.convert(polygon);
|
||||
Polygon result = DbObjectToPolygonConverter.INSTANCE.convert(dbo);
|
||||
|
||||
assertThat(result, is(polygon));
|
||||
assertThat(result.getClass().equals(org.springframework.data.mongodb.core.geo.Polygon.class), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void convertsSphereToDbObjectAndBackCorrectly() {
|
||||
|
||||
Sphere sphere = new Sphere(new Point(1, 2), 3);
|
||||
|
||||
BasicDBList dbo = SphereToDbObjectConverter.INSTANCE.convert(sphere);
|
||||
Sphere result = DbObjectToSphereConverter.INSTANCE.convert(dbo);
|
||||
|
||||
assertThat(result, is(sphere));
|
||||
assertThat(result.getClass().equals(org.springframework.data.mongodb.core.geo.Sphere.class), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void convertsPointToListAndBackCorrectly() {
|
||||
|
||||
Point point = new Point(1, 2);
|
||||
|
||||
List<Double> list = PointToListConverter.INSTANCE.convert(point);
|
||||
Point result = ListToPointConverter.INSTANCE.convert(list);
|
||||
|
||||
assertThat(result, is((Point) point));
|
||||
assertThat(result.getClass().equals(org.springframework.data.mongodb.core.geo.Point.class), is(true));
|
||||
}
|
||||
}
|
||||
@@ -53,11 +53,17 @@ import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.PersistenceConstructor;
|
||||
import org.springframework.data.annotation.TypeAlias;
|
||||
import org.springframework.data.geo.Box;
|
||||
import org.springframework.data.geo.Circle;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.data.geo.Polygon;
|
||||
import org.springframework.data.geo.Shape;
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
import org.springframework.data.mapping.model.MappingInstantiationException;
|
||||
import org.springframework.data.mongodb.core.DBObjectTestUtils;
|
||||
import org.springframework.data.mongodb.core.convert.DBObjectAccessorUnitTests.NestedType;
|
||||
import org.springframework.data.mongodb.core.convert.DBObjectAccessorUnitTests.ProjectingType;
|
||||
import org.springframework.data.mongodb.core.geo.Sphere;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
import org.springframework.data.mongodb.core.mapping.Field;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
@@ -1485,7 +1491,7 @@ public class MappingMongoConverterUnitTests {
|
||||
assertThat(result.enumMap.size(), is(1));
|
||||
assertThat(result.enumMap.get(SampleEnum.FIRST), is("Dave"));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-887
|
||||
*/
|
||||
@@ -1524,6 +1530,226 @@ public class MappingMongoConverterUnitTests {
|
||||
DBObject entry = getAsDBObject(map, "key");
|
||||
assertThat(entry.get("foo"), is((Object) "Dave"));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void shouldWriteEntityWithGeoBoxCorrectly() {
|
||||
|
||||
ClassWithGeoBox object = new ClassWithGeoBox();
|
||||
object.box = new Box(new Point(1, 2), new Point(3, 4));
|
||||
|
||||
DBObject dbo = new BasicDBObject();
|
||||
converter.write(object, dbo);
|
||||
|
||||
assertThat(dbo, is(notNullValue()));
|
||||
assertThat(dbo.get("box"), is(instanceOf(List.class)));
|
||||
assertThat(dbo.get("box"), is((Object) Arrays.asList(
|
||||
org.springframework.data.mongodb.core.geo.Point.asList(object.box.getFirst()),
|
||||
org.springframework.data.mongodb.core.geo.Point.asList(object.box.getSecond()))));
|
||||
}
|
||||
|
||||
/**
|
||||
* @DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void shouldReadEntityWithGeoBoxCorrectly() {
|
||||
|
||||
ClassWithGeoBox object = new ClassWithGeoBox();
|
||||
object.box = new Box(new Point(1, 2), new Point(3, 4));
|
||||
|
||||
DBObject dbo = new BasicDBObject();
|
||||
converter.write(object, dbo);
|
||||
|
||||
ClassWithGeoBox result = converter.read(ClassWithGeoBox.class, dbo);
|
||||
|
||||
assertThat(result, is(notNullValue()));
|
||||
assertThat(result.box, is(object.box));
|
||||
}
|
||||
|
||||
/**
|
||||
* @DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void shouldWriteEntityWithGeoPolygonCorrectly() {
|
||||
|
||||
ClassWithGeoPolygon object = new ClassWithGeoPolygon();
|
||||
object.polygon = new Polygon(new Point(1, 2), new Point(3, 4), new Point(4, 5));
|
||||
|
||||
DBObject dbo = new BasicDBObject();
|
||||
converter.write(object, dbo);
|
||||
|
||||
assertThat(dbo, is(notNullValue()));
|
||||
assertThat(dbo.get("polygon"), is(instanceOf(List.class)));
|
||||
assertThat(dbo.get("polygon"),
|
||||
is((Object) org.springframework.data.mongodb.core.geo.Polygon.asList(object.polygon)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void shouldReadEntityWithGeoPolygonCorrectly() {
|
||||
|
||||
ClassWithGeoPolygon object = new ClassWithGeoPolygon();
|
||||
object.polygon = new Polygon(new Point(1, 2), new Point(3, 4), new Point(4, 5));
|
||||
|
||||
DBObject dbo = new BasicDBObject();
|
||||
converter.write(object, dbo);
|
||||
|
||||
ClassWithGeoPolygon result = converter.read(ClassWithGeoPolygon.class, dbo);
|
||||
|
||||
assertThat(result, is(notNullValue()));
|
||||
assertThat(result.polygon, is(object.polygon));
|
||||
}
|
||||
|
||||
/**
|
||||
* @DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void shouldWriteEntityWithGeoCircleCorrectly() {
|
||||
|
||||
ClassWithGeoCircle object = new ClassWithGeoCircle();
|
||||
object.circle = new Circle(new Point(1, 2), 3);
|
||||
|
||||
DBObject dbo = new BasicDBObject();
|
||||
converter.write(object, dbo);
|
||||
|
||||
assertThat(dbo, is(notNullValue()));
|
||||
assertThat(dbo.get("circle"), is(instanceOf(List.class)));
|
||||
assertThat(dbo.get("circle"), is((Object) Arrays.asList(org.springframework.data.mongodb.core.geo.Point
|
||||
.asList(object.circle.getCenter()), object.circle.getRadius().getNormalizedValue())));
|
||||
}
|
||||
|
||||
/**
|
||||
* @DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void shouldReadEntityWithGeoCircleCorrectly() {
|
||||
|
||||
ClassWithGeoCircle object = new ClassWithGeoCircle();
|
||||
object.circle = new Circle(new Point(1, 2), 3);
|
||||
|
||||
DBObject dbo = new BasicDBObject();
|
||||
converter.write(object, dbo);
|
||||
|
||||
ClassWithGeoCircle result = converter.read(ClassWithGeoCircle.class, dbo);
|
||||
|
||||
assertThat(result, is(notNullValue()));
|
||||
assertThat(result.circle, is(result.circle));
|
||||
}
|
||||
|
||||
/**
|
||||
* @DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void shouldWriteEntityWithGeoLegacyCircleCorrectly() {
|
||||
|
||||
ClassWithGeoLegacyCircle object = new ClassWithGeoLegacyCircle();
|
||||
object.circle = new org.springframework.data.mongodb.core.geo.Circle(new Point(1, 2), 3);
|
||||
|
||||
DBObject dbo = new BasicDBObject();
|
||||
converter.write(object, dbo);
|
||||
|
||||
assertThat(dbo, is(notNullValue()));
|
||||
assertThat(dbo.get("circle"), is(instanceOf(List.class)));
|
||||
assertThat(dbo.get("circle"), is((Object) Arrays.asList(
|
||||
org.springframework.data.mongodb.core.geo.Point.asList(object.circle.getCenter()), object.circle.getRadius())));
|
||||
}
|
||||
|
||||
/**
|
||||
* @DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void shouldReadEntityWithGeoLegacyCircleCorrectly() {
|
||||
|
||||
ClassWithGeoLegacyCircle object = new ClassWithGeoLegacyCircle();
|
||||
object.circle = new org.springframework.data.mongodb.core.geo.Circle(new Point(1, 2), 3);
|
||||
|
||||
DBObject dbo = new BasicDBObject();
|
||||
converter.write(object, dbo);
|
||||
|
||||
ClassWithGeoLegacyCircle result = converter.read(ClassWithGeoLegacyCircle.class, dbo);
|
||||
|
||||
assertThat(result, is(notNullValue()));
|
||||
assertThat(result.circle, is(result.circle));
|
||||
}
|
||||
|
||||
/**
|
||||
* @DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void shouldWriteEntityWithGeoSphereCorrectly() {
|
||||
|
||||
ClassWithGeoSphere object = new ClassWithGeoSphere();
|
||||
object.sphere = new Sphere(new Point(1, 2), 3);
|
||||
|
||||
DBObject dbo = new BasicDBObject();
|
||||
converter.write(object, dbo);
|
||||
|
||||
assertThat(dbo, is(notNullValue()));
|
||||
assertThat(dbo.get("sphere"), is(instanceOf(List.class)));
|
||||
assertThat(dbo.get("sphere"), is((Object) Arrays.asList(org.springframework.data.mongodb.core.geo.Point
|
||||
.asList(object.sphere.getCenter()), object.sphere.getRadius().getNormalizedValue())));
|
||||
}
|
||||
|
||||
/**
|
||||
* @DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void shouldReadEntityWithGeoSphereCorrectly() {
|
||||
|
||||
ClassWithGeoSphere object = new ClassWithGeoSphere();
|
||||
object.sphere = new Sphere(new Point(1, 2), 3);
|
||||
|
||||
DBObject dbo = new BasicDBObject();
|
||||
converter.write(object, dbo);
|
||||
|
||||
ClassWithGeoSphere result = converter.read(ClassWithGeoSphere.class, dbo);
|
||||
|
||||
assertThat(result, is(notNullValue()));
|
||||
assertThat(result.sphere, is(object.sphere));
|
||||
}
|
||||
|
||||
/**
|
||||
* @DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void shouldWriteEntityWithGeoShapeCorrectly() {
|
||||
|
||||
ClassWithGeoShape object = new ClassWithGeoShape();
|
||||
Sphere sphere = new Sphere(new Point(1, 2), 3);
|
||||
object.shape = sphere;
|
||||
|
||||
DBObject dbo = new BasicDBObject();
|
||||
converter.write(object, dbo);
|
||||
|
||||
assertThat(dbo, is(notNullValue()));
|
||||
assertThat(dbo.get("shape"), is(instanceOf(List.class)));
|
||||
assertThat(dbo.get("shape"), is((Object) Arrays.asList(org.springframework.data.mongodb.core.geo.Point
|
||||
.asList(sphere.getCenter()), sphere.getRadius().getNormalizedValue())));
|
||||
}
|
||||
|
||||
/**
|
||||
* @DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void shouldReadEntityWithGeoShapeCorrectly() {
|
||||
|
||||
ClassWithGeoShape object = new ClassWithGeoShape();
|
||||
Sphere sphere = new Sphere(new Point(1, 2), 3);
|
||||
object.shape = sphere;
|
||||
|
||||
DBObject dbo = new BasicDBObject();
|
||||
converter.write(object, dbo);
|
||||
|
||||
ClassWithGeoShape result = converter.read(ClassWithGeoShape.class, dbo);
|
||||
|
||||
assertThat(result, is(notNullValue()));
|
||||
assertThat(result.shape, is((Shape) sphere));
|
||||
}
|
||||
|
||||
static class GenericType<T> {
|
||||
T content;
|
||||
@@ -1740,4 +1966,34 @@ public class MappingMongoConverterUnitTests {
|
||||
return m_property;
|
||||
}
|
||||
}
|
||||
|
||||
class ClassWithGeoBox {
|
||||
|
||||
Box box;
|
||||
}
|
||||
|
||||
class ClassWithGeoCircle {
|
||||
|
||||
Circle circle;
|
||||
}
|
||||
|
||||
class ClassWithGeoLegacyCircle {
|
||||
|
||||
org.springframework.data.mongodb.core.geo.Circle circle;
|
||||
}
|
||||
|
||||
class ClassWithGeoSphere {
|
||||
|
||||
Sphere sphere;
|
||||
}
|
||||
|
||||
class ClassWithGeoPolygon {
|
||||
|
||||
Polygon polygon;
|
||||
}
|
||||
|
||||
class ClassWithGeoShape {
|
||||
|
||||
Shape shape;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
* Copyright (c) 2011-2014 by the original author(s).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,15 +19,25 @@ import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.geo.Box;
|
||||
import org.springframework.data.geo.Circle;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.data.geo.Polygon;
|
||||
import org.springframework.data.geo.Shape;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverters.BigDecimalToStringConverter;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigDecimalConverter;
|
||||
import org.springframework.data.mongodb.core.geo.Sphere;
|
||||
|
||||
import com.mongodb.BasicDBList;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MongoConverters}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class MongoConvertersUnitTests {
|
||||
|
||||
@@ -41,4 +51,74 @@ public class MongoConvertersUnitTests {
|
||||
BigDecimal reference = StringToBigDecimalConverter.INSTANCE.convert(value);
|
||||
assertThat(reference, is(bigDecimal));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void convertsBoxToDbObjectAndBackCorrectly() {
|
||||
|
||||
Box box = new Box(new Point(1, 2), new Point(3, 4));
|
||||
|
||||
BasicDBList dbo = GeoConverters.BoxToDbObjectConverter.INSTANCE.convert(box);
|
||||
Shape shape = GeoConverters.DbObjectToBoxConverter.INSTANCE.convert(dbo);
|
||||
|
||||
assertThat(shape, is((org.springframework.data.geo.Shape) box));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void convertsCircleToDbObjectAndBackCorrectly() {
|
||||
|
||||
Circle circle = new Circle(new Point(1, 2), 3);
|
||||
|
||||
BasicDBList dbo = GeoConverters.CircleToDbObjectConverter.INSTANCE.convert(circle);
|
||||
Shape shape = GeoConverters.DbObjectToCircleConverter.INSTANCE.convert(dbo);
|
||||
|
||||
assertThat(shape, is((org.springframework.data.geo.Shape) circle));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void convertsPolygonToDbObjectAndBackCorrectly() {
|
||||
|
||||
Polygon polygon = new Polygon(new Point(1, 2), new Point(2, 3), new Point(3, 4), new Point(5, 6));
|
||||
|
||||
BasicDBList dbo = GeoConverters.PolygonToDbObjectConverter.INSTANCE.convert(polygon);
|
||||
Shape shape = GeoConverters.DbObjectToPolygonConverter.INSTANCE.convert(dbo);
|
||||
|
||||
assertThat(shape, is((org.springframework.data.geo.Shape) polygon));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void convertsSphereToDbObjectAndBackCorrectly() {
|
||||
|
||||
Sphere sphere = new Sphere(new Point(1, 2), 3);
|
||||
|
||||
BasicDBList dbo = GeoConverters.SphereToDbObjectConverter.INSTANCE.convert(sphere);
|
||||
org.springframework.data.geo.Shape shape = GeoConverters.DbObjectToSphereConverter.INSTANCE.convert(dbo);
|
||||
|
||||
assertThat(shape, is((org.springframework.data.geo.Shape) sphere));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-858
|
||||
*/
|
||||
@Test
|
||||
public void convertsPointToListAndBackCorrectly() {
|
||||
|
||||
Point point = new Point(1, 2);
|
||||
|
||||
List<Double> list = GeoConverters.PointToListConverter.INSTANCE.convert(point);
|
||||
org.springframework.data.geo.Point converted = GeoConverters.ListToPointConverter.INSTANCE.convert(list);
|
||||
|
||||
assertThat(converted, is((org.springframework.data.geo.Point) point));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,36 +15,43 @@
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.geo;
|
||||
|
||||
import static org.springframework.data.mongodb.core.geo.Metrics.*;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.mongodb.core.geo.Metrics.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.geo.Metric;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link Distance}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class DistanceUnitTests {
|
||||
|
||||
@Test
|
||||
public void defaultsMetricToNeutralOne() {
|
||||
assertThat(new Distance(2.5).getMetric(), is((Metric) Metrics.NEUTRAL));
|
||||
assertThat(new Distance(2.5, null).getMetric(), is((Metric) Metrics.NEUTRAL));
|
||||
|
||||
assertThat(new Distance(2.5).getMetric(), is((Metric) org.springframework.data.geo.Metrics.NEUTRAL));
|
||||
assertThat(new Distance(2.5, null).getMetric(), is((Metric) org.springframework.data.geo.Metrics.NEUTRAL));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addsDistancesWithoutExplicitMetric() {
|
||||
|
||||
Distance left = new Distance(2.5, KILOMETERS);
|
||||
Distance right = new Distance(2.5, KILOMETERS);
|
||||
assertThat(left.add(right), is(new Distance(5.0, KILOMETERS)));
|
||||
|
||||
assertThat(left.add(right), is(new org.springframework.data.geo.Distance(5.0, KILOMETERS)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addsDistancesWithExplicitMetric() {
|
||||
|
||||
Distance left = new Distance(2.5, KILOMETERS);
|
||||
Distance right = new Distance(2.5, KILOMETERS);
|
||||
assertThat(left.add(right, MILES), is(new Distance(3.106856281073925, MILES)));
|
||||
|
||||
assertThat(left.add(right, MILES), is(new org.springframework.data.geo.Distance(3.106856281073925, MILES)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
* Copyright 2011-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -26,6 +26,7 @@ import org.junit.Test;
|
||||
* Unit tests for {@link GeoResults}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class GeoResultsUnitTests {
|
||||
|
||||
@@ -37,6 +38,6 @@ public class GeoResultsUnitTests {
|
||||
GeoResult<Object> second = new GeoResult<Object>(new Object(), new Distance(5));
|
||||
GeoResults<Object> geoResults = new GeoResults<Object>(Arrays.asList(first, second));
|
||||
|
||||
assertThat(geoResults.getAverageDistance(), is(new Distance(3.5)));
|
||||
assertThat(geoResults.getAverageDistance(), is(new org.springframework.data.geo.Distance(3.5)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2013 the original author or authors.
|
||||
* Copyright 2010-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -31,6 +31,12 @@ import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
import org.springframework.data.geo.Box;
|
||||
import org.springframework.data.geo.Circle;
|
||||
import org.springframework.data.geo.Metric;
|
||||
import org.springframework.data.geo.Metrics;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.data.geo.Polygon;
|
||||
import org.springframework.data.mongodb.config.AbstractIntegrationTests;
|
||||
import org.springframework.data.mongodb.core.CollectionCallback;
|
||||
import org.springframework.data.mongodb.core.IndexOperations;
|
||||
@@ -54,6 +60,7 @@ import com.mongodb.WriteConcern;
|
||||
*
|
||||
* @author Mark Pollack
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class GeoSpatialTests extends AbstractIntegrationTests {
|
||||
|
||||
@@ -96,8 +103,9 @@ public class GeoSpatialTests extends AbstractIntegrationTests {
|
||||
NearQuery geoNear = NearQuery.near(-73, 40, Metrics.KILOMETERS).num(10).maxDistance(150);
|
||||
|
||||
GeoResults<Venue> result = template.geoNear(geoNear, Venue.class);
|
||||
|
||||
assertThat(result.getContent().size(), is(not(0)));
|
||||
assertThat(result.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
assertThat((Metric) result.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -24,8 +24,7 @@ import org.springframework.data.annotation.PersistenceConstructor;
|
||||
*/
|
||||
public class PersonCustomIdName extends BasePerson {
|
||||
|
||||
@Id
|
||||
private String lastName;
|
||||
@Id private String lastName;
|
||||
|
||||
public PersonCustomIdName(Integer ssn, String firstName) {
|
||||
this.ssn = ssn;
|
||||
|
||||
@@ -23,8 +23,7 @@ import org.springframework.data.annotation.Id;
|
||||
@Document
|
||||
public class PersonMultiDimArrays extends BasePerson {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
@Id private String id;
|
||||
private String[][] grid;
|
||||
|
||||
public PersonMultiDimArrays(Integer ssn, String firstName, String lastName, String[][] grid) {
|
||||
|
||||
@@ -16,15 +16,13 @@
|
||||
|
||||
package org.springframework.data.mongodb.core.mapping;
|
||||
|
||||
import org.springframework.data.mongodb.core.mapping.DBRef;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
public class PersonWithDbRef extends BasePerson {
|
||||
|
||||
@DBRef
|
||||
private GeoLocation home;
|
||||
@DBRef private GeoLocation home;
|
||||
|
||||
public PersonWithDbRef(Integer ssn, String firstName, String lastName, GeoLocation home) {
|
||||
super(ssn, firstName, lastName);
|
||||
|
||||
@@ -5,8 +5,7 @@ package org.springframework.data.mongodb.core.mapping;
|
||||
*/
|
||||
public class PersonWithLongDBRef extends BasePerson {
|
||||
|
||||
@DBRef
|
||||
private PersonPojoLongId personPojoLongId;
|
||||
@DBRef private PersonPojoLongId personPojoLongId;
|
||||
|
||||
public PersonWithLongDBRef(Integer ssn, String firstName, String lastName, PersonPojoLongId personPojoLongId) {
|
||||
super(ssn, firstName, lastName);
|
||||
|
||||
@@ -21,11 +21,11 @@ import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.geo.Distance;
|
||||
import org.springframework.data.geo.Metric;
|
||||
import org.springframework.data.geo.Metrics;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.data.mongodb.core.DBObjectTestUtils;
|
||||
import org.springframework.data.mongodb.core.geo.Distance;
|
||||
import org.springframework.data.mongodb.core.geo.Metric;
|
||||
import org.springframework.data.mongodb.core.geo.Metrics;
|
||||
import org.springframework.data.mongodb.core.geo.Point;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link NearQuery}.
|
||||
@@ -48,8 +48,8 @@ public class NearQueryUnitTests {
|
||||
|
||||
NearQuery query = NearQuery.near(2.5, 2.5, Metrics.KILOMETERS).maxDistance(150);
|
||||
|
||||
assertThat(query.getMaxDistance(), is(ONE_FIFTY_KILOMETERS));
|
||||
assertThat(query.getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
assertThat((Distance) query.getMaxDistance(), is(ONE_FIFTY_KILOMETERS));
|
||||
assertThat((Metric) query.getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
assertThat(query.isSpherical(), is(true));
|
||||
}
|
||||
|
||||
@@ -59,27 +59,28 @@ public class NearQueryUnitTests {
|
||||
NearQuery query = NearQuery.near(2.5, 2.5, Metrics.KILOMETERS).maxDistance(150);
|
||||
|
||||
query.inMiles();
|
||||
assertThat(query.getMetric(), is((Metric) Metrics.MILES));
|
||||
|
||||
assertThat((Metric) query.getMetric(), is((Metric) Metrics.MILES));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configuresResultMetricCorrectly() {
|
||||
|
||||
NearQuery query = NearQuery.near(2.5, 2.1);
|
||||
assertThat(query.getMetric(), is((Metric) Metrics.NEUTRAL));
|
||||
assertThat((Metric) query.getMetric(), is((Metric) Metrics.NEUTRAL));
|
||||
|
||||
query = query.maxDistance(ONE_FIFTY_KILOMETERS);
|
||||
assertThat(query.getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
assertThat(query.getMaxDistance(), is(ONE_FIFTY_KILOMETERS));
|
||||
assertThat((Metric) query.getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
assertThat((Distance) query.getMaxDistance(), is(ONE_FIFTY_KILOMETERS));
|
||||
assertThat(query.isSpherical(), is(true));
|
||||
|
||||
query = query.in(Metrics.MILES);
|
||||
assertThat(query.getMetric(), is((Metric) Metrics.MILES));
|
||||
assertThat(query.getMaxDistance(), is(ONE_FIFTY_KILOMETERS));
|
||||
assertThat((Metric) query.getMetric(), is((Metric) Metrics.MILES));
|
||||
assertThat((Distance) query.getMaxDistance(), is(ONE_FIFTY_KILOMETERS));
|
||||
assertThat(query.isSpherical(), is(true));
|
||||
|
||||
query = query.maxDistance(new Distance(200, Metrics.KILOMETERS));
|
||||
assertThat(query.getMetric(), is((Metric) Metrics.MILES));
|
||||
assertThat((Metric) query.getMetric(), is((Metric) Metrics.MILES));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,14 +33,14 @@ import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Slice;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
import org.springframework.data.geo.Metric;
|
||||
import org.springframework.data.geo.Metrics;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.geo.Box;
|
||||
import org.springframework.data.mongodb.core.geo.Circle;
|
||||
import org.springframework.data.mongodb.core.geo.Distance;
|
||||
import org.springframework.data.mongodb.core.geo.GeoPage;
|
||||
import org.springframework.data.mongodb.core.geo.GeoResults;
|
||||
import org.springframework.data.mongodb.core.geo.Metric;
|
||||
import org.springframework.data.mongodb.core.geo.Metrics;
|
||||
import org.springframework.data.mongodb.core.geo.Point;
|
||||
import org.springframework.data.mongodb.core.geo.Polygon;
|
||||
import org.springframework.data.mongodb.core.query.BasicQuery;
|
||||
@@ -406,7 +406,7 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
assertThat(results.getContent().isEmpty(), is(false));
|
||||
|
||||
// DATAMONGO-607
|
||||
assertThat(results.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
assertThat((Metric) results.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -619,7 +619,7 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
assertThat(results.getNumberOfElements(), is(2));
|
||||
assertThat(results.isFirstPage(), is(false));
|
||||
assertThat(results.isLastPage(), is(false));
|
||||
assertThat(results.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
assertThat((Metric) results.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
assertThat(results.getAverageDistance().getNormalizedValue(), is(0.0));
|
||||
}
|
||||
|
||||
@@ -643,7 +643,7 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
assertThat(results.getNumberOfElements(), is(1));
|
||||
assertThat(results.isFirstPage(), is(false));
|
||||
assertThat(results.isLastPage(), is(true));
|
||||
assertThat(results.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
assertThat((Metric) results.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -663,7 +663,7 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
assertThat(results.getNumberOfElements(), is(1));
|
||||
assertThat(results.isFirstPage(), is(true));
|
||||
assertThat(results.isLastPage(), is(true));
|
||||
assertThat(results.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
assertThat((Metric) results.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -682,7 +682,7 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
assertThat(results.getNumberOfElements(), is(0));
|
||||
assertThat(results.isFirstPage(), is(false));
|
||||
assertThat(results.isLastPage(), is(true));
|
||||
assertThat(results.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
assertThat((Metric) results.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,8 +22,7 @@ import javax.inject.Inject;
|
||||
*/
|
||||
class RepositoryClient {
|
||||
|
||||
@Inject
|
||||
CdiPersonRepository repository;
|
||||
@Inject CdiPersonRepository repository;
|
||||
|
||||
/**
|
||||
* @return the repository
|
||||
|
||||
@@ -22,8 +22,8 @@ import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.geo.Metrics;
|
||||
import org.springframework.data.mongodb.core.geo.Distance;
|
||||
import org.springframework.data.mongodb.core.geo.Metrics;
|
||||
import org.springframework.data.mongodb.core.geo.Point;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
||||
import org.springframework.data.mongodb.repository.Person;
|
||||
|
||||
@@ -36,11 +36,11 @@ import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.data.geo.Metrics;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.mongodb.core.Person;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverter;
|
||||
import org.springframework.data.mongodb.core.geo.Distance;
|
||||
import org.springframework.data.mongodb.core.geo.Metrics;
|
||||
import org.springframework.data.mongodb.core.geo.Point;
|
||||
import org.springframework.data.mongodb.core.mapping.DBRef;
|
||||
import org.springframework.data.mongodb.core.mapping.Field;
|
||||
|
||||
Reference in New Issue
Block a user