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

org.eclipse.collections.impl.lazy.AbstractLazyIterable Maven / Gradle / Ivy

There is a newer version: 12.0.0.M3
Show newest version
/*
 * Copyright (c) 2015 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.lazy;

import java.util.Collection;

import net.jcip.annotations.Immutable;
import org.eclipse.collections.api.LazyBooleanIterable;
import org.eclipse.collections.api.LazyByteIterable;
import org.eclipse.collections.api.LazyCharIterable;
import org.eclipse.collections.api.LazyDoubleIterable;
import org.eclipse.collections.api.LazyFloatIterable;
import org.eclipse.collections.api.LazyIntIterable;
import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.LazyLongIterable;
import org.eclipse.collections.api.LazyShortIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
import org.eclipse.collections.api.block.function.primitive.ByteFunction;
import org.eclipse.collections.api.block.function.primitive.CharFunction;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.ShortFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.multimap.Multimap;
import org.eclipse.collections.api.partition.list.PartitionMutableList;
import org.eclipse.collections.api.stack.MutableStack;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.AbstractRichIterable;
import org.eclipse.collections.impl.block.factory.Functions;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.factory.Procedures2;
import org.eclipse.collections.impl.block.procedure.MutatingAggregationProcedure;
import org.eclipse.collections.impl.block.procedure.NonMutatingAggregationProcedure;
import org.eclipse.collections.impl.block.procedure.PartitionProcedure;
import org.eclipse.collections.impl.lazy.primitive.CollectBooleanIterable;
import org.eclipse.collections.impl.lazy.primitive.CollectByteIterable;
import org.eclipse.collections.impl.lazy.primitive.CollectCharIterable;
import org.eclipse.collections.impl.lazy.primitive.CollectDoubleIterable;
import org.eclipse.collections.impl.lazy.primitive.CollectFloatIterable;
import org.eclipse.collections.impl.lazy.primitive.CollectIntIterable;
import org.eclipse.collections.impl.lazy.primitive.CollectLongIterable;
import org.eclipse.collections.impl.lazy.primitive.CollectShortIterable;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.multimap.list.FastListMultimap;
import org.eclipse.collections.impl.partition.list.PartitionFastList;
import org.eclipse.collections.impl.stack.mutable.ArrayStack;
import org.eclipse.collections.impl.utility.LazyIterate;

/**
 * AbstractLazyIterable provides a base from which deferred iterables such as SelectIterable,
 * RejectIterable and CollectIterable can be derived.
 */
@Immutable
public abstract class AbstractLazyIterable
        extends AbstractRichIterable
        implements LazyIterable
{
    @Override
    public LazyIterable asLazy()
    {
        return this;
    }

    public > R into(R target)
    {
        this.forEachWith(Procedures2.addToCollection(), target);
        return target;
    }

    @Override
    public  E[] toArray(E[] array)
    {
        return this.toList().toArray(array);
    }

    public int size()
    {
        return this.count(Predicates.alwaysTrue());
    }

    @Override
    public boolean isEmpty()
    {
        return !this.anySatisfy(Predicates.alwaysTrue());
    }

    public T getFirst()
    {
        return this.detect(Predicates.alwaysTrue());
    }

    public T getLast()
    {
        final T[] result = (T[]) new Object[1];
        this.forEach(new Procedure()
        {
            public void value(T each)
            {
                result[0] = each;
            }
        });
        return result[0];
    }

    public LazyIterable select(Predicate predicate)
    {
        return LazyIterate.select(this, predicate);
    }

    public 

LazyIterable selectWith(Predicate2 predicate, P parameter) { return LazyIterate.select(this, Predicates.bind(predicate, parameter)); } public LazyIterable reject(Predicate predicate) { return LazyIterate.reject(this, predicate); } public

LazyIterable rejectWith(Predicate2 predicate, P parameter) { return LazyIterate.reject(this, Predicates.bind(predicate, parameter)); } public PartitionMutableList partition(Predicate predicate) { PartitionMutableList partitionMutableList = new PartitionFastList(); this.forEach(new PartitionProcedure(predicate, partitionMutableList)); return partitionMutableList; } public

PartitionMutableList partitionWith(Predicate2 predicate, P parameter) { return this.partition(Predicates.bind(predicate, parameter)); } public LazyIterable selectInstancesOf(Class clazz) { return LazyIterate.selectInstancesOf(this, clazz); } public LazyIterable collect(Function function) { return LazyIterate.collect(this, function); } public LazyBooleanIterable collectBoolean(BooleanFunction booleanFunction) { return new CollectBooleanIterable(this, booleanFunction); } public LazyByteIterable collectByte(ByteFunction byteFunction) { return new CollectByteIterable(this, byteFunction); } public LazyCharIterable collectChar(CharFunction charFunction) { return new CollectCharIterable(this, charFunction); } public LazyDoubleIterable collectDouble(DoubleFunction doubleFunction) { return new CollectDoubleIterable(this, doubleFunction); } public LazyFloatIterable collectFloat(FloatFunction floatFunction) { return new CollectFloatIterable(this, floatFunction); } public LazyIntIterable collectInt(IntFunction intFunction) { return new CollectIntIterable(this, intFunction); } public LazyLongIterable collectLong(LongFunction longFunction) { return new CollectLongIterable(this, longFunction); } public LazyShortIterable collectShort(ShortFunction shortFunction) { return new CollectShortIterable(this, shortFunction); } public LazyIterable collectWith(Function2 function, P parameter) { return LazyIterate.collect(this, Functions.bind(function, parameter)); } public LazyIterable flatCollect(Function> function) { return LazyIterate.flatCollect(this, function); } public LazyIterable concatenate(Iterable iterable) { return LazyIterate.concatenate(this, iterable); } public LazyIterable collectIf(Predicate predicate, Function function) { return LazyIterate.collectIf(this, predicate, function); } public LazyIterable take(int count) { return LazyIterate.take(this, count); } public LazyIterable drop(int count) { return LazyIterate.drop(this, count); } public LazyIterable distinct() { return LazyIterate.distinct(this); } public MutableStack toStack() { return ArrayStack.newStack(this); } public Multimap groupBy(Function function) { return this.groupBy(function, FastListMultimap.newMultimap()); } public Multimap groupByEach(Function> function) { return this.groupByEach(function, FastListMultimap.newMultimap()); } public MapIterable groupByUniqueKey(Function function) { return this.groupByUniqueKey(function, UnifiedMap.newMap()); } public LazyIterable> zip(Iterable that) { return LazyIterate.zip(this, that); } public LazyIterable> zipWithIndex() { return LazyIterate.zipWithIndex(this); } public LazyIterable> chunk(int size) { return LazyIterate.chunk(this, size); } public LazyIterable tap(Procedure procedure) { return LazyIterate.tap(this, procedure); } public MapIterable aggregateInPlaceBy( Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new MutatingAggregationProcedure(map, groupBy, zeroValueFactory, mutatingAggregator)); return map; } public MapIterable aggregateBy( Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new NonMutatingAggregationProcedure(map, groupBy, zeroValueFactory, nonMutatingAggregator)); return map; } }