DATACMNS-482 - Added serialization support for Geospatial Types.

Added Serializable marker interface to Shape to make all geospatial types serializable. Changed equals implementation in GeoResults to use equals(…) instead of reference equality.

Original pull request: #77.
This commit is contained in:
Thomas Darimont
2014-03-31 10:10:55 +02:00
committed by Oliver Gierke
parent 9bd8e4db81
commit a30068c02d
17 changed files with 126 additions and 7 deletions

View File

@@ -27,6 +27,8 @@ import org.springframework.util.Assert;
*/
public class Box implements Shape {
private static final long serialVersionUID = 8198095179084040711L;
private final Point first;
private final Point second;

View File

@@ -28,6 +28,8 @@ import org.springframework.util.Assert;
*/
public class Circle implements Shape {
private static final long serialVersionUID = 5215611530535947924L;
private final Point center;
private final Distance radius;

View File

@@ -24,6 +24,8 @@ package org.springframework.data.geo;
*/
public class CustomMetric implements Metric {
private static final long serialVersionUID = -2972074177454114228L;
private final double multiplier;
/**

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.data.geo;
import java.io.Serializable;
import org.springframework.util.Assert;
/**
@@ -24,7 +26,9 @@ import org.springframework.util.Assert;
* @author Thomas Darimont
* @since 1.8
*/
public class Distance {
public class Distance implements Serializable {
private static final long serialVersionUID = 2460886201934027744L;
private final double value;
private final Metric metric;

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.data.geo;
import java.io.Serializable;
import org.springframework.util.Assert;
/**
@@ -24,7 +26,9 @@ import org.springframework.util.Assert;
* @author Thomas Darimont
* @since 1.8
*/
public class GeoResult<T> {
public class GeoResult<T> implements Serializable {
private static final long serialVersionUID = 1637452570977581370L;
private final T content;
private final Distance distance;

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.geo;
import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@@ -30,7 +31,9 @@ import org.springframework.util.StringUtils;
* @author Thomas Darimont
* @since 1.8
*/
public class GeoResults<T> implements Iterable<GeoResult<T>> {
public class GeoResults<T> implements Iterable<GeoResult<T>>, Serializable {
private static final long serialVersionUID = 8347363491300219485L;
private final List<? extends GeoResult<T>> results;
private final Distance averageDistance;
@@ -115,7 +118,7 @@ public class GeoResults<T> implements Iterable<GeoResult<T>> {
GeoResults<?> that = (GeoResults<?>) obj;
return this.results.equals(that.results) && this.averageDistance == that.averageDistance;
return this.results.equals(that.results) && this.averageDistance.equals(that.averageDistance);
}
/*

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.data.geo;
import java.io.Serializable;
/**
* Interface for {@link Metric}s that can be applied to a base scale.
*
@@ -22,7 +24,7 @@ package org.springframework.data.geo;
* @author Thomas Darimont
* @since 1.8
*/
public interface Metric {
public interface Metric extends Serializable {
/**
* Returns the multiplier to calculate metrics values from a base scale.

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.geo;
import java.io.Serializable;
import java.util.Locale;
import org.springframework.data.annotation.PersistenceConstructor;
@@ -28,7 +29,9 @@ import org.springframework.util.Assert;
* @author Thomas Darimont
* @since 1.8
*/
public class Point {
public class Point implements Serializable {
private static final long serialVersionUID = 3583151228933783558L;
private final double x;
private final double y;

View File

@@ -34,6 +34,8 @@ import org.springframework.util.StringUtils;
*/
public class Polygon implements Iterable<Point>, Shape {
private static final long serialVersionUID = -2705040068154648988L;
private final List<Point> points;
/**

View File

@@ -15,10 +15,12 @@
*/
package org.springframework.data.geo;
import java.io.Serializable;
/**
* Common interface for all shapes. Allows building external representations of them.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public interface Shape {}
public interface Shape extends Serializable {}

View File

@@ -19,6 +19,7 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.util.SerializationUtils;
/**
* Unit tests for {@link Box}.
@@ -61,4 +62,14 @@ public class BoxUnitTests {
assertThat(first.toString(), is("Box [Point [x=1.000000, y=1.000000], Point [x=2.000000, y=2.000000]]"));
}
/**
* @see DATACMNS-482
*/
@Test
public void testSerialization() {
Box serialized = (Box) SerializationUtils.deserialize(SerializationUtils.serialize(first));
assertThat(serialized, is(first));
}
}

View File

@@ -19,6 +19,7 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.util.SerializationUtils;
/**
* Unit tests for {@link Circle}.
@@ -70,4 +71,16 @@ public class CircleUnitTests {
assertThat(new Circle(1, 1, 1).toString(), is("Circle: [center=Point [x=1.000000, y=1.000000], radius=1.0]"));
}
/**
* @see DATACMNS-482
*/
@Test
public void testSerialization() {
Circle circle = new Circle(1, 1, 1);
Circle serialized = (Circle) SerializationUtils.deserialize(SerializationUtils.serialize(circle));
assertThat(serialized, is(circle));
}
}

View File

@@ -21,6 +21,7 @@ import static org.junit.Assert.*;
import static org.springframework.data.geo.Metrics.*;
import org.junit.Test;
import org.springframework.util.SerializationUtils;
/**
* Unit tests for {@link Distance}.
@@ -124,4 +125,16 @@ public class DistanceUnitTests {
assertThat(new Distance(6.21371256214785, MILES).in(KILOMETERS).toString(),
is(new Distance(10, KILOMETERS).toString()));
}
/**
* @see DATACMNS-482
*/
@Test
public void testSerialization() {
Distance dist = new Distance(10, KILOMETERS);
Distance serialized = (Distance) SerializationUtils.deserialize(SerializationUtils.serialize(dist));
assertThat(serialized, is(dist));
}
}

View File

@@ -19,6 +19,7 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.util.SerializationUtils;
/**
* Unit tests for {@link GeoResult}.
@@ -64,4 +65,17 @@ public class GeoResultUnitTests {
public void rejectsNullContent() {
new GeoResult(null, new Distance(2.5));
}
/**
* @see DATACMNS-482
*/
@Test
public void testSerialization() {
GeoResult<String> result = new GeoResult<String>("test", new Distance(2));
@SuppressWarnings("unchecked")
GeoResult<String> serialized = (GeoResult<String>) SerializationUtils.deserialize(SerializationUtils.serialize(result));
assertThat(serialized, is(result));
}
}

View File

@@ -21,6 +21,7 @@ import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.util.SerializationUtils;
/**
* Unit tests for {@link GeoResults}.
@@ -43,4 +44,20 @@ public class GeoResultsUnitTests {
assertThat(geoResults.getAverageDistance(), is(new Distance(3.5)));
}
/**
* @see DATACMNS-482
*/
@Test
public void testSerialization() {
GeoResult<String> result = new GeoResult<String>("test", new Distance(2));
@SuppressWarnings("unchecked")
GeoResults<String> geoResults = new GeoResults<String>(Arrays.asList(result));
@SuppressWarnings("unchecked")
GeoResults<String> serialized = (GeoResults<String>) SerializationUtils.deserialize(SerializationUtils
.serialize(geoResults));
assertThat(serialized, is(geoResults));
}
}

View File

@@ -19,6 +19,7 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.util.SerializationUtils;
/**
* Unit tests for {@link Point}.
@@ -55,4 +56,15 @@ public class PointUnitTests {
assertThat(new Point(1.5, 1.5).toString(), is("Point [x=1.500000, y=1.500000]"));
}
/**
* @see DATACMNS-482
*/
@Test
public void testSerialization() {
Point point = new Point(1.5, 1.5);
Point serialized = (Point) SerializationUtils.deserialize(SerializationUtils.serialize(point));
assertThat(serialized, is(point));
}
}

View File

@@ -19,6 +19,7 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.util.SerializationUtils;
/**
* Unit tests for {@link Polygon}.
@@ -73,4 +74,16 @@ public class PolygonUnitTests {
assertThat(new Polygon(third, second, first).toString(),
is("Polygon: [Point [x=3.000000, y=3.000000],Point [x=2.000000, y=2.000000],Point [x=1.000000, y=1.000000]]"));
}
/**
* @see DATACMNS-482
*/
@Test
public void testSerialization() {
Polygon polygon = new Polygon(third, second, first);
Polygon serialized = (Polygon) SerializationUtils.deserialize(SerializationUtils.serialize(polygon));
assertThat(serialized, is(polygon));
}
}