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

com.gs.collections.impl.lazy.CompositeIterable 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.lazy;

import java.util.Iterator;
import java.util.NoSuchElementException;

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.list.MutableList;
import com.gs.collections.impl.Counter;
import com.gs.collections.impl.EmptyIterator;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.utility.Iterate;
import net.jcip.annotations.Immutable;

@Immutable
public final class CompositeIterable
        extends AbstractLazyIterable
{
    private final MutableList> iterables;

    private CompositeIterable(MutableList> newIterables)
    {
        this.iterables = newIterables;
    }

    public CompositeIterable()
    {
        this(FastList.>newList());
    }

    public static  CompositeIterable with(Iterable... iterables)
    {
        return new CompositeIterable(FastList.newListWith(iterables));
    }

    public void each(final Procedure procedure)
    {
        this.iterables.forEach(new Procedure>()
        {
            public void value(Iterable iterable)
            {
                Iterate.forEach(iterable, procedure);
            }
        });
    }

    @Override
    public void forEachWithIndex(final ObjectIntProcedure objectIntProcedure)
    {
        final Counter index = new Counter();
        this.iterables.forEach(new Procedure>()
        {
            public void value(Iterable iterable)
            {
                Iterate.forEach(iterable, new Procedure()
                {
                    public void value(E object)
                    {
                        objectIntProcedure.value(object, index.getCount());
                        index.increment();
                    }
                });
            }
        });
    }

    @Override
    public 

void forEachWith(final Procedure2 procedure, final P parameter) { this.iterables.forEach(new Procedure>() { public void value(Iterable iterable) { Iterate.forEachWith(iterable, procedure, parameter); } }); } public void add(Iterable iterable) { this.iterables.add(iterable); } public Iterator iterator() { return new CompositeIterator(this.iterables); } private final class CompositeIterator implements Iterator { private final Iterator> iterablesIterator; private Iterator innerIterator; private CompositeIterator(MutableList> iterables) { this.iterablesIterator = iterables.listIterator(); this.innerIterator = EmptyIterator.getInstance(); } public boolean hasNext() { while (true) { if (this.innerIterator.hasNext()) { return true; } if (!this.iterablesIterator.hasNext()) { return false; } this.innerIterator = this.iterablesIterator.next().iterator(); } } public E next() { if (!this.hasNext()) { throw new NoSuchElementException(); } return this.innerIterator.next(); } public void remove() { throw new UnsupportedOperationException("Cannot remove from a composite iterator"); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy