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

ix.IxCollect Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright 2011-2016 David Karnok
 *
 * 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 ix;

import java.util.Iterator;

final class IxCollect extends IxSource {

    final IxSupplier initialFactory;

    final IxConsumer2 collector;

    IxCollect(Iterable source, IxSupplier initialFactory, IxConsumer2 collector) {
        super(source);
        this.initialFactory = initialFactory;
        this.collector = collector;
    }

    @Override
    public Iterator iterator() {
        return new CollectorIterator(source.iterator(), collector, initialFactory.get());
    }

    static final class CollectorIterator extends IxSourceIterator {

        final IxConsumer2 collector;

        CollectorIterator(Iterator it, IxConsumer2 collector, C value) {
            super(it);
            this.collector = collector;
            this.value = value;
        }

        @Override
        protected boolean moveNext() {
            Iterator it = this.it;

            IxConsumer2 coll = collector;

            C c = value;

            while (it.hasNext()) {
                coll.accept(c, it.next());
            }

            hasValue = true;
            done = true;
            return true;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy