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

org.eclipse.collections.impl.utility.LazyIterate Maven / Gradle / Ivy

Go to download

Builds the commons-text. Requires eclipse-collections-api be built first and be excluded from any other poms requiring it.

There is a newer version: 11.1.0-r13
Show newest version
/*
 * Copyright (c) 2021 Goldman Sachs.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

package org.eclipse.collections.impl.utility;

import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.lazy.ChunkIterable;
import org.eclipse.collections.impl.lazy.CollectIterable;
import org.eclipse.collections.impl.lazy.CompositeIterable;
import org.eclipse.collections.impl.lazy.DistinctIterable;
import org.eclipse.collections.impl.lazy.DropIterable;
import org.eclipse.collections.impl.lazy.DropWhileIterable;
import org.eclipse.collections.impl.lazy.FlatCollectIterable;
import org.eclipse.collections.impl.lazy.LazyIterableAdapter;
import org.eclipse.collections.impl.lazy.RejectIterable;
import org.eclipse.collections.impl.lazy.SelectInstancesOfIterable;
import org.eclipse.collections.impl.lazy.SelectIterable;
import org.eclipse.collections.impl.lazy.TakeIterable;
import org.eclipse.collections.impl.lazy.TakeWhileIterable;
import org.eclipse.collections.impl.lazy.TapIterable;
import org.eclipse.collections.impl.lazy.ZipIterable;
import org.eclipse.collections.impl.lazy.ZipWithIndexIterable;
import org.eclipse.collections.impl.tuple.Tuples;

/**
 * LazyIterate is a factory class which creates "deferred" iterables around the specified iterables. A "deferred"
 * iterable performs some operation, such as filtering or transforming, when the result iterable is iterated over. This
 * makes the operation very memory efficient, because you don't have to create intermediate collections during the
 * operation.
 *
 * @since 1.0
 */
public final class LazyIterate
{
    private static final LazyIterable EMPTY_ITERABLE = Lists.immutable.empty().asLazy();

    private LazyIterate()
    {
        throw new AssertionError("Suppress default constructor for noninstantiability");
    }

    /**
     * Creates a deferred rich iterable for the specified iterable.
     */
    public static  LazyIterable adapt(Iterable iterable)
    {
        return new LazyIterableAdapter<>(iterable);
    }

    /**
     * Creates a deferred filtering iterable for the specified iterable.
     */
    public static  LazyIterable select(Iterable iterable, Predicate predicate)
    {
        return new SelectIterable<>(iterable, predicate);
    }

    /**
     * Creates a deferred negative filtering iterable for the specified iterable.
     */
    public static  LazyIterable reject(Iterable iterable, Predicate predicate)
    {
        return new RejectIterable<>(iterable, predicate);
    }

    public static  LazyIterable selectInstancesOf(Iterable iterable, Class clazz)
    {
        return new SelectInstancesOfIterable<>(iterable, clazz);
    }

    /**
     * Creates a deferred transforming iterable for the specified iterable.
     */
    public static  LazyIterable collect(
            Iterable iterable,
            Function function)
    {
        return new CollectIterable<>(iterable, function);
    }

    /**
     * Creates a deferred flattening iterable for the specified iterable.
     */
    public static  LazyIterable flatCollect(
            Iterable iterable,
            Function> function)
    {
        return new FlatCollectIterable<>(iterable, function);
    }

    /**
     * Creates a deferred filtering and transforming iterable for the specified iterable.
     */
    public static  LazyIterable collectIf(
            Iterable iterable,
            Predicate predicate,
            Function function)
    {
        return LazyIterate.select(iterable, predicate).collect(function);
    }

    /**
     * Creates a deferred take iterable for the specified iterable using the specified count as the limit.
     */
    public static  LazyIterable take(Iterable iterable, int count)
    {
        return new TakeIterable<>(iterable, count);
    }

    /**
     * Creates a deferred drop iterable for the specified iterable using the specified count as the size to drop.
     */
    public static  LazyIterable drop(Iterable iterable, int count)
    {
        return new DropIterable<>(iterable, count);
    }

    /**
     * Creates a deferred takeWhile iterable for the specified iterable using the specified predicate.
     * Short circuits at the first element which does not satisfy the Predicate.
     *
     * @since 8.0
     */
    public static  LazyIterable takeWhile(Iterable iterable, Predicate predicate)
    {
        return new TakeWhileIterable<>(iterable, predicate);
    }

    /**
     * Creates a deferred dropWhile iterable for the specified iterable using the specified count as the size to drop.
     * Short circuits at the first element which satisfies the Predicate.
     *
     * @since 8.0
     */
    public static  LazyIterable dropWhile(Iterable iterable, Predicate predicate)
    {
        return new DropWhileIterable<>(iterable, predicate);
    }

    /**
     * Creates a deferred distinct iterable for the specified iterable.
     *
     * @since 5.0
     */
    public static  LazyIterable distinct(Iterable iterable)
    {
        return new DistinctIterable<>(iterable);
    }

    /**
     * Combines iterables into a deferred composite iterable.
     */
    public static  LazyIterable concatenate(Iterable... iterables)
    {
        return CompositeIterable.with(iterables);
    }

    public static  LazyIterable empty()
    {
        return (LazyIterable) EMPTY_ITERABLE;
    }

    public static  LazyIterable> zip(Iterable as, Iterable bs)
    {
        return new ZipIterable<>(as, bs);
    }

    public static  LazyIterable> zipWithIndex(Iterable iterable)
    {
        return new ZipWithIndexIterable<>(iterable);
    }

    public static  LazyIterable> chunk(Iterable iterable, int size)
    {
        return new ChunkIterable<>(iterable, size);
    }

    /**
     * Creates a deferred tap iterable for the specified iterable.
     *
     * @since 6.0
     */
    public static  LazyIterable tap(Iterable iterable, Procedure procedure)
    {
        return new TapIterable<>(iterable, procedure);
    }

    /**
     * Create a deferred cartesian product of the two specified iterables.
     *
     * See {@link LazyIterate#cartesianProduct(Iterable, Iterable, Function2)} about performance and presence of duplicates.
     *
     * @since 10.0
     */
    public static  LazyIterable> cartesianProduct(Iterable iterable1, Iterable iterable2)
    {
        return LazyIterate.cartesianProduct(iterable1, iterable2, Tuples::pair);
    }

    /**
     * Create a deferred cartesian product of the two specified iterables.
     *
     * This operation has O(n^2) performance.
     *
     * The presence of duplicates in the resulting iterable is both dependent on the
     * presence of duplicates in the two specified iterables, and on the behaviour
     * of the terminating operation that is applied to the resulting lazy iterable.
     *
     * @since 10.0
     */
    public static  LazyIterable cartesianProduct(Iterable iterable1, Iterable iterable2, Function2 function)
    {
        return LazyIterate.flatCollect(iterable1, first -> LazyIterate.collect(iterable2, second -> function.value(first, second)));
    }
}