class CommonAlgorithmsFromCartAGen {

  public static List<IDirectPosition> getSelfIntersections(ILineString line) {
    List<IDirectPosition> selfIntersections = new ArrayList<>();
    List<Segment> segments = Segment.getSegmentList(line);
    Set<Segment> remainingSegments = new HashSet<>(segments);

    for (int i = 0; i < segments.size(); i++) {
      Segment segment = segments.get(i);
      remainingSegments.remove(segment);
      Set<Segment> toTest = new HashSet<>(remainingSegments);
      // remove the next segment
      if (i < segments.size() - 1)
        toTest.remove(segments.get(i + 1));
      for (Segment other : toTest) {
        if (segment.intersects(other)) {
          if (!selfIntersections
              .contains(segment.intersection(other).centroid()))
            selfIntersections.add(segment.intersection(other).centroid());
        }
      }
    }
    return selfIntersections;
  }

}