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

net.dongliu.commons.collection.ExStream Maven / Gradle / Ivy

The newest version!
package net.dongliu.commons.collection;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.function.*;
import java.util.stream.*;

/**
 * Expanded stream with toList(), toSet() and other reduce method.
 *
 * @author Liu Dong
 */
public interface ExStream extends Stream {

    static  ExStream wrap(Stream stream) {
        if (stream instanceof ForwardingStream) {
            return (ForwardingStream) stream;
        } else {
            return new ForwardingStream<>(stream);
        }
    }

    /**
     * Create stream from array or multi values
     */
    @SafeVarargs
    static  ExStream wrap(T... stream) {
        return wrap(Arrays.stream(stream));
    }

    @Override
    ExStream filter(Predicate predicate);

    @Override
     ExStream map(Function mapper);

    @Override
    ExIntStream mapToInt(ToIntFunction mapper);

    @Override
    ExLongStream mapToLong(ToLongFunction mapper);

    @Override
    ExDoubleStream mapToDouble(ToDoubleFunction mapper);

    @Override
     ExStream flatMap(Function> mapper);

    @Override
    ExIntStream flatMapToInt(Function mapper);

    @Override
    ExLongStream flatMapToLong(Function mapper);

    @Override
    ExDoubleStream flatMapToDouble(Function mapper);

    @Override
    ExStream distinct();

    @Override
    ExStream sorted();

    @Override
    ExStream sorted(Comparator comparator);

    @Override
    ExStream peek(Consumer action);

    @Override
    ExStream limit(long maxSize);

    @Override
    ExStream skip(long n);

    @Override
    ExStream sequential();

    @Override
    ExStream parallel();

    @Override
    ExStream unordered();

    @Override
    ExStream onClose(Runnable closeHandler);

    /**
     * Collect items to a list
     */
    default ExList toList() {
        return toArrayList();
    }

    /**
     * Collect items to array list
     */
    default ExArrayList toArrayList() {
        return collect(new CollectorImpl<>((Supplier>) ExArrayList::new, List::add,
                (left, right) -> {
                    left.addAll(right);
                    return left;
                },
                CollectorImpl.CH_ID));
    }

    /**
     * Collect items to linked list
     */
    default ExArrayList toLinkedList() {
        return collect(new CollectorImpl<>((Supplier>) ExLinkedList::new, List::add,
                (left, right) -> {
                    left.addAll(right);
                    return left;
                },
                CollectorImpl.CH_ID));
    }

    /**
     * Collect item to a set
     */
    default ExSet toSet() {
        return toHashSet();
    }


    /**
     * Collect item to a hash set
     */
    default ExHashSet toHashSet() {
        return collect(new CollectorImpl<>((Supplier>) ExHashSet::new, Set::add,
                (left, right) -> {
                    left.addAll(right);
                    return left;
                },
                CollectorImpl.CH_UNORDERED_ID));
    }

    /**
     * Collect item to a linked hash set
     */
    default ExLinkedHashSet toLinkedHashSet() {
        return collect(new CollectorImpl<>((Supplier>) ExLinkedHashSet::new, Set::add,
                (left, right) -> {
                    left.addAll(right);
                    return left;
                },
                CollectorImpl.CH_UNORDERED_ID));
    }

    /**
     * Collect item to a linked hash set
     */
    default ExTreeSet toTreeSet() {
        return collect(new CollectorImpl<>((Supplier>) ExTreeSet::new, Set::add,
                (left, right) -> {
                    left.addAll(right);
                    return left;
                },
                CollectorImpl.CH_UNORDERED_ID));
    }


    /**
     * Collect item to a map.
     * If the mapped keys contains duplicates, later value will override the older value.
     *
     * @param keyMapper   get map key
     * @param valueMapper get map value
     * @param          key type
     * @param          value type
     * @return the map
     */
    default  ExMap toMap(Function keyMapper,
                                           Function valueMapper) {
        return toHashMap(keyMapper, valueMapper);
    }

    /**
     * Collect item to a map.
     *
     * @param keyMapper   get map key
     * @param valueMapper get map value
     * @param merger      a merge function, used to resolve collisions between
     *                    values associated with the same key
     * @param          key type
     * @param          value type
     * @return the map
     */
    default  ExMap toMap(Function keyMapper,
                                           Function valueMapper,
                                           BinaryOperator merger) {
        return toHashMap(keyMapper, valueMapper, merger);
    }

    /**
     * Collect item to a hash map.
     * If the mapped keys contains duplicates, later value will override the older value.
     *
     * @param keyMapper   get map key
     * @param valueMapper get map value
     * @param          key type
     * @param          value type
     * @return the map
     */
    default  ExHashMap toHashMap(Function keyMapper,
                                                   Function valueMapper) {
        return toHashMap(keyMapper, valueMapper, (older, newer) -> newer);
    }

    /**
     * Collect item to a hash map.
     *
     * @param keyMapper   get map key
     * @param valueMapper get map value
     * @param merger      a merge function, used to resolve collisions between
     *                    values associated with the same key
     * @param          key type
     * @param          value type
     * @return the map
     */
    default  ExHashMap toHashMap(Function keyMapper,
                                                   Function valueMapper,
                                                   BinaryOperator merger) {
        return collect(CollectorImpl.toMap(keyMapper, valueMapper, merger, ExHashMap::new));
    }

    /**
     * Collect item to a linked hash map.
     * If the mapped keys contains duplicates, later value will override the older value.
     *
     * @param keyMapper   get map key
     * @param valueMapper get map value
     * @param          key type
     * @param          value type
     * @return the map
     */
    default  ExLinkedHashMap toLinkedHashMap(
            Function keyMapper,
            Function valueMapper) {
        return toLinkedHashMap(keyMapper, valueMapper, (older, newer) -> newer);
    }

    /**
     * Collect item to a linked hash map.
     *
     * @param keyMapper   get map key
     * @param valueMapper get map value
     * @param merger      a merge function, used to resolve collisions between
     *                    values associated with the same key
     * @param          key type
     * @param          value type
     * @return the map
     */
    default  ExLinkedHashMap toLinkedHashMap(
            Function keyMapper,
            Function valueMapper,
            BinaryOperator merger) {
        return collect(CollectorImpl.toMap(keyMapper, valueMapper, merger, ExLinkedHashMap::new));
    }

    /**
     * Collect item to a tree map.
     * If the mapped keys contains duplicates, later value will override the older value.
     *
     * @param keyMapper   get map key
     * @param valueMapper get map value
     * @param          key type
     * @param          value type
     * @return the map
     */
    default  ExTreeMap toTreeMap(
            Function keyMapper,
            Function valueMapper) {
        return toTreeMap(keyMapper, valueMapper, (older, newer) -> newer);
    }

    /**
     * Collect item to a tree map.
     *
     * @param keyMapper   get map key
     * @param valueMapper get map value
     * @param merger      a merge function, used to resolve collisions between
     *                    values associated with the same key
     * @param          key type
     * @param          value type
     * @return the map
     */
    default  ExTreeMap toTreeMap(
            Function keyMapper,
            Function valueMapper,
            BinaryOperator merger) {
        return collect(CollectorImpl.toMap(keyMapper, valueMapper, merger, ExTreeMap::new));
    }


    /**
     * Returns a concurrent {@code Collector} that accumulates elements into a
     * {@code ConcurrentMap} whose keys and values are the result of applying
     * the provided mapping functions to the input elements.
     * If the mapped keys contains duplicates, later value will override the older value.
     *
     * @param keyMapper   get map key
     * @param valueMapper get map value
     * @param          key type
     * @param          value type
     * @return the map
     */
    default  ConcurrentMap toConcurrentMap(Function keyMapper,
                                                       Function valueMapper) {
        return collect(Collectors.toConcurrentMap(keyMapper, valueMapper, (older, newer) -> newer));
    }


    /**
     * Returns a concurrent {@code Collector} that accumulates elements into a
     * {@code ConcurrentMap} whose keys and values are the result of applying
     * the provided mapping functions to the input elements.
     *
     * @param keyMapper   get map key
     * @param valueMapper get map value
     * @param merger      a merge function, used to resolve collisions between
     *                    values associated with the same key
     * @param          key type
     * @param          value type
     * @return the map
     */
    default  ConcurrentMap toConcurrentMap(Function keyMapper,
                                                       Function valueMapper,
                                                       BinaryOperator merger) {
        return collect(Collectors.toConcurrentMap(keyMapper, valueMapper, merger));
    }

    /**
     * join list items to string
     */
    default String join() {
        return map(e -> e == null ? "null" : e.toString()).collect(Collectors.joining());
    }

    /**
     * join list items with delimiter to string
     */
    default String join(CharSequence delimiter) {
        return map(e -> e == null ? "null" : e.toString()).collect(Collectors.joining(delimiter));
    }

    /**
     * join list items with delimiter, prefix, suffix to string
     */
    default String join(CharSequence delimiter,
                        CharSequence prefix,
                        CharSequence suffix) {
        return map(e -> e == null ? "null" : e.toString())
                .collect(Collectors.joining(delimiter, prefix, suffix));
    }

}