Package webit.script.lang.iter

Source Code of webit.script.lang.iter.IterFilter

// Copyright (c) 2013-2014, Webit Team. All Rights Reserved.
package webit.script.lang.iter;

import java.util.NoSuchElementException;
import webit.script.lang.Iter;

/**
*
* @author zqq
*/
public abstract class IterFilter implements Iter {

    protected final Iter iter;
    protected boolean gotNext;
    protected Object nextItem;
    protected int _index;

    protected IterFilter(Iter iter) {
        this.iter = iter;
        this._index = -1;
    }

    protected abstract boolean valid(Object item);

    public final Object next() {
        ++ this._index;
        if (hasNext()) {
            this.gotNext = false;
            return this.nextItem;
        }
        throw new NoSuchElementException("no more next");
    }

    public final boolean hasNext() {
        if (this.gotNext) {
            return true;
        }
        Iter it;
        while ((it = this.iter).hasNext()) {
            Object item;
            if (valid(item = it.next())) {
                this.gotNext = true;
                this.nextItem = item;
                return true;
            }
        }
        return false;
    }

    public final boolean isFirst() {
        return this._index == 0;
    }

    public final int index() {
        return this._index;
    }
}
TOP

Related Classes of webit.script.lang.iter.IterFilter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.