All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.annimon.stream.operator.ObjChunkBy Maven / Gradle / Ivy

The newest version!
package com.annimon.stream.operator;

import com.annimon.stream.function.Function;
import com.annimon.stream.iterator.LsaIterator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ObjChunkBy extends LsaIterator> {

    private final Iterator iterator;
    private final Function classifier;
    private T next;
    private boolean peekedNext;

    public ObjChunkBy(Iterator iterator, Function classifier) {
        this.iterator = iterator;
        this.classifier = classifier;
    }

    @Override
    public boolean hasNext() {
        return peekedNext || iterator.hasNext();
    }

    @Override
    public List nextIteration() {
        final K key = classifier.apply(peek());

        final List list = new ArrayList();
        do {
            list.add(takeNext());
        } while ( iterator.hasNext() && key.equals(classifier.apply(peek())) );

        return list;
    }

    private T takeNext() {
        final T element = peek();
        peekedNext = false;
        return element;
    }

    private T peek() {
        if (!peekedNext) {
            next = iterator.next();
            peekedNext = true;
        }
        return next;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy