Polygon[] array = (Polygon[]) geometries.toArray(new Polygon[geometries.size()]);
MultiPolygon mp = gf.createMultiPolygon(array);
// a collection of valid polygon does not necessarily make up a valid multipolygon
if(array.length > 1 && !mp.isValid()) {
Geometry g = mp.buffer(0);
if(g instanceof Polygon) {
return gf.createMultiPolygon(new Polygon[] {(Polygon) g});
} else {
return (GeometryCollection) g;
}