DATACMNS-437 - Introduce reusable value objects for geo concepts.

Moved and retro-fitted the basic geo structures from SD MongoDB into SD Commons. This will serve as the foundation for more reusable geo spatial functionality in other SD modules. 

Adjusted equals methods to support subtypes in other SD modules. Added convenience constructor to Polygon. Renamed spanning points of Box to be first and second because some stores could have a different meaning of the spanning points. Changed radius field of circle to be a Distance (value + metric). Added additional mitigation constructor to Circle.

Original pull request: #68.
This commit is contained in:
Thomas Darimont
2014-02-26 01:17:26 +01:00
committed by Oliver Gierke
parent c422f798a3
commit e4dfda29a3
20 changed files with 1495 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
/*
* 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.geo;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Unit tests for {@link Box}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class BoxUnitTests {
Box first = new Box(new Point(1d, 1d), new Point(2d, 2d));
Box second = new Box(new Point(1d, 1d), new Point(2d, 2d));
Box third = new Box(new Point(3d, 3d), new Point(1d, 1d));
/**
* @see DATACMNS-437
*/
@Test
public void equalsWorksCorrectly() {
assertThat(first.equals(second), is(true));
assertThat(second.equals(first), is(true));
assertThat(first.equals(third), is(false));
}
/**
* @see DATACMNS-437
*/
@Test
public void hashCodeWorksCorrectly() {
assertThat(first.hashCode(), is(second.hashCode()));
assertThat(first.hashCode(), is(not(third.hashCode())));
}
}

View File

@@ -0,0 +1,64 @@
/*
* 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.geo;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Unit tests for {@link Circle}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class CircleUnitTests {
/**
* @see DATACMNS-437
*/
@Test(expected = IllegalArgumentException.class)
public void rejectsNullOrigin() {
new Circle(null, new Distance(0));
}
/**
* @see DATACMNS-437
*/
@Test(expected = IllegalArgumentException.class)
public void rejectsNegativeRadius() {
new Circle(1, 1, -1);
}
/**
* @see DATACMNS-437
*/
@Test
public void considersTwoCirclesEqualCorrectly() {
Circle left = new Circle(1, 1, 1);
Circle right = new Circle(1, 1, 1);
assertThat(left, is(right));
assertThat(right, is(left));
right = new Circle(new Point(1, 1), new Distance(1));
assertThat(left, is(right));
assertThat(right, is(left));
}
}

View File

@@ -0,0 +1,65 @@
/*
* 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.geo;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.geo.Metrics.*;
import org.junit.Test;
/**
* Unit tests for {@link Distance}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class DistanceUnitTests {
/**
* @see DATACMNS-437
*/
@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));
}
/**
* @see DATACMNS-437
*/
@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)));
}
/**
* @see DATACMNS-437
*/
@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)));
}
}

View File

@@ -0,0 +1,67 @@
/*
* 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.geo;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Unit tests for {@link GeoResult}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class GeoResultUnitTests {
GeoResult<String> first = new GeoResult<String>("Foo", new Distance(2.5));
GeoResult<String> second = new GeoResult<String>("Foo", new Distance(2.5));
GeoResult<String> third = new GeoResult<String>("Bar", new Distance(2.5));
GeoResult<String> fourth = new GeoResult<String>("Foo", new Distance(5.2));
/**
* @see DATACMNS-437
*/
@Test
public void considersSameInstanceEqual() {
assertThat(first.equals(first), is(true));
}
/**
* @see DATACMNS-437
*/
@Test
public void considersSameValuesAsEqual() {
assertThat(first.equals(second), is(true));
assertThat(second.equals(first), is(true));
assertThat(first.equals(third), is(false));
assertThat(third.equals(first), is(false));
assertThat(first.equals(fourth), is(false));
assertThat(fourth.equals(first), is(false));
}
/**
* @see DATACMNS-437
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test(expected = IllegalArgumentException.class)
public void rejectsNullContent() {
new GeoResult(null, new Distance(2.5));
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.geo;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Test;
/**
* Unit tests for {@link GeoResults}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class GeoResultsUnitTests {
/**
* @see DATACMNS-437
*/
@Test
@SuppressWarnings("unchecked")
public void calculatesAverageForGivenGeoResults() {
GeoResult<Object> first = new GeoResult<Object>(new Object(), new Distance(2));
GeoResult<Object> second = new GeoResult<Object>(new Object(), new Distance(5));
GeoResults<Object> geoResults = new GeoResults<Object>(Arrays.asList(first, second));
assertThat(geoResults.getAverageDistance(), is(new Distance(3.5)));
}
}

View File

@@ -0,0 +1,57 @@
/*
* 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.geo;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Unit tests for {@link Point}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class PointUnitTests {
/**
* @see DATACMNS-437
*/
@Test(expected = IllegalArgumentException.class)
public void rejectsNullforCopyConstructor() {
new Point(null);
}
/**
* @see DATACMNS-437
*/
@Test
public void equalsIsImplementedCorrectly() {
assertThat(new Point(1.5, 1.5), is(equalTo(new Point(1.5, 1.5))));
assertThat(new Point(1.5, 1.5), is(not(equalTo(new Point(2.0, 2.0)))));
assertThat(new Point(2.0, 2.0), is(not(equalTo(new Point(1.5, 1.5)))));
}
/**
* @see DATACMNS-437
*/
@Test
public void invokingToStringWorksCorrectly() {
new Point(1.5, 1.5).toString();
}
}

View File

@@ -0,0 +1,66 @@
/*
* 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.geo;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Unit tests for {@link Polygon}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class PolygonUnitTests {
Point first = new Point(1, 1);
Point second = new Point(2, 2);
Point third = new Point(3, 3);
/**
* @see DATACMNS-437
*/
@Test(expected = IllegalArgumentException.class)
public void rejectsNullPoints() {
new Polygon(null, null, null);
}
/**
* @see DATACMNS-437
*/
@Test
public void createsSimplePolygon() {
Polygon polygon = new Polygon(third, second, first);
assertThat(polygon, is(notNullValue()));
}
/**
* @see DATACMNS-437
*/
@Test
public void isEqualForSamePoints() {
Polygon left = new Polygon(third, second, first);
Polygon right = new Polygon(third, second, first);
assertThat(left, is(right));
assertThat(right, is(left));
}
}