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

pl.jalokim.utils.collection.Elements Maven / Gradle / Ivy

package pl.jalokim.utils.collection;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableSet;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
 * Simpler API than native java Stream API.
 * Contains some shortcut methods for return some types.
 * @param 
 */
public class Elements {

    private final Stream stream;

    private Elements(Stream stream) {
        this.stream = stream;
    }

    public static  Elements elements(Iterable iterable) {
        return new Elements<>(StreamSupport.stream(iterable.spliterator(), false));
    }

    public static  Elements elements(T... array) {
        return new Elements<>(Stream.of(array));
    }

    public static  Elements elements(Stream stream) {
        return new Elements<>(stream);
    }

    public Elements filter(Predicate predicate) {
        return new Elements<>(stream.filter(predicate));
    }

    public  Elements map(Function mapper) {
        return new Elements<>(this.stream.map(mapper));
    }

    public List asList() {
        return unmodifiableList(new ArrayList<>(stream.collect(toList())));
    }

    public Set asSet() {
        return unmodifiableSet(new HashSet<>(stream.collect(toSet())));
    }

    public T[] asArray(T[] array) {
        return stream.collect(Collectors.toList()).toArray(array);
    }

    public Stream asStream() {
        return stream;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy