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

org.voovan.tools.Chain Maven / Gradle / Ivy

There is a newer version: 4.3.8
Show newest version
package org.voovan.tools;

import java.util.ArrayDeque;
import java.util.Iterator;
/**
 * 对象链
 * 
 * @author helyho
 *
 * Voovan Framework.
 * WebSite: https://github.com/helyho/Voovan
 * Licence: Apache v2 License
 */
public class Chain extends ArrayDeque{
	private Iterator iterator;
	private Iterator invertedIterator;
	private boolean isStop;
	private E currentObj;
	
	/**
	 * 构造函数
	 */
	public Chain(){
		isStop = false;
		rewind();
	}
	
	/**
	 * 重置链的迭代器
	 */
	public void rewind(){
		isStop = false;
		iterator = this.iterator();
		invertedIterator = this.descendingIterator();
	}
	
	/**
	 * 迭代完成
	 */
	public void stop(){
		this.isStop = true;
	}

	/**
	 * 迭代器当前元素
	 * @return 当前元素
	 */
	public E current(){
		return currentObj;
	}

	/**
	 * 迭代器下一个元素
	 * @return 下一个元素
	 */
	public E next(){
		if(isStop){
			return null;
		} else {
			if(iterator.hasNext()){
				currentObj = iterator.next();
				return currentObj;
			} else {
				return null;
			}
		}
	}

	/**
	 * 迭代器是否有下一个对象
	 * @return 是否有下一个对象
	 */
	public boolean hasNext(){
		if(isStop){
			return false;
		} else {
			return iterator.hasNext();
		}
	}

	/**
	 * 迭代器上一个元素
	 * @return 上一个元素
	 */
	public E previous(){
		if(isStop){
			return null;
		} else {
			if(invertedIterator.hasNext()){
				currentObj = invertedIterator.next();
				return currentObj;
			} else {
				return null;
			}
		}
	}

	/**
	 * 迭代器是否有上一个对象
	 * @return 是否有上一个对象
	 */
	public boolean hasPrevious(){
		if(isStop){
			return false;
		} else {
			return invertedIterator.hasNext();
		}
	}
	
	/**
	 *  从当前对象克隆一个 Chain
	 *  @return 克隆后的对象
	 */
	public Chain clone(){
		ArrayDeque cloned = super.clone();
		Chain chain = new Chain();
		chain.addAll(cloned);
		chain.rewind();
		cloned.clear();
		return chain;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy