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

com.fizzed.rocker.runtime.IterableForIterator Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
/*
 * Copyright 2015 Fizzed Inc.
 *
 * 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.fizzed.rocker.runtime;

import com.fizzed.rocker.ForIterator;

import java.util.Iterator;

/**
 * ForIterator implementation that wraps an Iterable and actually
 * handles the iteration of elements.
 */
public class IterableForIterator implements ForIterator {

    private final Iterator iterator;
    private int index;

    public IterableForIterator(Iterable c) {
        this(c.iterator());
    }

    public IterableForIterator(T[] a) {
        this((Iterable) new PrimitiveCollections.ObjectCollection(a));
    }

    public IterableForIterator(boolean[] a) {
        this((Iterable) new PrimitiveCollections.BooleanCollection(a));
    }

    public IterableForIterator(byte[] a) {
        this((Iterable) new PrimitiveCollections.ByteCollection(a));
    }

    public IterableForIterator(char[] a) {
        this((Iterable) new PrimitiveCollections.CharacterCollection(a));
    }

    public IterableForIterator(short[] a) {
        this((Iterable) new PrimitiveCollections.ShortCollection(a));
    }

    public IterableForIterator(int[] a) {
        this((Iterable) new PrimitiveCollections.IntegerCollection(a));
    }

    public IterableForIterator(long[] a) {
        this((Iterable) new PrimitiveCollections.LongCollection(a));
    }

    public IterableForIterator(float[] a) {
        this((Iterable) new PrimitiveCollections.FloatCollection(a));
    }

    public IterableForIterator(double[] a) {
        this((Iterable) new PrimitiveCollections.DoubleCollection(a));
    }

    public IterableForIterator(Iterator iterator) {
        this.iterator = iterator;
        this.index = -1;
    }

    public T next() {
        T t = iterator.next();
        this.index++;
        return t;
    }

    public boolean hasNext() {
        return iterator.hasNext();
    }

    @Override
    public int index() {
        return index;
    }

    @Override
    public boolean first() {
        return index == 0;
    }

    @Override
    public boolean last() {
        return !iterator.hasNext();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy