code: private static LinearRing checkLineString(Geometry geometry) throws IllegalArgumentException {
          if (geometry instanceof LinearRing) {
              return (LinearRing) geometry;
  
          } else if (geometry instanceof LineString) {
              LineString lineString = (LineString) geometry;
              if (lineString.isClosed()) {
                  return geometry.getFactory().createLinearRing(lineString.getCoordinateSequence());
              } else {
                  throw new IllegalArgumentException("The linestring must be closed.");
              }
          } else  {
              throw new IllegalArgumentException("Only support linestring.");
          }
      }
docs: Check if a geometry is a linestring and if its closed.
  
  @param geometry
  @return
  @throws IllegalArgumentException