diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java index 3f003a1f8..3f1a85462 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java @@ -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 { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index e771f85cc..9e983b08c 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -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}. * diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/CustomConversions.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/CustomConversions.java index 93304cd8b..b78bb5dba 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/CustomConversions.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/CustomConversions.java @@ -96,6 +96,7 @@ public class CustomConversions { List toRegister = new ArrayList(); + // 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); diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoCommandUtils.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoCommandUtils.java new file mode 100644 index 000000000..db7d5b262 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoCommandUtils.java @@ -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); + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java new file mode 100644 index 000000000..e6a8fcbcc --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java @@ -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, Point> { + + INSTANCE; + + @SuppressWarnings("deprecation") + public Point convert(List 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> { + + INSTANCE; + + @Override + public List 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 { + + 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 { + + 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 { + + 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 { + + 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 { + + 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 { + + 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 { + + 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 { + + 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 { + + INSTANCE; + + @Override + public BasicDBList convert(Polygon source) { + + if (source == null) { + return null; + } + + List points = source.getPoints(); + List> pointTuples = new ArrayList>(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 { + + INSTANCE; + + @SuppressWarnings("deprecation") + @Override + public Polygon convert(BasicDBList source) { + + if (source == null) { + return null; + } + + List points = new ArrayList(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) 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 toList(Point point) { + return PointToListConverter.INSTANCE.convert(point); + } + + /** + * Returns the geo converters to be registered. + * + * @return + */ + @SuppressWarnings("unchecked") + public static Collection 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 // + ); + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverters.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverters.java index b432b389c..126a8f4c7 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverters.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverters.java @@ -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. diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Box.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Box.java index aa2355f58..d5dd9a6d4 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Box.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Box.java @@ -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 asList() { + List> list = new ArrayList>(); - 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; } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Circle.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Circle.java index 3768d5c19..a23b62d72 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Circle.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Circle.java @@ -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. + *

+ * 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 asList() { + List result = new ArrayList(); - 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; } /* diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/CustomMetric.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/CustomMetric.java index b68b3a99e..bb5fd4396 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/CustomMetric.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/CustomMetric.java @@ -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); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Distance.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Distance.java index f278c0ce8..571ffb4e7 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Distance.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Distance.java @@ -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); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoPage.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoPage.java index 7dbc501be..33eceda71 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoPage.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoPage.java @@ -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 extends PageImpl> { +@Deprecated +public class GeoPage extends org.springframework.data.geo.GeoPage { 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 extends PageImpl> { * @param content must not be {@literal null}. */ public GeoPage(GeoResults results) { - super(results.getContent()); - this.averageDistance = results.getAverageDistance(); + super(results); } /** @@ -48,16 +49,6 @@ public class GeoPage extends PageImpl> { * @param total */ public GeoPage(GeoResults 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); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoResult.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoResult.java index 6ba6fb82d..04601f4a7 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoResult.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoResult.java @@ -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 { - - private final T content; - private final Distance distance; +@Deprecated +public class GeoResult extends org.springframework.data.geo.GeoResult { /** * Creates a new {@link GeoResult} for the given content and distance. @@ -34,69 +33,6 @@ public class GeoResult { * @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()); - } -} \ No newline at end of file +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoResults.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoResults.java index a08e547c0..6197a8a1a 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoResults.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoResults.java @@ -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 implements Iterable> { - - private final List> results; - private final Distance averageDistance; +@Deprecated +public class GeoResults extends org.springframework.data.geo.GeoResults { /** * Creates a new {@link GeoResults} instance manually calculating the average distance from the distance values of the @@ -39,12 +39,12 @@ public class GeoResults implements Iterable> { * * @param results must not be {@literal null}. */ - public GeoResults(List> results) { - this(results, (Metric) null); + public GeoResults(List> results) { + super(results); } - public GeoResults(List> results, Metric metric) { - this(results, calculateAverageDistance(results, metric)); + public GeoResults(List> results, Metric metric) { + super(results, metric); } /** @@ -54,92 +54,7 @@ public class GeoResults implements Iterable> { * @param averageDistance */ @PersistenceConstructor - public GeoResults(List> 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> iterator() { - return results.iterator(); - } - - /** - * Returns the actual - * - * @return - */ - public List> 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> 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> results, Distance averageDistance) { + super(results, averageDistance); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Metric.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Metric.java index 300c19cc0..4db3d1785 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Metric.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Metric.java @@ -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(); -} \ No newline at end of file +@Deprecated +public interface Metric extends org.springframework.data.geo.Metric {} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Metrics.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Metrics.java index 882bb2656..2b5939198 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Metrics.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Metrics.java @@ -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; } -} \ No newline at end of file +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Point.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Point.java index 16f3c8793..55141dafd 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Point.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Point.java @@ -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 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 asList(org.springframework.data.geo.Point point) { + return Arrays.asList(point.getX(), point.getY()); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Polygon.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Polygon.java index f1fb0dc41..545b727a3 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Polygon.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Polygon.java @@ -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 { +@Deprecated +public class Polygon extends org.springframework.data.geo.Polygon implements Shape { - private final List 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 { * @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(3 + others.length); - this.points.addAll(Arrays.asList(x, y, z)); - this.points.addAll(Arrays.asList(others)); + public

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> asList() { - - List> result = new ArrayList>(); - - for (Point point : points) { - result.add(point.asList()); - } - - return result; + public

Polygon(List

points) { + super(points); } /* @@ -71,43 +60,33 @@ public class Polygon implements Shape, Iterable { * @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 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 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 asList(org.springframework.data.geo.Polygon polygon) { + + List points = polygon.getPoints(); + List> tuples = new ArrayList>(points.size()); + + for (Point point : points) { + tuples.add(Arrays.asList(point.getX(), point.getY())); + } + + return tuples; } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Shape.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Shape.java index 4c412455a..3a63935f7 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Shape.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Shape.java @@ -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 diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Sphere.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Sphere.java new file mode 100644 index 000000000..686034c46 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/Sphere.java @@ -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 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; + } +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/package-info.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/package-info.java index 792e68068..52505fe2e 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/package-info.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/package-info.java @@ -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. */ diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java index 22241b553..ec2499f78 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java @@ -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; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/NearQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/NearQuery.java index 093c3fa61..3a96914bc 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/NearQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/NearQuery.java @@ -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; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonWithVersionPropertyOfTypeLong.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonWithVersionPropertyOfTypeLong.java index a724654ff..f1653ca12 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonWithVersionPropertyOfTypeLong.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonWithVersionPropertyOfTypeLong.java @@ -23,8 +23,7 @@ public class PersonWithVersionPropertyOfTypeLong { String firstName; int age; - @Version - Long version; + @Version Long version; @Override public String toString() { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoConvertersUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoConvertersUnitTests.java new file mode 100644 index 000000000..02e7e8ed6 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoConvertersUnitTests.java @@ -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 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)); + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java index 44238e1d6..c8df89498 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java @@ -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 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; + } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoConvertersUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoConvertersUnitTests.java index 7bd5c6487..d4d13fdcf 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoConvertersUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoConvertersUnitTests.java @@ -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 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)); + } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/DistanceUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/DistanceUnitTests.java index ac09bd5a4..590e7a89e 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/DistanceUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/DistanceUnitTests.java @@ -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))); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoResultsUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoResultsUnitTests.java index 51f522ffd..8e5c6d8f3 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoResultsUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoResultsUnitTests.java @@ -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 second = new GeoResult(new Object(), new Distance(5)); GeoResults geoResults = new GeoResults(Arrays.asList(first, second)); - assertThat(geoResults.getAverageDistance(), is(new Distance(3.5))); + assertThat(geoResults.getAverageDistance(), is(new org.springframework.data.geo.Distance(3.5))); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatialTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatialTests.java index 1f929a9a3..0afcb903c 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatialTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoSpatialTests.java @@ -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 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 diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonCustomIdName.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonCustomIdName.java index 18f5ff2f2..2fe08566f 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonCustomIdName.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonCustomIdName.java @@ -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; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonMultiDimArrays.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonMultiDimArrays.java index 6981a43ab..d556e0f2e 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonMultiDimArrays.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonMultiDimArrays.java @@ -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) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonWithDbRef.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonWithDbRef.java index 39f9b312b..06890e1bf 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonWithDbRef.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonWithDbRef.java @@ -16,15 +16,13 @@ package org.springframework.data.mongodb.core.mapping; -import org.springframework.data.mongodb.core.mapping.DBRef; /** * @author Jon Brisbin */ 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); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonWithLongDBRef.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonWithLongDBRef.java index db14a3c1c..6af82cfeb 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonWithLongDBRef.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/PersonWithLongDBRef.java @@ -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); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/NearQueryUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/NearQueryUnitTests.java index 417dbf941..86b2de443 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/NearQueryUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/NearQueryUnitTests.java @@ -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)); } /** diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java index cceaeae0b..a28598bad 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java @@ -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)); } /** diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/cdi/RepositoryClient.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/cdi/RepositoryClient.java index f8fd1755c..643418bb1 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/cdi/RepositoryClient.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/cdi/RepositoryClient.java @@ -22,8 +22,7 @@ import javax.inject.Inject; */ class RepositoryClient { - @Inject - CdiPersonRepository repository; + @Inject CdiPersonRepository repository; /** * @return the repository diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessorUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessorUnitTests.java index 08502d127..e645c0ee8 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessorUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoParametersParameterAccessorUnitTests.java @@ -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; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java index b87624570..de192bd69 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java @@ -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;