package com.qmth.boot.tools.iterator;

import java.util.Iterator;

/**
 * 只包含单个对象的迭代器工具
 */
public class SingletonIterator<E> implements Iterator<E> {

    private E obj;

    private boolean finish;

    /**
     * 构造单对象迭代器
     *
     * @param obj - 包含的单个对象
     */
    public SingletonIterator(E obj) {
        this.obj = obj;
        this.finish = false;
    }

    @Override
    public boolean hasNext() {
        return !finish;
    }

    @Override
    public E next() {
        if (finish) {
            return null;
        } else {
            finish = true;
            return obj;
        }
    }

}
