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

com.gs.collections.impl.lazy.AbstractLazyIterable Maven / Gradle / Ivy

/*
 * Copyright 2014 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.lazy;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Comparator;

import com.gs.collections.api.LazyBooleanIterable;
import com.gs.collections.api.LazyByteIterable;
import com.gs.collections.api.LazyCharIterable;
import com.gs.collections.api.LazyDoubleIterable;
import com.gs.collections.api.LazyFloatIterable;
import com.gs.collections.api.LazyIntIterable;
import com.gs.collections.api.LazyIterable;
import com.gs.collections.api.LazyLongIterable;
import com.gs.collections.api.LazyShortIterable;
import com.gs.collections.api.RichIterable;
import com.gs.collections.api.bag.MutableBag;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.function.Function0;
import com.gs.collections.api.block.function.Function2;
import com.gs.collections.api.block.function.primitive.BooleanFunction;
import com.gs.collections.api.block.function.primitive.ByteFunction;
import com.gs.collections.api.block.function.primitive.CharFunction;
import com.gs.collections.api.block.function.primitive.DoubleFunction;
import com.gs.collections.api.block.function.primitive.DoubleObjectToDoubleFunction;
import com.gs.collections.api.block.function.primitive.FloatFunction;
import com.gs.collections.api.block.function.primitive.FloatObjectToFloatFunction;
import com.gs.collections.api.block.function.primitive.IntFunction;
import com.gs.collections.api.block.function.primitive.IntObjectToIntFunction;
import com.gs.collections.api.block.function.primitive.LongFunction;
import com.gs.collections.api.block.function.primitive.LongObjectToLongFunction;
import com.gs.collections.api.block.function.primitive.ShortFunction;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.predicate.Predicate2;
import com.gs.collections.api.block.procedure.Procedure2;
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
import com.gs.collections.api.collection.primitive.MutableBooleanCollection;
import com.gs.collections.api.collection.primitive.MutableByteCollection;
import com.gs.collections.api.collection.primitive.MutableCharCollection;
import com.gs.collections.api.collection.primitive.MutableDoubleCollection;
import com.gs.collections.api.collection.primitive.MutableFloatCollection;
import com.gs.collections.api.collection.primitive.MutableIntCollection;
import com.gs.collections.api.collection.primitive.MutableLongCollection;
import com.gs.collections.api.collection.primitive.MutableShortCollection;
import com.gs.collections.api.list.MutableList;
import com.gs.collections.api.map.MapIterable;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.map.sorted.MutableSortedMap;
import com.gs.collections.api.multimap.ImmutableMultimap;
import com.gs.collections.api.multimap.MutableMultimap;
import com.gs.collections.api.partition.list.PartitionMutableList;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.set.sorted.MutableSortedSet;
import com.gs.collections.api.stack.MutableStack;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.block.factory.Comparators;
import com.gs.collections.impl.block.factory.Functions;
import com.gs.collections.impl.block.factory.Predicates;
import com.gs.collections.impl.block.factory.Procedures2;
import com.gs.collections.impl.block.procedure.CountProcedure;
import com.gs.collections.impl.block.procedure.MapCollectProcedure;
import com.gs.collections.impl.block.procedure.MutatingAggregationProcedure;
import com.gs.collections.impl.block.procedure.NonMutatingAggregationProcedure;
import com.gs.collections.impl.factory.Bags;
import com.gs.collections.impl.factory.Lists;
import com.gs.collections.impl.lazy.primitive.CollectBooleanIterable;
import com.gs.collections.impl.lazy.primitive.CollectByteIterable;
import com.gs.collections.impl.lazy.primitive.CollectCharIterable;
import com.gs.collections.impl.lazy.primitive.CollectDoubleIterable;
import com.gs.collections.impl.lazy.primitive.CollectFloatIterable;
import com.gs.collections.impl.lazy.primitive.CollectIntIterable;
import com.gs.collections.impl.lazy.primitive.CollectLongIterable;
import com.gs.collections.impl.lazy.primitive.CollectShortIterable;
import com.gs.collections.impl.map.mutable.UnifiedMap;
import com.gs.collections.impl.map.sorted.mutable.TreeSortedMap;
import com.gs.collections.impl.multimap.list.FastListMultimap;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.set.sorted.mutable.TreeSortedSet;
import com.gs.collections.impl.stack.mutable.ArrayStack;
import com.gs.collections.impl.utility.ArrayIterate;
import com.gs.collections.impl.utility.Iterate;
import com.gs.collections.impl.utility.LazyIterate;
import com.gs.collections.impl.utility.internal.IterableIterate;
import net.jcip.annotations.Immutable;

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

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

    @Override
    public String toString()
    {
        return this.makeString("[", ", ", "]");
    }

    public Object[] toArray()
    {
        return this.toList().toArray();
    }

    public  E[] toArray(E[] array)
    {
        int size = this.size();

        final E[] result = array.length < size
                ? (E[]) Array.newInstance(array.getClass().getComponentType(), size)
                : array;

        this.forEachWithIndex(new ObjectIntProcedure()
        {
            public void value(Object each, int index)
            {
                result[index] = (E) each;
            }
        });
        if (result.length > size)
        {
            result[size] = null;
        }
        return result;
    }

    public boolean contains(Object object)
    {
        return this.anySatisfy(Predicates.equal(object));
    }

    public boolean containsAllIterable(Iterable source)
    {
        return Iterate.allSatisfy(source, new Predicate()
        {
            public boolean accept(Object each)
            {
                return AbstractLazyIterable.this.contains(each);
            }
        });
    }

    public boolean containsAll(Collection source)
    {
        return this.containsAllIterable(source);
    }

    public boolean containsAllArguments(Object... elements)
    {
        return ArrayIterate.allSatisfy(elements, new Predicate()
        {
            public boolean accept(Object each)
            {
                return AbstractLazyIterable.this.contains(each);
            }
        });
    }

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

    public boolean isEmpty()
    {
        return IterableIterate.isEmpty(this);
    }

    public boolean notEmpty()
    {
        return IterableIterate.notEmpty(this);
    }

    public T getFirst()
    {
        return IterableIterate.getFirst(this);
    }

    public T getLast()
    {
        return IterableIterate.getLast(this);
    }

    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 > R select(Predicate predicate, R target) { return IterableIterate.select(this, predicate, target); } public > R selectWith(Predicate2 predicate, P parameter, R targetCollection) { return IterableIterate.selectWith(this, predicate, parameter, targetCollection); } 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 > R reject(Predicate predicate, R target) { return IterableIterate.reject(this, predicate, target); } public > R rejectWith(Predicate2 predicate, P parameter, R targetCollection) { return IterableIterate.rejectWith(this, predicate, parameter, targetCollection); } public PartitionMutableList partition(Predicate predicate) { return IterableIterate.partition(this, predicate); } public

PartitionMutableList partitionWith(Predicate2 predicate, P parameter) { return IterableIterate.partitionWith(this, 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 R collectBoolean(BooleanFunction booleanFunction, R target) { return IterableIterate.collectBoolean(this, booleanFunction, target); } public LazyByteIterable collectByte(ByteFunction byteFunction) { return new CollectByteIterable(this, byteFunction); } public R collectByte(ByteFunction byteFunction, R target) { return IterableIterate.collectByte(this, byteFunction, target); } public LazyCharIterable collectChar(CharFunction charFunction) { return new CollectCharIterable(this, charFunction); } public R collectChar(CharFunction charFunction, R target) { return IterableIterate.collectChar(this, charFunction, target); } public LazyDoubleIterable collectDouble(DoubleFunction doubleFunction) { return new CollectDoubleIterable(this, doubleFunction); } public R collectDouble(DoubleFunction doubleFunction, R target) { return IterableIterate.collectDouble(this, doubleFunction, target); } public LazyFloatIterable collectFloat(FloatFunction floatFunction) { return new CollectFloatIterable(this, floatFunction); } public R collectFloat(FloatFunction floatFunction, R target) { return IterableIterate.collectFloat(this, floatFunction, target); } public LazyIntIterable collectInt(IntFunction intFunction) { return new CollectIntIterable(this, intFunction); } public R collectInt(IntFunction intFunction, R target) { return IterableIterate.collectInt(this, intFunction, target); } public LazyLongIterable collectLong(LongFunction longFunction) { return new CollectLongIterable(this, longFunction); } public R collectLong(LongFunction longFunction, R target) { return IterableIterate.collectLong(this, longFunction, target); } public LazyShortIterable collectShort(ShortFunction shortFunction) { return new CollectShortIterable(this, shortFunction); } public R collectShort(ShortFunction shortFunction, R target) { return IterableIterate.collectShort(this, shortFunction, target); } public > R collect(Function function, R target) { return IterableIterate.collect(this, function, target); } public LazyIterable collectWith(Function2 function, P parameter) { return LazyIterate.collect(this, Functions.bind(function, parameter)); } public > R collectWith(Function2 function, P parameter, R targetCollection) { return IterableIterate.collectWith(this, function, parameter, targetCollection); } public LazyIterable flatCollect(Function> function) { return LazyIterate.flatCollect(this, function); } public LazyIterable concatenate(Iterable iterable) { return LazyIterate.concatenate(this, iterable); } public > R flatCollect(Function> function, R target) { return IterableIterate.flatCollect(this, function, target); } public LazyIterable collectIf(Predicate predicate, Function function) { return LazyIterate.collectIf(this, predicate, function); } public > R collectIf(Predicate predicate, Function function, R target) { return IterableIterate.collectIf(this, predicate, function, target); } 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 T detect(Predicate predicate) { return IterableIterate.detect(this, predicate); } public

T detectWith(Predicate2 predicate, P parameter) { return IterableIterate.detectWith(this, predicate, parameter); } public T min(Comparator comparator) { return Iterate.min(this, comparator); } public T max(Comparator comparator) { return Iterate.max(this, comparator); } public T min() { return Iterate.min(this); } public T max() { return Iterate.max(this); } public > T minBy(Function function) { return IterableIterate.minBy(this, function); } public > T maxBy(Function function) { return IterableIterate.maxBy(this, function); } public T detectIfNone(Predicate predicate, Function0 function) { T result = this.detect(predicate); return result == null ? function.value() : result; } public

T detectWithIfNone(Predicate2 predicate, P parameter, Function0 function) { T result = this.detectWith(predicate, parameter); return result == null ? function.value() : result; } public int count(Predicate predicate) { CountProcedure procedure = new CountProcedure(predicate); this.forEach(procedure); return procedure.getCount(); } public

int countWith(Predicate2 predicate, P parameter) { return IterableIterate.countWith(this, predicate, parameter); } public boolean anySatisfy(Predicate predicate) { return IterableIterate.anySatisfy(this, predicate); } public boolean allSatisfy(Predicate predicate) { return IterableIterate.allSatisfy(this, predicate); } public boolean noneSatisfy(Predicate predicate) { return IterableIterate.noneSatisfy(this, predicate); } public

boolean anySatisfyWith(Predicate2 predicate, P parameter) { return IterableIterate.anySatisfyWith(this, predicate, parameter); } public

boolean allSatisfyWith(Predicate2 predicate, P parameter) { return IterableIterate.allSatisfyWith(this, predicate, parameter); } public

boolean noneSatisfyWith(Predicate2 predicate, P parameter) { return IterableIterate.noneSatisfyWith(this, predicate, parameter); } public IV injectInto(IV injectedValue, Function2 function) { return IterableIterate.injectInto(injectedValue, this, function); } public int injectInto(int injectedValue, IntObjectToIntFunction function) { return IterableIterate.injectInto(injectedValue, this, function); } public long injectInto(long injectedValue, LongObjectToLongFunction function) { return IterableIterate.injectInto(injectedValue, this, function); } public double injectInto(double injectedValue, DoubleObjectToDoubleFunction function) { return IterableIterate.injectInto(injectedValue, this, function); } public float injectInto(float injectedValue, FloatObjectToFloatFunction function) { return IterableIterate.injectInto(injectedValue, this, function); } public long sumOfInt(IntFunction function) { return IterableIterate.sumOfInt(this, function); } public double sumOfFloat(FloatFunction function) { return IterableIterate.sumOfFloat(this, function); } public long sumOfLong(LongFunction function) { return IterableIterate.sumOfLong(this, function); } public double sumOfDouble(DoubleFunction function) { return IterableIterate.sumOfDouble(this, function); } public MutableList toList() { MutableList list = Lists.mutable.of(); this.forEachWith(Procedures2.addToCollection(), list); return list; } public MutableList toSortedList() { return this.toList().sortThis(); } public MutableList toSortedList(Comparator comparator) { return this.toList().sortThis(comparator); } public > MutableList toSortedListBy(Function function) { return this.toSortedList(Comparators.byFunction(function)); } public MutableSortedSet toSortedSet() { MutableSortedSet treeSet = TreeSortedSet.newSet(); this.forEachWith(Procedures2.addToCollection(), treeSet); return treeSet; } public MutableSortedSet toSortedSet(Comparator comparator) { MutableSortedSet treeSet = TreeSortedSet.newSet(comparator); this.forEachWith(Procedures2.addToCollection(), treeSet); return treeSet; } public > MutableSortedSet toSortedSetBy(Function function) { return this.toSortedSet(Comparators.byFunction(function)); } public MutableSet toSet() { MutableSet set = UnifiedSet.newSet(); this.forEachWith(Procedures2.addToCollection(), set); return set; } public MutableBag toBag() { MutableBag bag = Bags.mutable.of(); this.forEachWith(Procedures2.addToCollection(), bag); return bag; } public MutableStack toStack() { return ArrayStack.newStack(this); } public MutableMap toMap( Function keyFunction, Function valueFunction) { UnifiedMap map = UnifiedMap.newMap(); this.forEach(new MapCollectProcedure(map, keyFunction, valueFunction)); return map; } public MutableSortedMap toSortedMap( Function keyFunction, Function valueFunction) { TreeSortedMap sortedMap = TreeSortedMap.newMap(); this.forEach(new MapCollectProcedure(sortedMap, keyFunction, valueFunction)); return sortedMap; } public MutableSortedMap toSortedMap(Comparator comparator, Function keyFunction, Function valueFunction) { TreeSortedMap sortedMap = TreeSortedMap.newMap(comparator); this.forEach(new MapCollectProcedure(sortedMap, keyFunction, valueFunction)); return sortedMap; } public String makeString() { return this.makeString(", "); } public String makeString(String separator) { return this.makeString("", separator, ""); } public String makeString(String start, String separator, String end) { Appendable stringBuilder = new StringBuilder(); this.appendString(stringBuilder, start, separator, end); return stringBuilder.toString(); } public void appendString(Appendable appendable) { this.appendString(appendable, ", "); } public void appendString(Appendable appendable, String separator) { this.appendString(appendable, "", separator, ""); } public void appendString(Appendable appendable, String start, String separator, String end) { IterableIterate.appendString(this, appendable, start, separator, end); } public ImmutableMultimap groupBy(Function function) { return IterableIterate.groupBy(this, function, FastListMultimap.newMultimap()).toImmutable(); } public > R groupBy( Function function, R target) { return IterableIterate.groupBy(this, function, target); } public ImmutableMultimap groupByEach(Function> function) { return IterableIterate.groupByEach(this, function, FastListMultimap.newMultimap()).toImmutable(); } public > R groupByEach( Function> function, R target) { return IterableIterate.groupByEach(this, function, target); } public MapIterable groupByUniqueKey(Function function) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".groupByUniqueKey() not implemented yet"); } public LazyIterable> zip(Iterable that) { return LazyIterate.zip(this, that); } public >> R zip(Iterable that, R target) { return IterableIterate.zip(this, that, target); } public LazyIterable> zipWithIndex() { return LazyIterate.zipWithIndex(this); } public >> R zipWithIndex(R target) { return IterableIterate.zipWithIndex(this, target); } public LazyIterable> chunk(int size) { return LazyIterate.chunk(this, size); } 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; } }