bhlangonijr / chesslib

chess library for legal move generation, FEN/PGN parsing and more
Apache License 2.0
229 stars 80 forks source link

GameIterator.hasNext and .next incorrect implementation #65

Closed liebig closed 2 years ago

liebig commented 3 years ago

The iterator implementations are unfortunately not correct and can lead to errors:

public boolean hasNext() {
     game = GameLoader.loadNextGame(pgnLines);
     return game != null;
 }

public Game next() {
    return game;
}

According to the Iterator interface, the functions should be implemented as follows: image

Therefore, hasNext should be callable any number of times without the iterator loading the next element. Only next shall return the next iterative object.

liebig commented 3 years ago

The same applies to the LargeFile class:

        public boolean hasNext() {
            try {
                currentLine = reader.readLine();
            } catch (Exception ex) {
                currentLine = null;
                ex.printStackTrace();
            }

            return currentLine != null;
        }

        public String next() {
            return currentLine;
        }