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

dz.jtsgen.processor.util.StreamUtils Maven / Gradle / Ivy

There is a newer version: 0.5.0
Show newest version
/*
 * Copyright (c) 2017 Dragan Zuvic
 *
 * This file is part of jtsgen.
 *
 * jtsgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jtsgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jtsgen.  If not, see http://www.gnu.org/licenses/
 *
 */

package dz.jtsgen.processor.util;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * Collection of stream related utility functions.
 */
public final class StreamUtils {

    private static  Stream zip(Stream x, Stream y, BiFunction f) {
        final Iterator iter = y.iterator();
        return x.filter( it -> iter.hasNext()).map(it -> f.apply(it, iter.next()));
    }

    /**
     * a simple zip function
     *
     * @param x first stream to zip
     * @param y second stream to zip
     * @param  type of the first stream
     * @param  type of the second stream
     * @throws IllegalArgumentException if one of the arguments is null
     * @return a Stream of type Tuple<X,Y>
     */
    public static  Stream> zip(Stream x, Stream y) {
        if ( x == null || y == null) throw new IllegalArgumentException("one zip arguments null");
        return zip(x, y, Tuple::new);
    }

    /**
     *
     * @param optionals functions returning optional
     * @param  common Type
     * @return the first result of the function not beeing empty
     */
    @SafeVarargs
    public static  Optional firstOptional(Supplier> ... optionals) {
        return Arrays.stream(optionals)
                .map(Supplier::get)
                .filter(Optional::isPresent)
                .findFirst()
                .orElseGet(Optional::empty);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy