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

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

/*
 * 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.function.Predicate;
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 GroupingOnSplittingSpliterator implements Spliterator> {

    private final Spliterator spliterator;
    private final Predicate splitter;
    private final boolean included;

    private Stream.Builder currentBuidler = Stream.builder();
    private Stream.Builder nextBuilder = Stream.builder();
    private boolean gateOpen = false;
    private boolean groupReady = false;
    private boolean builderReady = true;
    private boolean hasMore = true;

    public static  GroupingOnSplittingSpliterator of(
            Spliterator spliterator,
            Predicate splitter, boolean included) {
        Objects.requireNonNull(spliterator);
        Objects.requireNonNull(splitter);

        if ((spliterator.characteristics() & Spliterator.ORDERED) == 0) {
            throw new IllegalArgumentException("Why would you build a grouping on gating spliterator on a non-ordered spliterator?");
        }

        return new GroupingOnSplittingSpliterator(spliterator, splitter, included);
    }

    private GroupingOnSplittingSpliterator(
            Spliterator spliterator,
            Predicate splitter, boolean included) {
        this.spliterator = spliterator;
        this.splitter = splitter;
        this.included = included;
    }

    @Override
    public boolean tryAdvance(Consumer> action) {
        if (!hasMore) {
            return false;
        }
        hasMore = true;
        while (hasMore && !groupReady) {
            hasMore = spliterator.tryAdvance(e -> {
                if (gateOpen && splitter.test(e)) {
                    if (included) {
                        nextBuilder.add(e);
                    }
                    groupReady = true;
                }
                if (gateOpen && !splitter.test(e)) {
                    currentBuidler.add(e);
                    builderReady = false;
                }
                if (!gateOpen && splitter.test(e)) {
                    gateOpen = true;
                    if (included) {
                        currentBuidler.add(e);
                    }
                    builderReady = false;
                }
            });
        }

        if ((groupReady || !hasMore) && !builderReady) {
            action.accept(currentBuidler.build());
            currentBuidler = nextBuilder;
            groupReady = false;
            if (hasMore) {
                nextBuilder = Stream.builder();
            }
        }

        return true;
    }

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

    @Override
    public long estimateSize() {
        return 0L;
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy