diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoBaseCompositeAreaShape.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoBaseCompositeAreaShape.java index 164d45837a6..986e42ccedb 100644 --- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoBaseCompositeAreaShape.java +++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoBaseCompositeAreaShape.java @@ -17,6 +17,9 @@ package org.apache.lucene.spatial3d.geom; +import java.io.InputStream; +import java.io.IOException; + /** * Base class to create a composite of GeoAreaShapes * @@ -39,6 +42,16 @@ abstract class GeoBaseCompositeAreaShape extends GeoBase super(planetModel); } + /** + * Constructor for deserialization. + * @param planetModel is the planet model. + * @param inputStream is the input stream. + * @param clazz is the class of the generic. + */ + public GeoBaseCompositeAreaShape(final PlanetModel planetModel, final InputStream inputStream, final Class clazz) throws IOException { + super(planetModel, inputStream, clazz); + } + @Override public boolean intersects(GeoShape geoShape){ for(GeoAreaShape geoAreaShape : shapes){ diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoBaseCompositeMembershipShape.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoBaseCompositeMembershipShape.java index 0f19a7c870c..d4eab4e37ba 100644 --- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoBaseCompositeMembershipShape.java +++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoBaseCompositeMembershipShape.java @@ -17,6 +17,9 @@ package org.apache.lucene.spatial3d.geom; +import java.io.InputStream; +import java.io.IOException; + /** * Base class to create a composite of GeoMembershipShapes * @@ -33,6 +36,16 @@ abstract class GeoBaseCompositeMembershipShape super(planetModel); } + /** + * Constructor for deserialization. + * @param planetModel is the planet model. + * @param inputStream is the input stream. + * @param clazz is the class of the generic. + */ + GeoBaseCompositeMembershipShape(final PlanetModel planetModel, final InputStream inputStream, final Class clazz) throws IOException { + super(planetModel, inputStream, clazz); + } + @Override public double computeOutsideDistance(final DistanceStyle distanceStyle, final GeoPoint point) { return computeOutsideDistance(distanceStyle, point.x, point.y, point.z); diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoBaseCompositeShape.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoBaseCompositeShape.java index 32f6e9ad1c2..734eff6d18b 100644 --- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoBaseCompositeShape.java +++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoBaseCompositeShape.java @@ -20,6 +20,9 @@ package org.apache.lucene.spatial3d.geom; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.IOException; /** * Base class to create a composite of GeoShapes. @@ -71,6 +74,28 @@ public abstract class GeoBaseCompositeShape extends BasePlan return shapes.get(index); } + /** + * Constructor for deserialization. + * @param planetModel is the planet model. + * @param inputStream is the input stream. + * @param clazz is the class of the generic. + */ + public GeoBaseCompositeShape(final PlanetModel planetModel, final InputStream inputStream, final Class clazz) throws IOException { + this(planetModel); + final SerializableObject[] array = SerializableObject.readHeterogeneousArray(planetModel, inputStream); + for (final SerializableObject member : array) { + if (!(clazz.isAssignableFrom(member.getClass()))) { + throw new IOException("Can't deserialize composite because member has wrong type: "+member.getClass().getName()); + } + addShape(clazz.cast(member)); + } + } + + @Override + public void write(final OutputStream outputStream) throws IOException { + SerializableObject.writeHeterogeneousArray(outputStream, shapes); + } + @Override public boolean isWithin(final Vector point) { return isWithin(point.x, point.y, point.z); diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java index 6bd66d41584..107815dd38e 100644 --- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java +++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java @@ -19,6 +19,9 @@ package org.apache.lucene.spatial3d.geom; import java.util.Arrays; import java.util.List; import java.util.ArrayList; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.IOException; /** * GeoComplexPolygon objects are structures designed to handle very large numbers of edges. @@ -37,6 +40,7 @@ class GeoComplexPolygon extends GeoBasePolygon { private final Tree yTree; private final Tree zTree; + private final List> pointsList; private final boolean testPointInSet; private final GeoPoint testPoint; @@ -66,6 +70,7 @@ class GeoComplexPolygon extends GeoBasePolygon { */ public GeoComplexPolygon(final PlanetModel planetModel, final List> pointsList, final GeoPoint testPoint, final boolean testPointInSet) { super(planetModel); + this.pointsList = pointsList; // For serialization this.testPointInSet = testPointInSet; this.testPoint = testPoint; @@ -115,6 +120,41 @@ class GeoComplexPolygon extends GeoBasePolygon { zTree = new ZTree(allEdges); } + /** + * Constructor for deserialization. + * @param planetModel is the planet model. + * @param inputStream is the input stream. + */ + public GeoComplexPolygon(final PlanetModel planetModel, final InputStream inputStream) throws IOException { + this(planetModel, + readPointsList(planetModel, inputStream), + new GeoPoint(planetModel, inputStream), + SerializableObject.readBoolean(inputStream)); + } + + private static List> readPointsList(final PlanetModel planetModel, final InputStream inputStream) throws IOException { + final int count = SerializableObject.readInt(inputStream); + final List> array = new ArrayList<>(count); + for (int i = 0; i < count; i++) { + array.add(java.util.Arrays.asList(SerializableObject.readPointArray(planetModel, inputStream))); + } + return array; + } + + @Override + public void write(final OutputStream outputStream) throws IOException { + writePointsList(outputStream, pointsList); + testPoint.write(outputStream); + SerializableObject.writeBoolean(outputStream, testPointInSet); + } + + private static void writePointsList(final OutputStream outputStream, final List> pointsList) throws IOException { + SerializableObject.writeInt(outputStream, pointsList.size()); + for (final List points : pointsList) { + SerializableObject.writePointArray(outputStream, points); + } + } + @Override public boolean isWithin(final double x, final double y, final double z) { // If we're right on top of the point, we know the answer. diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoCompositeAreaShape.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoCompositeAreaShape.java index 085dbb3bf84..118144d5c4d 100644 --- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoCompositeAreaShape.java +++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoCompositeAreaShape.java @@ -17,6 +17,9 @@ package org.apache.lucene.spatial3d.geom; +import java.io.InputStream; +import java.io.IOException; + /** * GeoCompositeAreaShape is a set of GeoAreaShape's, treated as a unit. * @@ -31,6 +34,15 @@ public class GeoCompositeAreaShape extends GeoBaseCompositeAreaShape i super(planetModel); } + /** + * Constructor for deserialization. + * @param planetModel is the planet model. + * @param inputStream is the input stream. + */ + public GeoCompositePolygon(final PlanetModel planetModel, final InputStream inputStream) throws IOException { + super(planetModel, inputStream, GeoPolygon.class); + } + @Override public boolean equals(Object o) { if (!(o instanceof GeoCompositePolygon)) diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SerializableObject.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SerializableObject.java index 9479cf2170b..da23cf2775a 100644 --- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SerializableObject.java +++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SerializableObject.java @@ -376,4 +376,24 @@ public interface SerializableObject { return l1 + l2 + l3 + l4; } + /** Write a boolean to a stream. + * @param outputStream is the output stream. + * @param value is the value to write. + */ + static void writeBoolean(final OutputStream outputStream, final boolean value) throws IOException { + outputStream.write(value?0:1); + } + + /** Read a boolean from a stream. + * @param inputStream is the input stream. + * @return the boolean value. + */ + static boolean readBoolean(final InputStream inputStream) throws IOException { + final int valueRead = inputStream.read(); + if (valueRead == -1) { + throw new IOException("Unexpected end of input stream"); + } + return (valueRead == 0)?false:true; + } + }