class JTS {

    public static List<Polygon> makeValid(Polygon polygon, boolean removeHoles) {
        // add all segments into the polygonizer
        final Polygonizer p = new Polygonizer();
        polygon.apply(new CoordinateSequenceFilter() {

            public boolean isGeometryChanged() {
                return false;
            }

            public boolean isDone() {
                return false;
            }

            public void filter(CoordinateSequence seq, int i) {
                if (i == 0) {
                    return;
                }
                p.add(new GeometryFactory().createLineString(new Coordinate[] {
                        seq.getCoordinate(i - 1), seq.getCoordinate(i) }));
            }
        });

        List<Polygon> result = new ArrayList<Polygon>(p.getPolygons());

        // if necessary throw away the holes and return just the shells
        if (removeHoles) {
            for (int i = 0; i < result.size(); i++) {
                Polygon item = result.get(i);
                if (item.getNumInteriorRing() > 0) {
                    GeometryFactory factory = item.getFactory();
                    Polygon noHoles = factory.createPolygon((LinearRing) item.getExteriorRing(),
                            null);
                    result.set(i, noHoles);
                }
            }
        }

        return result;
    }

    private  JTS();

}