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

com.gs.collections.impl.utility.LazyIterate Maven / Gradle / Ivy

Go to download

GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map implementations with a rich API and set of utility classes that work with any JDK compatible Collections, Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.

There is a newer version: 7.0.3
Show newest version
/*
 * Copyright 2015 Goldman Sachs.
 *
 * 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 com.gs.collections.impl.utility;

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

/**
 * 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 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);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy