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

org.paumard.spliterators.ZippingSpliterator 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.BiFunction;
import java.util.function.Consumer;

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

    private final Spliterator spliterator1;
    private final Spliterator spliterator2;
    private final BiFunction zipper;

    public static class Builder {

        private Spliterator spliterator1;
        private Spliterator spliterator2;
        private BiFunction zipper;

        public Builder() {
        }

        public Builder with(Spliterator spliterator) {
            this.spliterator1 = Objects.requireNonNull(spliterator);
            return this;
        }

        public Builder and(Spliterator spliterator) {
            this.spliterator2 = Objects.requireNonNull(spliterator);
            return this;
        }

        public Builder mergedBy(BiFunction zipper) {
            this.zipper = Objects.requireNonNull(zipper);
            return this;
        }

        public ZippingSpliterator build() {
            return new ZippingSpliterator<>(spliterator1, spliterator2, zipper);
        }
    }

    ZippingSpliterator(
            Spliterator spliterator1, Spliterator spliterator2,
            BiFunction zipper) {
        this.spliterator1 = Objects.requireNonNull(spliterator1, "spliterator1");
        this.spliterator2 = Objects.requireNonNull(spliterator2, "spliterator2");
        this.zipper = Objects.requireNonNull(zipper, "zipper");
    }

    @Override
    public boolean tryAdvance(Consumer action) {
        return spliterator1.tryAdvance(
                e1 -> {
                    spliterator2.tryAdvance(e2 -> {
                        action.accept(Objects.requireNonNull(zipper.apply(e1, e2)));
                    });
                }
        );
    }

    @Override
    public Spliterator trySplit() {
        Spliterator splitedSpliterator1 = spliterator1.trySplit();
        Spliterator splitedSpliterator2 = spliterator2.trySplit();

        return splitedSpliterator1 == null || splitedSpliterator2 == null ? null :
                new ZippingSpliterator<>(splitedSpliterator1, splitedSpliterator2, zipper);
    }

    @Override
    public long estimateSize() {
        return Long.min(spliterator1.estimateSize(), spliterator2.estimateSize());
    }

    @Override
    public int characteristics() {
        return this.spliterator1.characteristics() & this.spliterator2.characteristics() & ~Spliterator.SORTED;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy