code: private Geometry makePolygonValid(Polygon polygon) {
          //This first step analyze linear components and create degenerate geometries
          //of dimension 0 or 1 if they do not form valid LinearRings
          //If degenerate geometries are found, it may produce a GeometryCollection with
          //heterogeneous dimension
          Geometry geom = makePolygonComponentsValid(polygon);
          List<Geometry> list = new ArrayList<>();
          for (int i = 0; i < geom.getNumGeometries(); i++) {
              Geometry component = geom.getGeometryN(i);
              if (component instanceof Polygon) {
                  Geometry nodedPolygon = nodePolygon((Polygon) component);
                  for (int j = 0; j < nodedPolygon.getNumGeometries(); j++) {
                      list.add(nodedPolygon.getGeometryN(j));
                  }
              } else {
                  list.add(component);
              }
          }
          return polygon.getFactory().buildGeometry(list);
      }
docs: Making a Polygon valid may creates
  <ul>
  <li>an Empty Polygon if input has no valid coordinate</li>
  <li>a Point if input has only one valid coordinate</li>
  <li>a LineString if input has only a valid segment</li>
  <li>a Polygon in most cases</li>
  <li>a MultiPolygon if input has a self-intersection</li>
  <li>a GeometryCollection if input has degenerate parts (ex. degenerate
  holes)</li>
  </ul>
  
  @param polygon the Polygon to make valid
  @return a valid Geometry which may be of any type if the source geometry
  is not valid.