class JTSUtilities {

    public static CoordinateSequence removeColinearCoordinates(CoordinateSequence coordinateSequence)
    {
        if (coordinateSequence.size() < 3)
        {
            return null;
        }

        List<Coordinate> result = null;

        Coordinate a = coordinateSequence.getCoordinate(0);
        Coordinate b = coordinateSequence.getCoordinate(1);
        Coordinate c;
        int coordIndex = 2;

        do
        {
            c = coordinateSequence.getCoordinate(coordIndex);

            if (duplicateOrColinear(a, b, c))
            {
                if (result == null)
                {
                    // Copy the coordinate over to a new sequence, skipping "b"
                    result = New.list(coordinateSequence.size() - 1);
                    for (int ix = 0; ix < coordIndex - 1; ++ix)
                    {
                        result.add(coordinateSequence.getCoordinate(ix));
                    }
                    result.add(c);
                }
                else
                {
                    // Replace "b" in the new sequence with "c".
                    result.set(result.size() - 1, c);
                }
                b = c;
            }
            else
            {
                a = b;
                b = c;
                if (result != null)
                {
                    result.add(c);
                }
            }
        }
        while (++coordIndex < coordinateSequence.size());

        c = result == null ? coordinateSequence.getCoordinate(1) : result.get(1);
        if (duplicateOrColinear(a, b, c))
        {
            if (result == null)
            {
                result = New.list(coordinateSequence.size() - 1);
                for (int ix = 1; ix < coordinateSequence.size() - 1; ++ix)
                {
                    result.add(coordinateSequence.getCoordinate(ix));
                }
                result.add(coordinateSequence.getCoordinate(1));
            }
            else
            {
                result.remove(0);
                result.set(result.size() - 1, result.get(0));
            }
        }
        return result == null ? null
                : GEOMETRY_FACTORY.getCoordinateSequenceFactory().create(New.array(result, Coordinate.class));
    }

    private  JTSUtilities();

}