import io.atomix.storage.journal.index.JournalIndex;
import io.atomix.storage.journal.index.Position;
-import java.util.NoSuchElementException;
import org.eclipse.jdt.annotation.Nullable;
abstract sealed class JournalSegmentReader<E> permits DiskJournalSegmentReader, MappedJournalSegmentReader {
return currentEntry != null ? currentEntry.index() + 1 : firstIndex;
}
- /**
- * Returns whether the reader has a next entry to read.
- *
- * @return Whether the reader has a next entry to read.
- */
- final boolean hasNext() {
- return nextEntry != null || (nextEntry = readNext()) != null;
- }
-
/**
* Returns the next entry in the reader.
*
- * @return The next entry in the reader.
- * @throws UnsupportedOperationException if there is no such entry
+ * @return The next entry in the reader, or {@code null}
*/
- final Indexed<E> next() {
- if (!hasNext()) {
- throw new NoSuchElementException();
+ final @Nullable Indexed<E> tryNext() {
+ if (nextEntry == null) {
+ nextEntry = readNext();
+ }
+ if (nextEntry == null) {
+ return null;
}
// Set the current entry to the next entry.
setPosition(position.position());
nextEntry = readNext();
}
- while (getNextIndex() < index && hasNext()) {
- next();
+ while (getNextIndex() < index && tryNext() != null) {
+ // Nothing else
}
}
@Override
public Indexed<E> tryNext() {
- if (currentReader.hasNext()) {
- previousEntry = currentReader.getCurrentEntry();
- return currentReader.next();
+ final var current = currentReader.getCurrentEntry();
+ final var next = currentReader.tryNext();
+ if (next != null) {
+ previousEntry = current;
+ return next;
}
final var nextSegment = journal.getNextSegment(currentSegment.index());
currentSegment = nextSegment;
currentReader = currentSegment.createReader();
- return currentReader.hasNext() ? currentReader.next() : null;
+ return currentReader.tryNext();
}
@Override