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

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

import java.util.Collection;

import com.gs.collections.api.block.function.primitive.IntToObjectFunction;
import com.gs.collections.api.block.function.primitive.ObjectIntToObjectFunction;
import com.gs.collections.api.block.predicate.primitive.IntPredicate;
import com.gs.collections.api.block.procedure.primitive.IntProcedure;
import com.gs.collections.api.collection.primitive.MutableIntCollection;
import com.gs.collections.api.iterator.IntIterator;

/**
 * The IntIteratorIterate class provides implementations of the various iteration patterns for use with the {@link IntIterator}.
 * This file was automatically generated from template file primitiveIteratorIterate.stg.
 *
 * @since 5.0
 */
public final class IntIteratorIterate
{
    private IntIteratorIterate()
    {
        throw new AssertionError("Suppress default constructor for noninstantiability");
    }

    public static void forEach(IntIterator iterator, IntProcedure procedure)
    {
        while (iterator.hasNext())
        {
            procedure.value(iterator.next());
        }
    }

    public static  R select(
            IntIterator iterator,
            IntPredicate predicate,
            R targetCollection)
    {
        while (iterator.hasNext())
        {
            int item = iterator.next();
            if (predicate.accept(item))
            {
                targetCollection.add(item);
            }
        }
        return targetCollection;
    }

    public static  R reject(
            IntIterator iterator,
            IntPredicate predicate,
            R targetCollection)
    {
        while (iterator.hasNext())
        {
            int item = iterator.next();
            if (!predicate.accept(item))
            {
                targetCollection.add(item);
            }
        }
        return targetCollection;
    }

    public static > R collect(
            IntIterator iterator,
            IntToObjectFunction function,
            R targetCollection)
    {
        while (iterator.hasNext())
        {
            int item = iterator.next();
            targetCollection.add(function.valueOf(item));
        }
        return targetCollection;
    }

    public static int detectIfNone(IntIterator iterator, IntPredicate predicate, int ifNone)
    {
        while (iterator.hasNext())
        {
            int item = iterator.next();
            if (predicate.accept(item))
            {
                return item;
            }
        }
        return ifNone;
    }

    public static int count(IntIterator iterator, IntPredicate predicate)
    {
        int count = 0;
        while (iterator.hasNext())
        {
            if (predicate.accept(iterator.next()))
            {
                count++;
            }
        }
        return count;
    }

    public static boolean anySatisfy(IntIterator iterator, IntPredicate predicate)
    {
        while (iterator.hasNext())
        {
            if (predicate.accept(iterator.next()))
            {
                return true;
            }
        }
        return false;
    }

    public static boolean allSatisfy(IntIterator iterator, IntPredicate predicate)
    {
        while (iterator.hasNext())
        {
            if (!predicate.accept(iterator.next()))
            {
                return false;
            }
        }
        return true;
    }

    public static boolean noneSatisfy(IntIterator iterator, IntPredicate predicate)
    {
        while (iterator.hasNext())
        {
            if (predicate.accept(iterator.next()))
            {
                return false;
            }
        }
        return true;
    }

    public static  T injectInto(IntIterator iterator, T injectedValue, ObjectIntToObjectFunction function)
    {
        T result = injectedValue;
        while(iterator.hasNext())
        {
            result = function.valueOf(result, iterator.next());
        }
        return result;
    }

    public static long sum(IntIterator iterator)
    {
        long sum = 0L;
        while (iterator.hasNext())
        {
            sum += iterator.next();
        }
        return sum;
    }

    public static int max(IntIterator iterator)
    {
        int max = iterator.next();
        while (iterator.hasNext())
        {
            int next = iterator.next();
            if (max < next)
            {
                max = next;
            }
        }
        return max;
    }

    public static int min(IntIterator iterator)
    {
        int min = iterator.next();
        while (iterator.hasNext())
        {
            int next = iterator.next();
            if (next < min)
            {
                min = next;
            }
        }
        return min;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy