<commit_before>package net.emaze.dysfunctional.multiplexing;

import java.util.ArrayList;
import java.util.Iterator;
import net.emaze.dysfunctional.collections.ArrayListFactory;
import net.emaze.dysfunctional.consumers.ConsumeIntoCollection;
import net.emaze.dysfunctional.contracts.dbc;
import net.emaze.dysfunctional.iterations.ReadOnlyIterator;

/**
 * A cyclic iterator. The given iterator is iterated indefinitely.
 *
 * @param <E> the element type
 * @author mcodella, asturman
 */
public class CyclicIterator<E> extends ReadOnlyIterator<E> {

    private final Iterable<E> matrix;
    private Iterator<E> current;
    
    public CyclicIterator(Iterator<E> iterator) {
        dbc.precondition(iterator != null, "iterator cannot be null");
        dbc.precondition(iterator.hasNext(), "iterator cannot be empty");
        this.matrix = new ConsumeIntoCollection<ArrayList<E>, E>(new ArrayListFactory<E>()).perform(iterator);
        this.current = matrix.iterator();
    }
    
    @Override
    public boolean hasNext() {
        return true;
    }

    @Override
    public E next() {
        if(current.hasNext() == false) {
            current = matrix.iterator();
        }
        return current.next();
    }
}<commit_msg>fix: lazyfied CyclicIterator<commit_after>package net.emaze.dysfunctional.multiplexing;

import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import net.emaze.dysfunctional.contracts.dbc;
import net.emaze.dysfunctional.iterations.ReadOnlyIterator;

/**
 * A cyclic iterator. The given iterator is iterated indefinitely.
 *
 * @param <E> the element type
 * @author mcodella, asturman
 */
public class CyclicIterator<E> extends ReadOnlyIterator<E> {

    private final Deque<E> memory = new LinkedList<E>();
    private Iterator<E> source;

    public CyclicIterator(Iterator<E> source) {
        dbc.precondition(source != null, "source iterator cannot be null");
        dbc.precondition(source.hasNext(), "source iterator cannot be empty");
        this.source = source;
    }

    @Override
    public boolean hasNext() {
        return true;
    }

    @Override
    public E next() {
        final E value = source.hasNext() ? source.next() : memory.removeLast();
        memory.push(value);
        return value;
    }
}
{'fraction_non_alphanumeric': 0.11140235910878113, 'fraction_numerical': 0.0, 'mean_word_length': 3.4901960784313726, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5326780', 'n_tokens_mistral': 685, 'n_tokens_neox': 659, 'n_words': 166}