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

org.paumard.spliterators.GroupingSpliterator Maven / Gradle / Ivy

Go to download

Streams Utils is a set of operations written on Java 8 Streams. It allows several basic operations that are not available in the the Java 8, and that have proven to be very useful in some cases.

There is a newer version: 2.1.1
Show newest version
/*
 * Copyright (C) 2015 José Paumard
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.paumard.spliterators;

import org.paumard.streams.StreamsUtils;

import java.util.Objects;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * See the documentation and patterns to be used in this class in the {@link StreamsUtils} factory class.
 *
 * @author José
 */
public class GroupingSpliterator implements Spliterator> {

    private final long grouping;
    private final Spliterator spliterator;
    private Stream.Builder builder = Stream.builder();
    private boolean firstGroup = true;

    public static  GroupingSpliterator of(Spliterator spliterator, long grouping) {
        Objects.requireNonNull(spliterator);
        if (grouping < 2) {
            throw new IllegalArgumentException("Why would you build a grouping spliterator with a grouping factor of less than 2?");
        }
        if ((spliterator.characteristics() & Spliterator.ORDERED) == 0) {
            throw new IllegalArgumentException("Why would you build a grouping spliterator on a non-ordered spliterator?");
        }

        return new GroupingSpliterator<>(spliterator, grouping);
    }

    private GroupingSpliterator(Spliterator spliterator, long grouping) {
        this.spliterator = spliterator;
        this.grouping = grouping;
    }

    @Override
    public boolean tryAdvance(Consumer> action) {
        boolean moreElements = true;
        if (firstGroup) {
            moreElements = spliterator.tryAdvance(builder::add);
            firstGroup = false;
        }
        if (!moreElements) {
            action.accept(builder.build());
            return false;
        }
        for (int i = 1; i < grouping && moreElements; i++) {
            if (!spliterator.tryAdvance(builder::add)) {
                moreElements = false;
            }
        }
        Stream subStream = builder.build();
        action.accept(subStream);
        if (moreElements) {
            builder = Stream.builder();
            moreElements = spliterator.tryAdvance(builder::add);
        }

        return moreElements;
    }

    @Override
    public Spliterator> trySplit() {
        Spliterator splitSpliterator = this.spliterator.trySplit();
        return splitSpliterator == null ? null : new GroupingSpliterator<>(splitSpliterator, grouping);
    }

    @Override
    public long estimateSize() {
        long estimateSize = spliterator.estimateSize();
        return estimateSize == Long.MAX_VALUE ? Long.MAX_VALUE : estimateSize / grouping;
    }

    @Override
    public int characteristics() {
        // this spliterator is already ordered
        return spliterator.characteristics();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy