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

com.gs.collections.impl.utility.internal.RandomAccessListIterate 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.internal;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.RandomAccess;

import com.gs.collections.api.RichIterable;
import com.gs.collections.api.block.HashingStrategy;
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.Function3;
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.Procedure;
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.list.primitive.MutableBooleanList;
import com.gs.collections.api.list.primitive.MutableByteList;
import com.gs.collections.api.list.primitive.MutableCharList;
import com.gs.collections.api.list.primitive.MutableDoubleList;
import com.gs.collections.api.list.primitive.MutableFloatList;
import com.gs.collections.api.list.primitive.MutableIntList;
import com.gs.collections.api.list.primitive.MutableLongList;
import com.gs.collections.api.list.primitive.MutableShortList;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.map.primitive.ObjectDoubleMap;
import com.gs.collections.api.map.primitive.ObjectLongMap;
import com.gs.collections.api.multimap.MutableMultimap;
import com.gs.collections.api.ordered.OrderedIterable;
import com.gs.collections.api.partition.list.PartitionMutableList;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.api.tuple.Twin;
import com.gs.collections.impl.block.factory.Functions0;
import com.gs.collections.impl.block.factory.HashingStrategies;
import com.gs.collections.impl.block.procedure.MutatingAggregationProcedure;
import com.gs.collections.impl.block.procedure.NonMutatingAggregationProcedure;
import com.gs.collections.impl.factory.Lists;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.list.mutable.primitive.BooleanArrayList;
import com.gs.collections.impl.list.mutable.primitive.ByteArrayList;
import com.gs.collections.impl.list.mutable.primitive.CharArrayList;
import com.gs.collections.impl.list.mutable.primitive.DoubleArrayList;
import com.gs.collections.impl.list.mutable.primitive.FloatArrayList;
import com.gs.collections.impl.list.mutable.primitive.IntArrayList;
import com.gs.collections.impl.list.mutable.primitive.LongArrayList;
import com.gs.collections.impl.list.mutable.primitive.ShortArrayList;
import com.gs.collections.impl.map.mutable.UnifiedMap;
import com.gs.collections.impl.map.mutable.primitive.ObjectDoubleHashMap;
import com.gs.collections.impl.map.mutable.primitive.ObjectLongHashMap;
import com.gs.collections.impl.multimap.list.FastListMultimap;
import com.gs.collections.impl.partition.list.PartitionFastList;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.set.strategy.mutable.UnifiedSetWithHashingStrategy;
import com.gs.collections.impl.tuple.Tuples;
import com.gs.collections.impl.utility.Iterate;
import com.gs.collections.impl.utility.ListIterate;

/**
 * The ListIterate class provides a few of the methods from the Smalltalk Collection Protocol for use with ArrayLists.
 * This includes do:, select:, reject:, collect:, inject:into:, detect:, detect:ifNone:, anySatisfy: and allSatisfy:
 */
public final class RandomAccessListIterate
{
    private RandomAccessListIterate()
    {
        throw new AssertionError("Suppress default constructor for noninstantiability");
    }

    public static  void toArray(List list, T[] target, int startIndex, int sourceSize)
    {
        for (int i = 0; i < sourceSize; i++)
        {
            target[startIndex + i] = list.get(i);
        }
    }

    /**
     * @see Iterate#select(Iterable, Predicate)
     */
    public static  MutableList select(List list, Predicate predicate)
    {
        return RandomAccessListIterate.select(list, predicate, FastList.newList());
    }

    /**
     * @see Iterate#selectWith(Iterable, Predicate2, Object)
     */
    public static  MutableList selectWith(
            List list,
            Predicate2 predicate,
            IV injectedValue)
    {
        return RandomAccessListIterate.selectWith(list, predicate, injectedValue, FastList.newList());
    }

    /**
     * @see Iterate#select(Iterable, Predicate, Collection)
     */
    public static > R select(
            List list,
            Predicate predicate,
            R targetCollection)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            T item = list.get(i);
            if (predicate.accept(item))
            {
                targetCollection.add(item);
            }
        }
        return targetCollection;
    }

    /**
     * @see Iterate#selectWith(Iterable, Predicate2, Object, Collection)
     */
    public static > R selectWith(
            List list,
            Predicate2 predicate,
            P parameter,
            R targetCollection)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            T item = list.get(i);
            if (predicate.accept(item, parameter))
            {
                targetCollection.add(item);
            }
        }
        return targetCollection;
    }

    /**
     * @see Iterate#selectInstancesOf(Iterable, Class)
     */
    public static  MutableList selectInstancesOf(
            List list,
            Class clazz)
    {
        int size = list.size();
        FastList result = FastList.newList(size);

        for (int i = 0; i < size; i++)
        {
            Object item = list.get(i);
            if (clazz.isInstance(item))
            {
                result.add((T) item);
            }
        }
        result.trimToSize();
        return result;
    }

    /**
     * @see Iterate#count(Iterable, Predicate)
     */
    public static  int count(
            List list,
            Predicate predicate)
    {
        int count = 0;
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            if (predicate.accept(list.get(i)))
            {
                count++;
            }
        }
        return count;
    }

    public static  int countWith(
            List list,
            Predicate2 predicate,
            IV injectedValue)
    {
        int count = 0;
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            if (predicate.accept(list.get(i), injectedValue))
            {
                count++;
            }
        }
        return count;
    }

    /**
     * @see Iterate#collectIf(Iterable, Predicate, Function)
     */
    public static  MutableList collectIf(
            List list,
            Predicate predicate,
            Function function)
    {
        return RandomAccessListIterate.collectIf(list, predicate, function, FastList.newList());
    }

    /**
     * @see Iterate#collectIf(Iterable, Predicate, Function, Collection)
     */
    public static > R collectIf(
            List list,
            Predicate predicate,
            Function function,
            R targetCollection)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            T item = list.get(i);
            if (predicate.accept(item))
            {
                targetCollection.add(function.valueOf(item));
            }
        }
        return targetCollection;
    }

    /**
     * @see Iterate#reject(Iterable, Predicate)
     */
    public static  MutableList reject(List list, Predicate predicate)
    {
        return RandomAccessListIterate.reject(list, predicate, FastList.newList());
    }

    /**
     * @see Iterate#rejectWith(Iterable, Predicate2, Object)
     */
    public static  MutableList rejectWith(
            List list,
            Predicate2 predicate,
            IV injectedValue)
    {
        return RandomAccessListIterate.rejectWith(list, predicate, injectedValue, FastList.newList());
    }

    /**
     * @see Iterate#reject(Iterable, Predicate, Collection)
     */
    public static > R reject(
            List list,
            Predicate predicate,
            R targetCollection)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            T item = list.get(i);
            if (!predicate.accept(item))
            {
                targetCollection.add(item);
            }
        }
        return targetCollection;
    }

    /**
     * @see Iterate#reject(Iterable, Predicate, Collection)
     */
    public static > R rejectWith(
            List list,
            Predicate2 predicate,
            P parameter,
            R targetCollection)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            T item = list.get(i);
            if (!predicate.accept(item, parameter))
            {
                targetCollection.add(item);
            }
        }
        return targetCollection;
    }

    /**
     * @see Iterate#collect(Iterable, Function)
     */
    public static  MutableList collect(
            List list,
            Function function)
    {
        return RandomAccessListIterate.collect(list, function, FastList.newList(list.size()));
    }

    /**
     * @see Iterate#collect(Iterable, Function, Collection)
     */
    public static > R collect(
            List list,
            Function function,
            R targetCollection)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            targetCollection.add(function.valueOf(list.get(i)));
        }
        return targetCollection;
    }

    /**
     * @see Iterate#collectBoolean(Iterable, BooleanFunction)
     */
    public static  MutableBooleanList collectBoolean(
            List list,
            BooleanFunction booleanFunction)
    {
        return RandomAccessListIterate.collectBoolean(list, booleanFunction, new BooleanArrayList(list.size()));
    }

    /**
     * @see Iterate#collectBoolean(Iterable, BooleanFunction)
     */
    public static  R collectBoolean(
            List list,
            BooleanFunction booleanFunction,
            R target)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            target.add(booleanFunction.booleanValueOf(list.get(i)));
        }
        return target;
    }

    /**
     * @see Iterate#collectByte(Iterable, ByteFunction)
     */
    public static  MutableByteList collectByte(
            List list,
            ByteFunction byteFunction)
    {
        return RandomAccessListIterate.collectByte(list, byteFunction, new ByteArrayList(list.size()));
    }

    /**
     * @see Iterate#collectByte(Iterable, ByteFunction)
     */
    public static  R collectByte(
            List list,
            ByteFunction byteFunction,
            R target)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            target.add(byteFunction.byteValueOf(list.get(i)));
        }
        return target;
    }

    /**
     * @see Iterate#collectChar(Iterable, CharFunction)
     */
    public static  MutableCharList collectChar(
            List list,
            CharFunction charFunction)
    {
        return RandomAccessListIterate.collectChar(list, charFunction, new CharArrayList(list.size()));
    }

    /**
     * @see Iterate#collectChar(Iterable, CharFunction)
     */
    public static  R collectChar(
            List list,
            CharFunction charFunction,
            R target)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            target.add(charFunction.charValueOf(list.get(i)));
        }
        return target;
    }

    /**
     * @see Iterate#collectDouble(Iterable, DoubleFunction)
     */
    public static  MutableDoubleList collectDouble(
            List list,
            DoubleFunction doubleFunction)
    {
        return RandomAccessListIterate.collectDouble(list, doubleFunction, new DoubleArrayList(list.size()));
    }

    /**
     * @see Iterate#collectDouble(Iterable, DoubleFunction)
     */
    public static  R collectDouble(
            List list,
            DoubleFunction doubleFunction,
            R target)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            target.add(doubleFunction.doubleValueOf(list.get(i)));
        }
        return target;
    }

    /**
     * @see Iterate#collectFloat(Iterable, FloatFunction)
     */
    public static  MutableFloatList collectFloat(
            List list,
            FloatFunction floatFunction)
    {
        return RandomAccessListIterate.collectFloat(list, floatFunction, new FloatArrayList(list.size()));
    }

    /**
     * @see Iterate#collectFloat(Iterable, FloatFunction)
     */
    public static  R collectFloat(
            List list,
            FloatFunction floatFunction,
            R target)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            target.add(floatFunction.floatValueOf(list.get(i)));
        }
        return target;
    }

    /**
     * @see Iterate#collectInt(Iterable, IntFunction)
     */
    public static  MutableIntList collectInt(
            List list,
            IntFunction intFunction)
    {
        return RandomAccessListIterate.collectInt(list, intFunction, new IntArrayList(list.size()));
    }

    /**
     * @see Iterate#collectInt(Iterable, IntFunction)
     */
    public static  R collectInt(
            List list,
            IntFunction intFunction,
            R target)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            target.add(intFunction.intValueOf(list.get(i)));
        }
        return target;
    }

    /**
     * @see Iterate#collectLong(Iterable, LongFunction)
     */
    public static  MutableLongList collectLong(
            List list,
            LongFunction longFunction)
    {
        return RandomAccessListIterate.collectLong(list, longFunction, new LongArrayList(list.size()));
    }

    /**
     * @see Iterate#collectLong(Iterable, LongFunction)
     */
    public static  R collectLong(
            List list,
            LongFunction longFunction,
            R target)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            target.add(longFunction.longValueOf(list.get(i)));
        }
        return target;
    }

    /**
     * @see Iterate#collectShort(Iterable, ShortFunction)
     */
    public static  MutableShortList collectShort(
            List list,
            ShortFunction shortFunction)
    {
        return RandomAccessListIterate.collectShort(list, shortFunction, new ShortArrayList(list.size()));
    }

    /**
     * @see Iterate#collectShort(Iterable, ShortFunction)
     */
    public static  R collectShort(
            List list,
            ShortFunction shortFunction,
            R target)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            target.add(shortFunction.shortValueOf(list.get(i)));
        }
        return target;
    }

    /**
     * @see Iterate#flatCollect(Iterable, Function)
     */
    public static  MutableList flatCollect(
            List list,
            Function> function)
    {
        return RandomAccessListIterate.flatCollect(list, function, FastList.newList(list.size()));
    }

    /**
     * @see Iterate#flatCollect(Iterable, Function, Collection)
     */
    public static > R flatCollect(
            List list,
            Function> function,
            R targetCollection)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            Iterate.addAllTo(function.valueOf(list.get(i)), targetCollection);
        }
        return targetCollection;
    }

    /**
     * Returns the last element of a list.
     */
    public static  T getLast(List collection)
    {
        return Iterate.isEmpty(collection) ? null : collection.get(collection.size() - 1);
    }

    /**
     * @see Iterate#forEach(Iterable, Procedure)
     */
    public static  void forEach(List list, Procedure procedure)
    {
        int size = list.size();
        for (int i = 0; i < size; i++)
        {
            procedure.value(list.get(i));
        }
    }

    /**
     * Iterates over the section of the list covered by the specified indexes.  The indexes are both inclusive.  If the
     * from is less than the to, the list is iterated in forward order. If the from is greater than the to, then the
     * list is iterated in the reverse order.
     * 

*

e.g.
     * MutableList people = FastList.newListWith(ted, mary, bob, sally);
     * ListIterate.forEach(people, 0, 1, new Procedure()
     * {
     *     public void value(Person person)
     *     {
     *          LOGGER.info(person.getName());
     *     }
     * });
     * 
*

* This code would output ted and mary's names. */ public static void forEach(List list, int from, int to, Procedure procedure) { ListIterate.rangeCheck(from, to, list.size()); if (from <= to) { for (int i = from; i <= to; i++) { procedure.value(list.get(i)); } } else { for (int i = from; i >= to; i--) { procedure.value(list.get(i)); } } } /** * Iterates over the section of the list covered by the specified indexes. The indexes are both inclusive. If the * from is less than the to, the list is iterated in forward order. If the from is greater than the to, then the * list is iterated in the reverse order. The index passed into the ObjectIntProcedure is the actual index of the * range. *

*

e.g.
     * MutableList people = FastList.newListWith(ted, mary, bob, sally);
     * ListIterate.forEachWithIndex(people, 0, 1, new ObjectIntProcedure()
     * {
     *     public void value(Person person, int index)
     *     {
     *          LOGGER.info(person.getName() + " at index: " + index);
     *     }
     * });
     * 
*

* This code would output ted and mary's names. */ public static void forEachWithIndex(List list, int from, int to, ObjectIntProcedure objectIntProcedure) { ListIterate.rangeCheck(from, to, list.size()); if (from <= to) { for (int i = from; i <= to; i++) { objectIntProcedure.value(list.get(i), i); } } else { for (int i = from; i >= to; i--) { objectIntProcedure.value(list.get(i), i); } } } /** * For each element in both of the Lists, operation is evaluated with both elements as parameters. */ public static void forEachInBoth(List list1, List list2, Procedure2 procedure) { if (list1 != null && list2 != null) { int size1 = list1.size(); int size2 = list2.size(); if (size1 == size2) { for (int i = 0; i < size1; i++) { procedure.value(list1.get(i), list2.get(i)); } } else { throw new IllegalArgumentException("Attempt to call forEachInBoth with two Lists of different sizes :" + size1 + ':' + size2); } } } /** * Iterates over a collection passing each element and the current relative int index to the specified instance of * ObjectIntProcedure. */ public static void forEachWithIndex(List list, ObjectIntProcedure objectIntProcedure) { int size = list.size(); for (int i = 0; i < size; i++) { objectIntProcedure.value(list.get(i), i); } } public static IV injectInto(IV injectValue, List list, Function2 function) { IV result = injectValue; int size = list.size(); for (int i = 0; i < size; i++) { result = function.value(result, list.get(i)); } return result; } public static int injectInto(int injectValue, List list, IntObjectToIntFunction function) { int result = injectValue; int size = list.size(); for (int i = 0; i < size; i++) { result = function.intValueOf(result, list.get(i)); } return result; } public static long injectInto(long injectValue, List list, LongObjectToLongFunction function) { long result = injectValue; int size = list.size(); for (int i = 0; i < size; i++) { result = function.longValueOf(result, list.get(i)); } return result; } public static double injectInto(double injectValue, List list, DoubleObjectToDoubleFunction function) { double result = injectValue; int size = list.size(); for (int i = 0; i < size; i++) { result = function.doubleValueOf(result, list.get(i)); } return result; } public static float injectInto(float injectValue, List list, FloatObjectToFloatFunction function) { float result = injectValue; int size = list.size(); for (int i = 0; i < size; i++) { result = function.floatValueOf(result, list.get(i)); } return result; } public static long sumOfInt(List list, IntFunction function) { long result = 0; for (int i = 0; i < list.size(); i++) { result += (long) function.intValueOf(list.get(i)); } return result; } public static long sumOfLong(List list, LongFunction function) { long result = 0L; for (int i = 0; i < list.size(); i++) { result += function.longValueOf(list.get(i)); } return result; } public static double sumOfFloat(List list, FloatFunction function) { double sum = 0.0d; double compensation = 0.0d; for (int i = 0; i < list.size(); i++) { double adjustedValue = (double) function.floatValueOf(list.get(i)) - compensation; double nextSum = sum + adjustedValue; compensation = nextSum - sum - adjustedValue; sum = nextSum; } return sum; } public static double sumOfDouble(List list, DoubleFunction function) { double sum = 0.0d; double compensation = 0.0d; for (int i = 0; i < list.size(); i++) { double adjustedValue = function.doubleValueOf(list.get(i)) - compensation; double nextSum = sum + adjustedValue; compensation = nextSum - sum - adjustedValue; sum = nextSum; } return sum; } public static BigDecimal sumOfBigDecimal(List list, Function function) { BigDecimal result = BigDecimal.ZERO; int size = list.size(); for (int i = 0; i < size; i++) { result = result.add(function.valueOf(list.get(i))); } return result; } public static BigInteger sumOfBigInteger(List list, Function function) { BigInteger result = BigInteger.ZERO; int size = list.size(); for (int i = 0; i < size; i++) { result = result.add(function.valueOf(list.get(i))); } return result; } public static MutableMap sumByBigDecimal(List list, Function groupBy, final Function function) { MutableMap result = UnifiedMap.newMap(); int size = list.size(); for (int i = 0; i < size; i++) { final T item = list.get(i); result.updateValue(groupBy.valueOf(item), Functions0.zeroBigDecimal(), new Function() { public BigDecimal valueOf(BigDecimal original) { return original.add(function.valueOf(item)); } }); } return result; } public static MutableMap sumByBigInteger(List list, Function groupBy, final Function function) { MutableMap result = UnifiedMap.newMap(); int size = list.size(); for (int i = 0; i < size; i++) { final T item = list.get(i); result.updateValue(groupBy.valueOf(item), Functions0.zeroBigInteger(), new Function() { public BigInteger valueOf(BigInteger original) { return original.add(function.valueOf(item)); } }); } return result; } public static boolean shortCircuit( List list, Predicate predicate, boolean expected, boolean onShortCircuit, boolean atEnd) { int size = list.size(); for (int i = 0; i < size; i++) { if (predicate.accept(list.get(i)) == expected) { return onShortCircuit; } } return atEnd; } public static boolean shortCircuitWith( List list, Predicate2 predicate2, P parameter, boolean expected, boolean onShortCircuit, boolean atEnd) { int size = list.size(); for (int i = 0; i < size; i++) { if (predicate2.accept(list.get(i), parameter) == expected) { return onShortCircuit; } } return atEnd; } public static boolean corresponds(List list, OrderedIterable

other, Predicate2 predicate) { if (!(list instanceof RandomAccess)) { throw new IllegalArgumentException(); } int size = list.size(); if (size != other.size()) { return false; } if (other instanceof RandomAccess) { List

otherList = (List

) other; for (int index = 0; index < size; index++) { if (!predicate.accept(list.get(index), otherList.get(index))) { return false; } } return true; } Iterator

iterator = other.iterator(); for (int index = 0; index < size; index++) { if (!predicate.accept(list.get(index), iterator.next())) { return false; } } return true; } public static boolean anySatisfy(List list, Predicate predicate) { return RandomAccessListIterate.shortCircuit(list, predicate, true, true, false); } public static boolean anySatisfyWith(List list, Predicate2 predicate, P parameter) { return RandomAccessListIterate.shortCircuitWith(list, predicate, parameter, true, true, false); } public static boolean allSatisfy(List list, Predicate predicate) { return RandomAccessListIterate.shortCircuit(list, predicate, false, false, true); } public static boolean allSatisfyWith(List list, Predicate2 predicate, P parameter) { return RandomAccessListIterate.shortCircuitWith(list, predicate, parameter, false, false, true); } public static boolean noneSatisfy(List list, Predicate predicate) { return RandomAccessListIterate.shortCircuit(list, predicate, true, false, true); } public static boolean noneSatisfyWith(List list, Predicate2 predicate, P parameter) { return RandomAccessListIterate.shortCircuitWith(list, predicate, parameter, true, false, true); } public static T detect(List list, Predicate predicate) { int size = list.size(); for (int i = 0; i < size; i++) { T each = list.get(i); if (predicate.accept(each)) { return each; } } return null; } public static T detectWith(List list, Predicate2 predicate, P parameter) { int size = list.size(); for (int i = 0; i < size; i++) { T each = list.get(i); if (predicate.accept(each, parameter)) { return each; } } return null; } public static Twin> selectAndRejectWith( List list, Predicate2 predicate, IV injectedValue) { MutableList positiveResult = Lists.mutable.empty(); MutableList negativeResult = Lists.mutable.empty(); int size = list.size(); for (int i = 0; i < size; i++) { T item = list.get(i); (predicate.accept(item, injectedValue) ? positiveResult : negativeResult).add(item); } return Tuples.twin(positiveResult, negativeResult); } public static PartitionMutableList partition(List list, Predicate predicate) { PartitionFastList partitionFastList = new PartitionFastList(); int size = list.size(); for (int i = 0; i < size; i++) { T each = list.get(i); MutableList bucket = predicate.accept(each) ? partitionFastList.getSelected() : partitionFastList.getRejected(); bucket.add(each); } return partitionFastList; } public static PartitionMutableList partitionWith(List list, Predicate2 predicate, P parameter) { PartitionFastList partitionFastList = new PartitionFastList(); int size = list.size(); for (int i = 0; i < size; i++) { T each = list.get(i); MutableList bucket = predicate.accept(each, parameter) ? partitionFastList.getSelected() : partitionFastList.getRejected(); bucket.add(each); } return partitionFastList; } public static boolean removeIf(List list, Predicate predicate) { boolean changed = false; for (int i = 0; i < list.size(); i++) { T each = list.get(i); if (predicate.accept(each)) { list.remove(i--); changed = true; } } return changed; } public static boolean removeIfWith(List list, Predicate2 predicate, P parameter) { boolean changed = false; for (int i = 0; i < list.size(); i++) { T each = list.get(i); if (predicate.accept(each, parameter)) { list.remove(i--); changed = true; } } return changed; } public static boolean removeIf(List list, Predicate predicate, Procedure procedure) { boolean changed = false; for (int i = 0; i < list.size(); i++) { T each = list.get(i); if (predicate.accept(each)) { procedure.value(each); list.remove(i--); changed = true; } } return changed; } public static boolean removeIfWith(List list, Predicate2 predicate, P parameter, Procedure procedure) { boolean changed = false; for (int i = 0; i < list.size(); i++) { T each = list.get(i); if (predicate.accept(each, parameter)) { procedure.value(each); list.remove(i--); changed = true; } } return changed; } /** * Searches for the first occurrence where the predicate evaluates to true. * * @see Iterate#detectIndex(Iterable, Predicate) */ public static int detectIndex(List list, Predicate predicate) { int size = list.size(); for (int i = 0; i < size; i++) { if (predicate.accept(list.get(i))) { return i; } } return -1; } /** * Searches for the first occurrence where the predicate evaluates to true. * * @see Iterate#detectIndexWith(Iterable, Predicate2, Object) */ public static int detectIndexWith(List list, Predicate2 predicate, IV injectedValue) { int size = list.size(); for (int i = 0; i < size; i++) { if (predicate.accept(list.get(i), injectedValue)) { return i; } } return -1; } public static int detectLastIndex(List list, Predicate predicate) { int size = list.size(); for (int i = size - 1; i >= 0; i--) { if (predicate.accept(list.get(i))) { return i; } } return -1; } public static IV injectIntoWith( IV injectedValue, List list, Function3 function, P parameter) { IV result = injectedValue; int size = list.size(); for (int i = 0; i < size; i++) { result = function.value(result, list.get(i), parameter); } return result; } public static void forEachWith(List list, Procedure2 procedure, P parameter) { int size = list.size(); for (int i = 0; i < size; i++) { procedure.value(list.get(i), parameter); } } public static > R collectWith( List list, Function2 function, P parameter, R targetCollection) { int size = list.size(); for (int i = 0; i < size; i++) { targetCollection.add(function.value(list.get(i), parameter)); } return targetCollection; } /** * @deprecated in 7.0. */ @Deprecated public static > R distinct(List list, R targetList) { MutableSet seenSoFar = UnifiedSet.newSet(); int size = list.size(); for (int i = 0; i < size; i++) { T item = list.get(i); if (seenSoFar.add(item)) { targetList.add(item); } } return targetList; } /** * @since 7.0. */ public static MutableList distinct(List list) { return RandomAccessListIterate.distinct(list, FastList.newList()); } /** * @since 7.0. */ public static MutableList distinct(List list, HashingStrategy hashingStrategy) { MutableSet seenSoFar = UnifiedSetWithHashingStrategy.newSet(hashingStrategy); FastList result = FastList.newList(); int size = list.size(); for (int i = 0; i < size; i++) { T item = list.get(i); if (seenSoFar.add(item)) { result.add(item); } } return result; } /** * @see Iterate#take(Iterable, int) */ public static MutableList take(List list, int count) { if (count < 0) { throw new IllegalArgumentException("Count must be greater than zero, but was: " + count); } return RandomAccessListIterate.take(list, count, FastList.newList(Math.min(list.size(), count))); } /** * @see Iterate#take(Iterable, int) */ public static > R take(List list, int count, R targetList) { if (count < 0) { throw new IllegalArgumentException("Count must be greater than zero, but was: " + count); } int end = Math.min(list.size(), count); for (int i = 0; i < end; i++) { targetList.add(list.get(i)); } return targetList; } /** * @see Iterate#drop(Iterable, int) */ public static MutableList drop(List list, int count) { if (count < 0) { throw new IllegalArgumentException("Count must be greater than zero, but was: " + count); } return RandomAccessListIterate.drop(list, count, FastList.newList(list.size() - Math.min(list.size(), count))); } /** * @see Iterate#drop(Iterable, int) */ public static > R drop(List list, int count, R targetList) { if (count < 0) { throw new IllegalArgumentException("Count must be greater than zero, but was: " + count); } if (count >= list.size()) { return targetList; } int start = Math.min(list.size(), count); targetList.addAll(list.subList(start, list.size())); return targetList; } /** * @see RichIterable#appendString(Appendable, String, String, String) */ public static void appendString( List list, Appendable appendable, String start, String separator, String end) { try { appendable.append(start); if (Iterate.notEmpty(list)) { appendable.append(IterableIterate.stringValueOfItem(list, list.get(0))); int size = list.size(); for (int i = 1; i < size; i++) { appendable.append(separator); appendable.append(IterableIterate.stringValueOfItem(list, list.get(i))); } } appendable.append(end); } catch (IOException e) { throw new RuntimeException(e); } } /** * @see Iterate#groupBy(Iterable, Function) */ public static FastListMultimap groupBy( List list, Function function) { return RandomAccessListIterate.groupBy(list, function, FastListMultimap.newMultimap()); } /** * @see Iterate#groupBy(Iterable, Function, MutableMultimap) */ public static > R groupBy( List list, Function function, R target) { int size = list.size(); for (int i = 0; i < size; i++) { T item = list.get(i); target.put(function.valueOf(item), item); } return target; } public static FastListMultimap groupByEach( List list, Function> function) { return RandomAccessListIterate.groupByEach(list, function, FastListMultimap.newMultimap()); } public static > R groupByEach( List list, Function> function, R target) { int size = list.size(); for (int i = 0; i < size; i++) { T item = list.get(i); Iterable iterable = function.valueOf(item); for (V key : iterable) { target.put(key, item); } } return target; } public static MutableMap groupByUniqueKey( List list, Function function) { return RandomAccessListIterate.groupByUniqueKey(list, function, UnifiedMap.newMap()); } public static > R groupByUniqueKey( List list, Function function, R target) { int size = list.size(); for (int i = 0; i < size; i++) { T value = list.get(i); K key = function.valueOf(value); if (target.put(key, value) != null) { throw new IllegalStateException("Key " + key + " already exists in map!"); } } return target; } public static > T minBy(List list, Function function) { if (list.isEmpty()) { throw new NoSuchElementException(); } T min = list.get(0); V minValue = function.valueOf(min); int size = list.size(); for (int i = 1; i < size; i++) { T next = list.get(i); V nextValue = function.valueOf(next); if (nextValue.compareTo(minValue) < 0) { min = next; minValue = nextValue; } } return min; } public static > T maxBy(List list, Function function) { if (list.isEmpty()) { throw new NoSuchElementException(); } T max = list.get(0); V maxValue = function.valueOf(max); int size = list.size(); for (int i = 1; i < size; i++) { T next = list.get(i); V nextValue = function.valueOf(next); if (nextValue.compareTo(maxValue) > 0) { max = next; maxValue = nextValue; } } return max; } public static T min(List list, Comparator comparator) { if (list.isEmpty()) { throw new NoSuchElementException(); } T min = list.get(0); int size = list.size(); for (int i = 1; i < size; i++) { T item = list.get(i); if (comparator.compare(item, min) < 0) { min = item; } } return min; } public static T max(List list, Comparator comparator) { if (list.isEmpty()) { throw new NoSuchElementException(); } T max = list.get(0); int size = list.size(); for (int i = 1; i < size; i++) { T item = list.get(i); if (comparator.compare(item, max) > 0) { max = item; } } return max; } public static T min(List list) { if (list.isEmpty()) { throw new NoSuchElementException(); } T min = list.get(0); int size = list.size(); for (int i = 1; i < size; i++) { T item = list.get(i); if (((Comparable) item).compareTo(min) < 0) { min = item; } } return min; } public static T max(List list) { if (list.isEmpty()) { throw new NoSuchElementException(); } T max = list.get(0); int size = list.size(); for (int i = 1; i < size; i++) { T item = list.get(i); if (((Comparable) item).compareTo(max) > 0) { max = item; } } return max; } public static MutableList> zip( List list, Iterable iterable) { return RandomAccessListIterate.zip(list, iterable, FastList.>newList()); } public static >> R zip( List list, Iterable iterable, R target) { Iterator yIterator = iterable.iterator(); int size = list.size(); for (int i = 0; i < size && yIterator.hasNext(); i++) { target.add(Tuples.pair(list.get(i), yIterator.next())); } return target; } public static MutableList> zipWithIndex(List list) { return RandomAccessListIterate.zipWithIndex(list, FastList.>newList()); } public static >> R zipWithIndex( List list, R target) { int size = list.size(); for (int i = 0; i < size; i++) { target.add(Tuples.pair(list.get(i), i)); } return target; } public static MutableMap aggregateInPlaceBy( List list, Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { MutableMap map = UnifiedMap.newMap(); RandomAccessListIterate.forEach(list, new MutatingAggregationProcedure(map, groupBy, zeroValueFactory, mutatingAggregator)); return map; } public static MutableMap aggregateBy( List list, Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { MutableMap map = UnifiedMap.newMap(); RandomAccessListIterate.forEach(list, new NonMutatingAggregationProcedure(map, groupBy, zeroValueFactory, nonMutatingAggregator)); return map; } public static MutableList takeWhile(List list, Predicate predicate) { MutableList result = FastList.newList(); int size = list.size(); for (int i = 0; i < size; i++) { T each = list.get(i); if (predicate.accept(each)) { result.add(each); } else { return result; } } return result; } public static MutableList dropWhile(List list, Predicate predicate) { MutableList result = FastList.newList(); int size = list.size(); for (int i = 0; i < size; i++) { T each = list.get(i); if (!predicate.accept(each)) { result.add(each); for (int j = i + 1; j < size; j++) { T eachNotDropped = list.get(j); result.add(eachNotDropped); } return result; } } return result; } public static PartitionMutableList partitionWhile(List list, Predicate predicate) { PartitionMutableList result = new PartitionFastList(); MutableList selected = result.getSelected(); int size = list.size(); for (int i = 0; i < size; i++) { T each = list.get(i); if (predicate.accept(each)) { selected.add(each); } else { MutableList rejected = result.getRejected(); rejected.add(each); for (int j = i + 1; j < size; j++) { T eachRejected = list.get(j); rejected.add(eachRejected); } return result; } } return result; } public static ObjectLongMap sumByInt(List list, Function groupBy, IntFunction function) { ObjectLongHashMap result = ObjectLongHashMap.newMap(); for (int i = 0; i < list.size(); i++) { T item = list.get(i); result.addToValue(groupBy.valueOf(item), function.intValueOf(item)); } return result; } public static ObjectLongMap sumByLong(List list, Function groupBy, LongFunction function) { ObjectLongHashMap result = ObjectLongHashMap.newMap(); for (int i = 0; i < list.size(); i++) { T item = list.get(i); result.addToValue(groupBy.valueOf(item), function.longValueOf(item)); } return result; } public static ObjectDoubleMap sumByFloat(List list, Function groupBy, FloatFunction function) { ObjectDoubleHashMap result = ObjectDoubleHashMap.newMap(); for (int i = 0; i < list.size(); i++) { T item = list.get(i); result.addToValue(groupBy.valueOf(item), function.floatValueOf(item)); } return result; } public static ObjectDoubleMap sumByDouble(List list, Function groupBy, DoubleFunction function) { ObjectDoubleHashMap result = ObjectDoubleHashMap.newMap(); for (int i = 0; i < list.size(); i++) { T item = list.get(i); result.addToValue(groupBy.valueOf(item), function.doubleValueOf(item)); } return result; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy