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

com.gs.collections.impl.lazy.CollectIterable 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 2013 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.util.Iterator;

import com.gs.collections.api.block.function.Function;
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.impl.block.factory.Functions;
import com.gs.collections.impl.lazy.iterator.CollectIterator;
import com.gs.collections.impl.utility.Iterate;
import net.jcip.annotations.Immutable;

/**
 * A CollectIterable is an iterable that transforms a source iterable using a function as it iterates.
 */
@Immutable
public class CollectIterable
        extends AbstractLazyIterable
{
    private final Iterable adapted;
    private final Function function;

    public CollectIterable(Iterable newAdapted, Function function)
    {
        this.adapted = newAdapted;
        this.function = function;
    }

    public void forEach(Procedure procedure)
    {
        Iterate.forEach(this.adapted, Functions.bind(procedure, this.function));
    }

    public void forEachWithIndex(ObjectIntProcedure objectIntProcedure)
    {
        Iterate.forEachWithIndex(this.adapted, Functions.bind(objectIntProcedure, this.function));
    }

    public 

void forEachWith(Procedure2 procedure, P parameter) { Iterate.forEachWith(this.adapted, Functions.bind(procedure, this.function), parameter); } public Iterator iterator() { return new CollectIterator(this.adapted, this.function); } @Override public int size() { return Iterate.sizeOf(this.adapted); } @Override public boolean isEmpty() { return Iterate.isEmpty(this.adapted); } @Override public boolean notEmpty() { return !this.isEmpty(); } @Override public Object[] toArray() { Object[] array = Iterate.toArray(this.adapted); for (int i = 0; i < array.length; i++) { array[i] = this.function.valueOf((T) array[i]); } return array; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy